Home 2015 FRQ 2
Post
Cancel

2015 FRQ 2

  1. 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class HiddenWord {
    //instance variable word
    private String word;

    //method to create hidden word
    public HiddenWord(String hword) {
        word = hword;
    }

    //method to get hint
    public String getHint(String guess){
        String hint = "";
        for (int i=0; i< guess.length(); i++){
            if (guess.charAt(i) == word.charAt(i)){
                hint += guess.charAt(i);
            }
            //had to google search these syntaxes. 
            //"valueOf" converts the character at index i in the guess string to a string
            else if(word.contains(String.valueOf(guess.charAt(i)))){
                hint += "+";
            }
            else {
                hint += "*";
            }
        }
        return hint;

    }

    //testing
    public static void main(String[] args) {
            // create an instance of the HiddenWord class
            HiddenWord hiddenWordInstance = new HiddenWord("FROG");
    
            // call the getHint method on the instance
            String hintResult1 = hiddenWordInstance.getHint("FLOW");
            String hintResult2 = hiddenWordInstance.getHint("FOUR");
            String hintResult3 = hiddenWordInstance.getHint("FROG");

            // print the result
            System.out.println(hintResult1);
            System.out.println(hintResult2);
            System.out.println(hintResult3);
    }
}

HiddenWord.main(null);
1
2
3
F*O*
F+*+
FROG
This post is licensed under CC BY 4.0 by the author.

2015 FRQ 1

2015 FRQ 3