Home 2015 FRQ Question 2
Post
Cancel

2015 FRQ Question 2

2015 Collegeboard 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…

  • Also in the same position in the hidden word -> hint: matching letter
  • Also in the hidden word, but in the different position -> hint: “+”
  • Not in the hidden word -> 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.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class HiddenWord {
    private String word;
    public HiddenWord(String w){
        word = w;
    }
    
    public String getHint(String guess){
        String s="";
        for (int i = 0; i <guess.length(); i++){
            if (word.charAt(i) == guess.charAt(i)){
                s += word.charAt(i);
            } else if (word.indexOf(guess.charAt(i)) != -1){
                s += "+";
            } else {
                s += "*";
            }
        }
        return s;
    }

    // test
    public static void main(String[] args){
        HiddenWord puzzle = new HiddenWord("HARPS");
        
        System.out.println(puzzle.getHint("AAAAA"));
        System.out.println(puzzle.getHint("HEART"));
        System.out.println(puzzle.getHint("HARMS"));
        System.out.println(puzzle.getHint("HARPS"));
    }
}
HiddenWord.main(null);
1
2
3
4
+A+++
H*++*
HAR*S
HARPS

Blog

The specific type of FRQ 2 is classes. It focused on the HiddenWord class and using OOP and string manipulation to create a game where it would give you hints as you try to guess a hidden word. HiddenWord class contains all necessary components, such as the private string word to be guessed, constructor to initialize it, getHint method which is the main part that has conditional statements to check the guess.

This post is licensed under CC BY 4.0 by the author.