Question 2

Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Notes:

  • Assume that the classes listed in the Java Quick Reference have been imported where appropriate.

  • Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.

  • In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.

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.

a

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.

a

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.

Solution:

public class HiddenWord {
    public String secret;

    // just public to be used as a constructor
    public HiddenWord(String word) {
        this.secret = word;
    }

    public String getHint(String guess) {
        String word = this.secret;
        String hint = "";
        int len = word.length();

        // Not necessary but I felt like it
        if (len != guess.length()) {
            return "Invalid guess";
        }

        for (int i = 0; i < len; i ++) {
            char guessChar = guess.charAt(i);
            char wordChar = word.charAt(i);

            if (word.indexOf(guessChar) != -1) {
                if (wordChar == guessChar) {
                    hint += guessChar;
                }

                else {
                    hint += "+";
                }
            }

            else {
                hint += "*";
            }
        }

        return hint;
    }
}

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"));
+A+++
H****
H*++*
HAR*S


HARPS

Reflection

Again, the code itself wasn’t hard, just syntax stuff. This time, it was with making a constructor, where I autopiloted and added “void” to it, which makes Java treat it as a method instead of a contructor.

This one’s type was method and control structures. Woo.