Reflection and Solutions of the FRQ
public class HiddenWord {
private String hidden; // the hidden word to guess
// constructor to initialize the hidden word
public HiddenWord(String h) {
hidden = h;
}
// method to generate a hint based on the guessed word
public String getHint(String guess) {
String hint = "{ "; // start building the hint string
for (int i = 0; i < guess.length(); i++) {
if (guess.charAt(i) == hidden.charAt(i))
hint += "" + guess.charAt(i); // append the correct character at the right position
else if (hidden.indexOf(guess.charAt(i)) > -1)
hint += "+"; // append a '+' if the guessed character exists in the hidden word but at a different position
else
hint += "*"; // append a '*' if the guessed character doesn't exist in the hidden word
}
hint += " }"; // close the hint string
return hint; // return the generated hint
}
//Proof it works
public static void main(String[] args) {
HiddenWord word = new HiddenWord("Finn");
System.out.println(word.getHint("swim"));
System.out.println(word.getHint("Fred"));
}
}
HiddenWord.main(null);
{ **+* }
{ F*** }