Home 2015 CSA FRQ Q2
Post
Cancel

2015 CSA FRQ Q2

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 table is shown with two columns titled If the letter in the guess is… on the left and the corresponding character in the hint is on the right. The first row reads also in the same position in the hidden word on the left, and the matching letter on the right. The second row reads also in the hidden word but in a different position on the left, and a plus sign on the right. The third row reads not in the hidden word on the left, and an asterisk on the right.

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 table is shown with two columns titled Call to getHint on the left and String returned on the right. The first row reads puzzle dot getHint open parentheses “AAAAA” close parentheses on the left, and “plus A plus plus plus” on the right. The second row reads puzzle dot getHint open parentheses “HELLO” close parentheses on the left, and “H asterisk asterisk asterisk asterisk” on the right. The third row reads puzzle dot getHint open parentheses “HEART” close parentheses on the left, and “H asterisk plus plus asterisk” on the right. The fourth row reads puzzle dot getHint open parentheses “HARMS” close parentheses on the left, and “HAR asterisk S” on the right. The fifth row reads puzzle dot getHint open parentheses “HARPS” close parentheses on the left, and “HARPS” on the right.

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 String getHint(String guess) {
        String hint = "";
        for (int i = 0; i < guess.length(); i++) {
            if (guess.substring(i, i + 1).equals(word.substring(i, i + 1))) {
                hint += guess.substring(i, i + 1);
            } else if (word.indexOf(guess.substring(i, i + 1)) != -1) {
                hint += "+";
            } else {
                hint += "*";
            }
        }
        return hint;
    }
}

/* explanation:
 * 
 * Given a hidden string, we take the guess and compare each character of the guess with the hidden string.
 * The order of our if statement logic is really important here. We first check for a 1:1 match, adding the character to the hint.
 * Next, we look for whether or not the letter is somewhere in the string, adding a "+" to the hint.
 * Lastly, we just add a "*" if none of the conditions are met.
 * 
 */

HiddenWord puzzle = new HiddenWord("HARPS");
System.out.println(puzzle.getHint("AAAAA")); // should return "+A+++"
System.out.println(puzzle.getHint("HELLO")); // should return "H****"
System.out.println(puzzle.getHint("HEART")); // should return "H*++*"
System.out.println(puzzle.getHint("HARMS")); // should return "HAR*S"
System.out.println(puzzle.getHint("HARPS")); // should return "HARPS"
1
2
3
4
5
+A+++
H****
H*++*
HAR*S
HARPS
This post is licensed under CC BY 4.0 by the author.