Quick Note
For each part of this FRQ and the other three FRQs, I included two versions of the solution: one with no runtime (i.e. CollegeBoard’s key) and one with runtime.
Question 2: Classes
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Notes:
-
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
-
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.
-
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.
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.
Question 2a (the only part of this question)
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.
No Runtime
public class HiddenWord {
private String concealed;
public HiddenWord(String c){
concealed = c;
}
public String getHint(String guess){
String result = "";
for(int i = 0; i < guess.length(); i++){
String chunk = concealed.substring(i, i+1);
String letter = guess.substring(i, i+1);
if(chunk.equals(letter)){
result += chunk;
} else if (concealed.indexOf(letter) != -1){
result += "+";
} else {
result += "*"
}
}
return result;
}
}
With Runtime
public class HiddenWord {
private String word;
// initializes the hidden word
public HiddenWord(String hWord) {
word = hWord;
}
// produces a hint based on the guess
public String getHint(String guess) {
StringBuilder hint = new StringBuilder();
for (int i = 0; i < guess.length(); i++) {
String guessLetter = guess.substring(i, i + 1);
String hiddenLetter = word.substring(i, i + 1);
if (guessLetter.equals(hiddenLetter)) {
hint.append(guessLetter); // correct letter in the correct position
} else if (word.contains(guessLetter)) {
hint.append('+'); // correct letter but in the wrong position
} else {
hint.append('*'); // not a correct letter
}
}
return hint.toString();
}
// tests the HiddenWord class
public static void main(String[] args) {
HiddenWord puzzle = new HiddenWord("CODING");
// Tests the getHint method with a list of various guesses
String[] guesses = {"CCCCCC", "SKETCH", "COPPER", "CODERS", "CODING"};
for (String guess : guesses) {
System.out.println("Guess: " + guess + " - Hint: " + puzzle.getHint(guess));
}
}
}
HiddenWord.main(null);
Guess: CCCCCC - Hint: C+++++
Guess: SKETCH - Hint: ****+*
Guess: COPPER - Hint: CO****
Guess: CODERS - Hint: COD***
Guess: CODING - Hint: CODING
Reflection for FRQ 2
Looking through all of question 2, this was definitely a Classes type question, as the whole question was based on being able to write the code for the entire HiddenWord class based on the information and rules that College Board gave for the guessing game. For the most part, completing this FRQ was not very difficult; however, the only thing that I was stunned on while completing this question was thinking about what the code would look like for if the user guessed a correct letter but it was in the wrong position. That was when I realized I could use != -1 because of the fact that -1 would indicate that the letter was not in the word at all. By writing != -1, that indicates that the letter the user guessed is an index in the word for the user to guess, but it is just in the wrong position. Overall, I also didn’t have too terrible of a time completing this FRQ, although I do think that it is important that I review concepts like these further as we approach the AP exam.