This question involves the WordMatch class, which stores a secret string and provides methods that compare other strings to the secret. The following class declaration shows the constructor, which takes the secret string, and two methods. You will implement the two methods.
WordMatch Class
public class WordMatch
{
/** The secret string */
private String secret;
/**
* Constructs a WordMatch object with the given secret string
* of lowercase letters
*/
public WordMatch(String word)
{
secret = word;
}
/**
* Returns a score for guess, as described in part (a)
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess(String guess)
{ /* to be implemented in part (a) */ }
/**
* Returns the better of two guesses, as determined by scoreGuess
* and the rules for a tie-Loss, as described in part (b)
* Precondition: guess1 and guess2 are not the same
*/
public String findBetterGuess(String guess1, String guess2)
{ /* to be implemented in part (b) */ }
}
Scoring Rules
The score for a guess is computed by finding the number of times the guess occurs as a substring of the secret, then multiplying by the square of the guess length.
score = (number of occurrences) × (guess.length())²
| Secret | Guess | Occurrences | Score |
|---|---|---|---|
| "mississippi" | "issippi" | 1 | 1 × 7² = 49 |
| "mississippi" | "iss" | 2 | 2 × 3² = 18 |
| "mississippi" | "sip" | 1 | 1 × 3² = 9 |
| "mississippi" | "mist" | 0 | 0 × 4² = 0 |