Sprint View

2015 FRQ 2

3 min read

FRQ 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.

If the letter in the guess is … the corresponding character in the hint is
also in the same position in the hidden word, the matching letter
also in the hidden word, but in a different position "+"
not in the hidden word, "*"
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.
Call to getHint String returned
puzzle.getHint("AAAAA") "+A+++"
puzzle.getHint("HELLO") "H****"
puzzle.getHint("HEART") "H*++*"
puzzle.getHint("HARMS") "HAR*S"
puzzle.getHint("HARPS") "HARPS"
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.

Code Runner Challenge

HiddenWord

View IPYNB Source
// CODE_RUNNER: HiddenWord
public class Main {

    // ===============================
    // 2015 FRQ #2 — HiddenWord class
    // ===============================
    public static class HiddenWord {

        // STUDENT CODE STARTS HERE
        private String word;
        // STUDENT CODE ENDS HERE

        /**
         * Constructs a HiddenWord object.
         * @param hWord the word to be guessed
         * Precondition: hWord contains only uppercase letters.
         */
        public HiddenWord(String hWord) {

            // STUDENT CODE STARTS HERE
            word = hWord;
            // STUDENT CODE ENDS HERE
        }

        /**
         * Returns a hint for guess.
         * @param guess the word guessed
         * Precondition: guess has the same length as the hidden word
         *               and contains only uppercase letters.
         */
        public String getHint(String guess) {

            // STUDENT CODE STARTS HERE

            String hint = "";

            for (int i = 0; i < guess.length(); i++) {
                String guessLetter = guess.substring(i, i + 1);

                if (guessLetter.equals(word.substring(i, i + 1))) {
                    hint += guessLetter;
                } else if (word.indexOf(guessLetter) != -1) {
                    hint += "+";
                } else {
                    hint += "*";
                }
            }

            return hint;

            // STUDENT CODE ENDS HERE
        }
    }

    // ===============================
    // Driver to prove it runs
    // ===============================
    public static void main(String[] args) {

        HiddenWord puzzle = new HiddenWord("HARPS");

        System.out.println(puzzle.getHint("AAAAA")); // +****
        System.out.println(puzzle.getHint("HELLO")); // H****
        System.out.println(puzzle.getHint("HEART")); // H*++*
        System.out.println(puzzle.getHint("HARMS")); // HAR*S
    }
}

// Required by runner
Main.main(null);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
## Scoring Guidelines Intent: Define implementation of class to represent hidden word in guessing game Part 1a
Points Criterion
+1 Uses correct class, constructor, and method headers
+1 Declares appropriate private instance variable
+1 Initializes instance variable within constructor using parameter
Part 2a: Implement getHint (6 pts)
+1 Accesses all letters in both guess and hidden word in loop (n bounds errors in either)
+1 Extracts and compares corresponding single letters from guess and hidden word
+1 Tests whether guess letter occurs in same position in both guess and hidden word
+1 Tests whether guess letter occurs in hidden word but not in same position as in guess
+1 Adds correct character exactly once to the hint string based on the test result
+1 Declares, initializes, and returns constructed hint string

Course Timeline