Skip to main content Link Menu Expand (external link) Document Search Copy Copied

FRQ 2 • 8 min read

Description

Final FRQ 2


2

Screenshot 2024-02-20 at 12 47 58 PM

Answer:

public class HiddenWord {

    // A private instance variable that holds the hidden word
    private String hiddenWord;

    // A constructor that takes a String argument to initialize the hidden word
    public HiddenWord(String hiddenWord) {
        this.hiddenWord = hiddenWord;
    }

    // A method that takes a guess and returns a hint based on the guess
    public String getHint(String guess) {
        StringBuilder hint = new StringBuilder();

        // Iterate over each character in the hidden word
        for (int i = 0; i < hiddenWord.length(); i++) {
            // Get the corresponding characters from the guess and the hidden word
            char guessChar = guess.charAt(i);
            char hiddenChar = hiddenWord.charAt(i);

            // If the guessed character matches the hidden character at the same position
            if (guessChar == hiddenChar) {
                hint.append(guessChar);
            } 
            // If the guessed character is in the hidden word but at a different position
            else if (hiddenWord.indexOf(guessChar) != -1) {
                hint.append("+");
            } 
            // If the guessed character is not in the hidden word
            else {
                hint.append("*");
            }
        }

        return hint.toString();
    }
}

public static void main(String[] args) {
        HiddenWord puzzle = new HiddenWord("TANAY");
        System.out.println("Guess: AAAAA -> Hint: " + puzzle.getHint("AAAAA"));
        System.out.println("Guess: HELLO -> Hint: " + puzzle.getHint("TANNED"));
        System.out.println("Guess: HEART -> Hint: " + puzzle.getHint("TINED"));
        System.out.println("Guess: HARMS -> Hint: " + puzzle.getHint("TAHITI"));
        System.out.println("Guess: HARPS -> Hint: " + puzzle.getHint("TANAY"));
    }

FRQ #2

public class HiddenWord { //
    // A private instance variable that holds the hidden word
    private String hiddenWord;

    // A constructor that takes a String argument to initialize the hidden word
    public HiddenWord(String hiddenWord) {
        this.hiddenWord = hiddenWord;
    }

    // A method that takes a guess and returns a hint based on the guess
    public String getHint(String guess) {
        // Create a StringBuilder to build the hint
        StringBuilder hint = new StringBuilder();

        // Iterate over each character in the hidden word
        for (int i = 0; i < hiddenWord.length(); i++) {
            // Get the corresponding characters from the guess and the hidden word
            char guessChar = guess.charAt(i);
            char hiddenChar = hiddenWord.charAt(i);

            // If the guessed character matches the hidden character at the same position
            if (guessChar == hiddenChar) {
                // Append the correct character to the hint
                hint.append(guessChar);
            } 
            // If the guessed character is in the hidden word but at a different position
            else if (hiddenWord.indexOf(guessChar) != -1) {
                // Append a "+" to the hint
                hint.append("+");
            } 
            // If the guessed character is not in the hidden word
            else {
                // Append a "*" to the hint
                hint.append("*");
            }
        }

        // Return the hint as a String
        return hint.toString();
    }

    public static void main(String[] args) {
        // Create a new HiddenWord object with the word "TANAY"
        HiddenWord puzzle = new HiddenWord("TANAY");
        // Print out the hints for various guesses
        System.out.println("Guess: AAAAA -> Hint: " + puzzle.getHint("AAAAA"));
        System.out.println("Guess: HELLO -> Hint: " + puzzle.getHint("TANNED"));
        System.out.println("Guess: HEART -> Hint: " + puzzle.getHint("TINED"));
        System.out.println("Guess: HARMS -> Hint: " + puzzle.getHint("TAHITI"));
        System.out.println("Guess: HARPS -> Hint: " + puzzle.getHint("TANAY"));
    }
}
HiddenWord.main(null)

Guess: AAAAA -> Hint: +A+A+
Guess: HELLO -> Hint: TAN+*
Guess: HEART -> Hint: T*N**
Guess: HARMS -> Hint: TA**+
Guess: HARPS -> Hint: TANAY