Question 2: Classes

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.

image

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.

image

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.

public class HiddenWord{
    private String hiddenWord;
    
    public HiddenWord(String hiddenWord){
        this.hiddenWord = hiddenWord.toUpperCase().strip();
    }

    public String getHiddenWord(){
        return this.hiddenWord;
    }

    public String getHint(String guess){
        guess = guess.strip().toUpperCase();
        String word = this.getHiddenWord();
        int wordLength = word.length(); 
        char[] hintArr = new char[wordLength];

        for(int i = 0; i < wordLength; i++){
            hintArr[i] = '*';
        }

        for(int i = 0; i < guess.length(); i++){   
            if(guess.charAt(i) == word.charAt(i)){
                hintArr[i] = guess.charAt(i);
            } else if(word.indexOf(guess.charAt(i)) != -1){
                hintArr[i] = '+';
            }     
        }
        
        String hint = String.valueOf(hintArr);
        return hint;
    }

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

HiddenWord.main(null);
+A+++
H****
H*++*
HAR*S
HARPS

Reflection

This problem was very interesting in the sense that it was about testing a lot of the fundamental principles about classes from asking about how to declare the HiddenWord class and creating the needed constructor for the class and practicing the basics on that front, which due to the familiarity with creating many classes to accomplish the functions of our prior projects it was very simple. The added aspect of the question from being able to find elements at the needed positions in Strings was testing our familiarity with the chatAt and indexOf methods to see what was at certain indices of a string and whether or not a certain character was in a string this was useful as we don’t really use these methods often and was a good refresher. However, I later realized the method that I had used for returning a the hint from creating a an array of characters then assigning the various elements, +, *, and the character then converting it into a string and then returning it was a bit overkill and I could have just used a string an then concatenated the characters to the string and then returned it. This was a good reminder to not be too tunnel visioned as I was thinking a bit about the prior problem which was about Arrays and was the solution I had come up with.