public class Main{
    public static void main (String args[]){
        HiddenWord puzzle = new HiddenWord("HARPS");
        System.out.println(puzzle.getHint("AAAAA"));
        System.out.println(puzzle.getHint("HEART"));
    }
}

public class HiddenWord{
    private String word;
    public HiddenWord(String word){
        this.word = word;
    }
    public String getHint(String guess){
        String hint = "";
        for (int i = 0; i < guess.length(); i++){
            if (this.word.contains(String.valueOf(guess.charAt(i)))){
                if (this.word.charAt(i) == guess.charAt(i)){
                    hint = hint + guess.charAt(i);
                }
                else {
                    hint = hint + "+";
                }
            }
            else {
                hint = hint + "*";
            }
        }
        return hint;
    }
}
Main.main(null);
+A+++
H*++*

Learnings:

  1. I can check if a letter is contained in a string by using the .contains() method on the string
  2. length of a string is a method, length(). Length of an array is a property of the class.
  3. I can’t iterate through a string using brackents, I need to do charAt().
  4. String.contains can only include a string as the parameter, not a char. You need to convert a char if you’re going to use a char.

Reflection

This FRQ was mainly about a few of the basic java concepts like contains and even nested if statements. The FRQ had a branching logic chain, in which one answer led to false and another led to a second if statement. This was interesting to work with and is a useful concept to understand, and I feel I have a solid understanding of it. However, looking back, I can see why some programmers prefer an alternate syntax where you ask for the negative and then return earlier in order to avoid large chains of nested functions or if statements. It would have made my code more readable to check if instead if the word did not contain the letter and then just checked if the exact letter matched up, rather than checking both seperately.