Question 2

copied below

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.

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.

Type of FRQ

This is a Classes type of CSA question. The question asks us to completely design a new class, and it requires an understanding of object oriented programming, and knowing what a class needs such as constructor methods.

public class HiddenWord {

    private String answerWord;  // instance variable for the answer word

    public HiddenWord(String answerWord) { 
        this.answerWord = answerWord; // constructor method
    }

    public String getHint(String inputWord) {
        
        StringBuilder hintWord = new StringBuilder("*****"); // setting the hint word given to player to be entirely *

        for (int i = 0; i < answerWord.length(); i++) {
            char inputCharacter = inputWord.charAt(i); // pulling single character from the word inputted and answer word
            char answerCharacter = answerWord.charAt(i);

            if (inputCharacter == answerCharacter) {
                hintWord.setCharAt(i, inputCharacter); // if char matches, set char in answerWord
            }
            else if (answerWord.contains(String.valueOf(inputCharacter))){
                hintWord.setCharAt(i, '+'); // if char is in different index, set as +
            } 
        }

        return hintWord.toString();
    }

    public static void main(String[] args) {
        HiddenWord hiddenWord = new HiddenWord("HARPS");

        String input1 = "AAAAA";
        String input2 = "HELLO";
        String input3 = "HEART";
        String input4 = "HARMS";
        String input5 = "HARPS";

        System.out.println("Hint 1: " + hiddenWord.getHint(input1));  
        System.out.println("Hint 2: " + hiddenWord.getHint(input2));
        System.out.println("Hint 3: " + hiddenWord.getHint(input3));
        System.out.println("Hint 4: " + hiddenWord.getHint(input4));
        System.out.println("Hint 5: " + hiddenWord.getHint(input5));
    }
}

HiddenWord.main(null);
Hint 1: +A+++
Hint 2: H****
Hint 3: H*++*
Hint 4: HAR*S
Hint 5: HARPS

How I solved this problem:

I first set an instance variable for the word that the player is trying to guess (answerWord).

Then I created a constructor method of the HiddenWord class, which initializes answerWord as an argument.

The HiddenWord class first creates the output word which the player will receive as a hint. The output word is set as all asterisks as default, assuming the player guesses none of the letters. Then, the iteration loop goes through each possible character index of the answer and input words (which are the same length). The letters at each of these indexes are compared, and the output word’s character at that index changes accordingly.

Reflection

New Learning: I learned that the StringBuilder class must be used to create a new string. String builder allows the output string to be modified, which is essential for the purpose of this problem.

Also need to review the essential parts of a Java class object, such as instance variables, object properties, constructors, methods (including specific types like getters, setters, ect)