Skip to the content.

2015 Practice FRQ Question 2 • 8 min read

Description

Analysis and breakdown of Question 2 from the 2015 AP Exam

Question 2: CLASSES

image

Question breakdown and approach:

This question describes a guessing game where a player attempts to guess a hidden word consisting of capital letters. The player receives hints after each guess, where each hint is based on a comparison between the hidden word and the guess. The hint follows specific rules: matching letters in the same position, letters in different positions marked with a plus sign, and letters not in the hidden word marked with an asterisk. The task is to create a class named HiddenWord to represent the hidden word, with a constructor accepting the hidden word as a parameter and a method named getHint that takes a guess and produces a hint. Approach: (1) Define an instance variable in the HiddenWord class to store the hidden word. (2) Create a constructor to initialize the hidden word when an object of the HiddenWord class is created. (3) Implement the getHint method to compare the guess with the hidden word based on the rules provided. (4) Iterate through each character in the guess, compare it with the corresponding character in the hidden word, and construct the hint accordingly. (5) Return the hint as a string.

Write the complete HiddenWord class, including any necessary instance variables, its constructor, and the method, getHint, described above. You may assume that the length of the guess is the same as the length of the hidden word.

import java.util.Scanner;

public class HiddenWord {
    private String word;

    public HiddenWord(String hiddenWord) {
        word = hiddenWord;
    }

    public String getHint(String guess) {
        String hint = "";
        for (int i = 0; i < guess.length(); i++) {
            if (guess.substring(i, i + 1).equals(word.substring(i, i + 1))) {
                hint += guess.substring(i, i + 1);
            } else if (word.indexOf(guess.substring(i, i + 1)) != -1) {
                hint += "+";
            } else {
                hint += "*";
            }
        }
        return hint;
    }

    public static void main(String[] args) {
        HiddenWord puzzle = new HiddenWord("HARPS");
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the Hidden Word Game!");
        System.out.println("Guess the Hidden Word 😁 (all caps, 5 letters):");

        boolean guessedCorrectly = false;
        while (!guessedCorrectly) {
            System.out.print("Enter your guess: ");
            String guess = scanner.nextLine();

            String hint = puzzle.getHint(guess);
            System.out.println("Hint: " + hint);

            if (hint.equals(puzzle.word)) {
                System.out.println("That is correct! Great job! ");
                guessedCorrectly = true;
            } else {
                System.out.println("Wrong! Keep trying...");
            }
        }
        scanner.close();
    }
}

HiddenWord.main(null);

Welcome to the Hidden Word Game!
Guess the Hidden Word 😁 (all caps, 5 letters):
Enter your guess: Hint: *****
Wrong! Keep trying...
Enter your guess: Hint: *****
Wrong! Keep trying...
Enter your guess: Hint: HARPS
That is correct! Great job! 

Summary:

I felt that this question was pretty straightforward to understand and implement. The task was to create a HiddenWord class with a constructor and a getHint method, where the getHint method compared each character in the guessed word with the hidden word and generated hints accordingly. Earlier in the year I was experimenting with making a Wordle style game, so having some knowledge from that definitely helped when I went to approach this problem.