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.

public class HiddenWord{

    private String word;

    public HiddenWord(String word){
        this.word = word;
    } 

    public boolean inWord(char charAtWord){
        for(int i = 0; i < this.word.length(); i++){
            if(this.word.charAt(i) == charAtWord){
                return true;
            }
        }
        return false;
    }

    public boolean isChar(char charAtWord, int index){
        if(this.word.charAt(index) == charAtWord){
            return true;
        }
        return false;
    }

    public String getHint(String guess){  
        String returnString = "" ;
        for (int i = 0; i < this.word.length(); i++){
            char currChar = guess.charAt(i);
            if(isChar(currChar, i)){
                returnString += currChar;
            }

            else if (inWord(currChar)){
                returnString += '+';
            }

            else {
                returnString += '*';
            }
        }
        return returnString;
    }

    public static void main(String[] args){
        // testing with above example
        HiddenWord harps = new HiddenWord("HARPS");

        System.out.println(harps.getHint("HEART"));
        System.out.println(harps.getHint("HELLO"));
        System.out.println(harps.getHint("HARPS"));
    }

}

HiddenWord.main(null);
H*++*
H****
HARPS

Another way, using StringBuilder

public class HiddenWord2 {

    private String word;

    public HiddenWord2(String word) {
        this.word = word;
    }

    public boolean inWord(char charAtWord) {
        for (int i = 0; i < this.word.length(); i++) {
            if (this.word.charAt(i) == charAtWord) {
                return true;
            }
        }
        return false;
    }

    public boolean isChar(char charAtWord, int index) {
        return this.word.charAt(index) == charAtWord;
    }

    public String getHint(String guess) {
        StringBuilder returnStringBuilder = new StringBuilder();
        for (int i = 0; i < this.word.length(); i++) {
            char currChar = guess.charAt(i);
            if (isChar(currChar, i)) {
                returnStringBuilder.append(currChar);
            } else if (inWord(currChar)) {
                returnStringBuilder.append('+');
            } else {
                returnStringBuilder.append('*');
            }
        }
        return returnStringBuilder.toString();
    }

    public static void main(String[] args) {
        // testing with above example
        HiddenWord2 harps = new HiddenWord2("HARPS");

        System.out.println(harps.getHint("HEART"));
        System.out.println(harps.getHint("HELLO"));
        System.out.println(harps.getHint("HARPS"));
    }
}

HiddenWord2.main(null)

H*++*
H****
HARPS