Goal of the FRQ
This free-response question asks you to write a complete Java class called HiddenWord.
The class must:
- Store the hidden word.
- Have a constructor that receives the hidden word.
- Have a method called
getHint. - Compare the hidden word with the guess one letter at a time.
- Return a hint using letters, plus signs, and stars.
Hint Rules
| If the guess letter is... | The hint should show... |
|---|---|
| In the same position as the hidden word | The matching letter |
| In the hidden word, but in a different position | + |
| Not in the hidden word | * |
Step 1: Create the Class
The problem says to write the complete HiddenWord class.
public class HiddenWord
{
}
Step 2: Add an Instance Variable
The class needs to remember the hidden word.
private String word;
This variable stores the hidden word that is passed into the constructor.
Step 3: Write the Constructor
The constructor receives the hidden word and stores it in the instance variable.
public HiddenWord(String h)
{
word = h;
}
HiddenWord puzzle = new HiddenWord("HARPS");
Now the instance variable word stores "HARPS".
Step 4: Understand the getHint Method
The getHint method receives a guess and returns a hint.
puzzle.getHint("HEART");
The hidden word is:
HARPS
The guess is:
HEART
Now compare each letter by position.
| Index | Hidden Word | Guess | Result |
|---|---|---|---|
| 0 | H | H | Same position, use H |
| 1 | A | E | E is not in HARPS, use * |
| 2 | R | A | A is in HARPS, but different position, use + |
| 3 | P | R | R is in HARPS, but different position, use + |
| 4 | S | T | T is not in HARPS, use * |
The final hint is:
H*++*
Step 5: Use a Loop
Since every letter must be checked, use a for loop.
for (int i = 0; i < guess.length(); i++)
{
}
This loop checks each character in the guess from left to right.
Step 6: Get Each Letter
Use substring to get one character at a time.
String letter = guess.substring(i, i + 1);
If guess is "HEART", then:
guess.substring(0, 1)
returns:
"H"
Step 7: Check for Exact Match First
The most important rule is checking whether the guessed letter is in the exact same position as the hidden word.
if (letter.equals(word.substring(i, i + 1)))
{
hint += letter;
}
This checks whether the guessed letter matches the hidden word at the same index.
Hidden: H A R P S
Guess: H E A R T
Index: 0
The H matches exactly, so the hint gets H.
Step 8: Check if the Letter Exists Somewhere Else
If the letter is not an exact match, check if it appears anywhere in the hidden word.
Use indexOf.
word.indexOf(letter)
If the letter is found, indexOf returns its position.
If the letter is not found, indexOf returns -1.
else if (word.indexOf(letter) != -1)
{
hint += "+";
}
If the hidden word is "HARPS" and the guess letter is
"A", then A is in the hidden word.
Since it is not in the correct position, add +.
Step 9: If the Letter Is Not Found
If the letter is not an exact match and is not in the hidden word, add
*.
else
{
hint += "*";
}
Complete Solution
public class HiddenWord
{
private String word;
public HiddenWord(String h)
{
word = h;
}
public String getHint(String guess)
{
String hint = "";
for (int i = 0; i < guess.length(); i++)
{
String letter = guess.substring(i, i + 1);
if (letter.equals(word.substring(i, i + 1)))
{
hint += letter;
}
else if (word.indexOf(letter) != -1)
{
hint += "+";
}
else
{
hint += "*";
}
}
return hint;
}
}
Walkthrough Example
HiddenWord puzzle = new HiddenWord("HARPS");
System.out.println(puzzle.getHint("HARMS"));
Hidden word:
HARPS
Guess:
HARMS
| Index | Hidden | Guess | Hint |
|---|---|---|---|
| 0 | H | H | H |
| 1 | A | A | A |
| 2 | R | R | R |
| 3 | P | M | * |
| 4 | S | S | S |
The result is:
HAR*S
Why This Works
The method checks each guessed letter in the correct order.
- If the letter is exactly correct, add the letter.
- Otherwise, if the letter exists somewhere in the hidden word, add
+. - Otherwise, add
*.
Common Student Mistakes
Mistake 1: Checking indexOf First
Incorrect:
if (word.indexOf(letter) != -1)
{
hint += "+";
}
+, which is wrong.
You must check exact matches first.
Mistake 2: Using == to Compare Strings
Incorrect:
if (letter == word.substring(i, i + 1))
Correct:
if (letter.equals(word.substring(i, i + 1)))
.equals() to compare the contents of Strings.
Mistake 3: Forgetting to Return the Hint
The method must return the completed hint.
return hint;
Student-Friendly Summary
The HiddenWord class stores a secret word.
The getHint method compares a guess to the secret word
one letter at a time.
For each letter:
| Situation | Hint Character |
|---|---|
| Exact match | Show the letter |
| Wrong spot but in the word | + |
| Not in the word | * |
The final hint is built one character at a time and returned as a
String.