• actual answer compiled into running code:
  • FRQ reflection
  • QUESTION 2

    Consider a guessing game in which a player tries to guess a hidden word. The hidden word contains only capital letters and has a length known to the player. A guess contains only capital letters and has the same length as the hidden word.

    After a guess is made, the player is given a hint that is based on a comparison between the hidden word and the guess. Each position in the hint contains a character that corresponds to the letter in the same position in the guess. The following rules determine the characters that appear in the hint.

    The HiddenWord class will be used to represent the hidden word in the game. The hidden word is passed to the constructor. The class contains a method, getHint, that takes a guess and produces a hint.

    For example, suppose the variable puzzle is declared as follows.

    HiddenWord puzzle = new HiddenWord(“HARPS”);

    The following table shows several guesses and the hints that would be produced.

    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.

    my attempt at the problem:

    public class HiddenWord(String puzzle) {
        public String puzzle = "test";
    
        public static String getHint(String puzzle, String guess) {
            for (int i = 0; i < puzzle.length(); i++) {
                if (puzzle.charAt(i) == guess.charAt(i)) {
                    return puzzle.charAt(i);
                    else {
                        if (puzzle.contains(guess)) {
                            return "+";
                            else {
                                return "*";
                            }
                    }
                }
            }
        }
    }
    }
    

    actual answer compiled into running code:

    public class HiddenWord {
        private String word;
    
        // Constructor to initialize the hidden word
        public HiddenWord(String hWord) {
            word = hWord;
        }
    
        // Method to generate a hint based on the guess
        public String getHint(String guess) {
            String hint = "";
    
            // Ensure the guess length does not exceed the hidden word length
            int maxLength = Math.min(guess.length(), word.length());
    
            // Loop through each character in the guess
            for (int i = 0; i < maxLength; i++) {
                // Check if the character in the guess matches the character in the same position in the hidden word
                if (guess.substring(i, i + 1).equals(word.substring(i, i + 1))) {
                    hint += guess.substring(i, i + 1); // If match, append the character to hint
                } else if (word.indexOf(guess.substring(i, i + 1)) != -1) {
                    hint += "+"; // If the character exists in the hidden word but not in the correct position, append '+'
                } else {
                    hint += "*"; // If the character doesn't exist in the hidden word, append '*'
                }
            }
    
            return hint;
        }
    
        public static void main(String[] args) {
            // Example run
            HiddenWord hiddenWord = new HiddenWord("JAVA");
            String guess1 = "APPLE";
            String guess2 = "JUMP";
            
            System.out.println("Guess: " + guess1);
            System.out.println("Hint: " + hiddenWord.getHint(guess1));
            
            System.out.println("Guess: " + guess2);
            System.out.println("Hint: " + hiddenWord.getHint(guess2));
        }
    }
    
    HiddenWord.main(null);
    
    
    Guess: APPLE
    Hint: +***
    Guess: JUMP
    Hint: J***
    

    FRQ reflection

    This frq relies on analyzing strings and characters in each string, then comparing those characters in one string to another string.

    The frq also tests your ability to use constructors in classes and pass them to methods.

    Key algorithm: using .substring and if else statements to determine if character at index is present in puzzle word.