Home
Time Box
Calculator
Snake
Blogs
Hacks

Past FRQ question • 19 min read

Description

frq 2


FRQ 2021 Free Response questions

FRQ 2: Classes

Question from college board

This question involves the WordMatch class, which stores a secret string and provides methods that compare other strings to the secret string. You will write two methods in the WordMatch class.

public class WordMatch 
{ 
    /** The secret string. */ 
    private String secret; 
    /** Constructs a WordMatch object with the given secret string of lowercase letters. */ 
    public WordMatch(String word) 
    { 
    /* implementation not shown */ 
    } 
    /** Returns a score for guess, as described in part (a). 
    * Precondition: 0 < guess.length() <= secret.length() 
    */ 
    public int scoreGuess(String guess) 
    { /* to be implemented in part (a) */ } 
    /** Returns the better of two guesses, as determined by scoreGuess and the rules for a 
    * tie-breaker that are described in part (b). 
    * Precondition: guess1 and guess2 contain all lowercase letters. 
    * guess1 is not the same as guess2. 
    */ 
    public String findBetterGuess(String guess1, String guess2) 
    { /* to be implemented in part (b) */ } 
}
public class WordMatch {

    private String secret = "mississippi"; // The secret string to be matched against.

    // Calculate the score for a guess based on matching substrings.
    public int scoreGuess(String guess) {
        int count = 0; // Initialize a count for the number of matches.
        String temp; // Declare a temporary variable to store substrings.

        // Iterate through possible substrings of the secret string.
        for (int x = 0; x <= secret.length() - guess.length(); x++) {
            // Extract a substring of the same length as the guess.
            temp = secret.substring(x, x + guess.length());

            // Check if the extracted substring matches the guess, ignoring case.
            if (temp.equalsIgnoreCase(guess)) {
                count++;
            }
        }

        // Calculate the score based on the number of matches and the length of the guess.
        int score = count * guess.length() * guess.length();
        return score;
    }

    // Find the better guess among two options by comparing their scores and lexicographical order.
    public String findBetterGuess(String guess1, String guess2) {
        int score1 = scoreGuess(guess1); // Calculate the score for guess1.
        int score2 = scoreGuess(guess2); // Calculate the score for guess2.

        if (score1 > score2) {
            return guess1; // Return guess1 if it has a higher score.
        } else if (score2 > score1) {
            return guess2; // Return guess2 if it has a higher score.
        } else {
            // If both guesses have the same score, compare them lexicographically.
            if (guess1.compareTo(guess2) > 0) {
                return guess1;
            }
            return guess2;
        }
    }

    public static void main(String[] args) {
        WordMatch wordMatch = new WordMatch(); // Create an instance of the WordMatch class.
        
        // Find and print the better guess between "i" and "a".
        String betterGuess = wordMatch.findBetterGuess("i", "a");
        System.out.println("Better Guess: " + betterGuess);
    }
}

WordMatch.main(null);
Better Guess: i

Takeways

Scoring

  1. 1/1 Compares guess to a substring of secert
    • My code does that
  2. 1/1 Uses a substring of secert with correct length comparrasion
    • It choses the correct substring
    • Choosing I over A in mississippi
  3. 1/1 Loops through all necessary substrings of secret (no bounds errors)
    • Loops through the length of the secert string so no overlaps happen
  4. 1/1 Counts number of identified occurrences of guess within secret (in the context of a condition involving both secret and guess)
    • The count varible has this covered
  5. 1/1 Calculates score correctly
    • The score values when compared are correct
  6. 1/1 Calls ScoreGuess to get scores for guess1 and guess2
    • Calls that function when comparing the two
  7. 1/1 Compares Scores
    • If staments that does those things
  8. 0/1 Determines which of guess1 and guess2 is alphabetically greater
    • I didn’t do this
  9. 1/1 Returns the idenified guess1 or guess2
    • My code does do this

8/9

Adding more on to my code

What extra tools I needed to add

import java.util.Scanner;
import java.util.Random;

public class WordMatch {


    //New Variables
    private String secret;
    private int player1Score;
    private int player2Score;
    private Random random = new Random();
    private Scanner scanner = new Scanner(System.in);

    public WordMatch() {
        // Initialize scores and get a random secret word
        player1Score = 0;
        player2Score = 0;
        secret = generateRandomWord();
    }

    public int scoreGuess(String guess) {
        int count = 0;
        String temp;
        for (int x = 0; x <= secret.length() - guess.length(); x++) {
            temp = secret.substring(x, x + guess.length());
            if (temp.equalsIgnoreCase(guess)) {
                count++;
            }
        }
        int score = count * guess.length() * guess.length();
        return score;
    }

    public String findBetterGuess(String guess1, String guess2) {
        int score1 = scoreGuess(guess1);
        int score2 = scoreGuess(guess2);

        if (score1 > score2) {
            return guess1;
        } else if (score2 > score1) {
            return guess2;
        } else {
            if (guess1.compareTo(guess2) > 0) {
                return guess1;
            }
            return guess2;
        }
    }

    // Getting Random Word
    public String generateRandomWord() {
        String[] words = { "mississippi", "apple", "banana", "chocolate", "programming" };
        return words[random.nextInt(words.length)];
    }

    //Play Game Method
    public void playGame() {
        System.out.println("Welcome to the Word Matching Game!");
        System.out.println("Rule 1: No guessing the same letter (only applies to oneself)");
        System.out.println("Rule 2: Hide your guesses from the other player");
        System.out.println("Rule 3: Have fun");

        for (int round = 1; round <= 5; round++) {
            System.out.println("Round " + round);
            
            // Player 1's turn
            System.out.print("Player 1, enter your guess: ");
            String player1Guess = scanner.next();
            int player1ScoreRound = scoreGuess(player1Guess);
            player1Score += player1ScoreRound;
            System.out.println("Player 1 Score: " + player1Score);

            // Player 2's turn
            System.out.print("Player 2, enter your guess: ");
            String player2Guess = scanner.next();
            int player2ScoreRound = scoreGuess(player2Guess);
            player2Score += player2ScoreRound;
            System.out.println("Player 2 Score: " + player2Score);
        }

        System.out.println("Game Over!");

        if (player1Score > player2Score) {
            System.out.println("Player 1 wins!");
        } else if (player2Score > player1Score) {
            System.out.println("Player 2 wins!");
        } else {
            System.out.println("It's a tie!");
        }

        System.out.println("The secret word was: " + secret);
    }

    public static void main(String[] args) {
        WordMatch wordMatch = new WordMatch();
        wordMatch.playGame();
    }
}

WordMatch.main(null);
Welcome to the Word Matching Game!
Rule 1: No guessing the same letter (only applies to oneself)
Rule 2: Hide your guesses from the other player
Rule 3: Have fun
Round 1
Player 1, enter your guess: Player 1 Score: 1
Player 2, enter your guess: Player 2 Score: 1
Round 2
Player 1, enter your guess: Player 1 Score: 1
Player 2, enter your guess: Player 2 Score: 3
Round 3
Player 1, enter your guess: Player 1 Score: 2
Player 2, enter your guess: Player 2 Score: 4
Round 4
Player 1, enter your guess: Player 1 Score: 3
Player 2, enter your guess: Player 2 Score: 5
Round 5
Player 1, enter your guess: Player 1 Score: 3
Player 2, enter your guess: Player 2 Score: 5
Game Over!
Player 2 wins!
The secret word was: apple