Unit 2 HW
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){
/* implementation not shown */
}
/* Returns a score for guess, as describe in part (a)
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess (String guess){
int value = 0;
int length = guess.length();
for (int i = 0; i<= secret.length - length; i+=1){
String a = secret.substring(i, i+length);
if (a.equals(guess)){
value++;
}
return value*length*length;
}
}
/* Returns the better of two guesses, as determined by scoreGuess and the rules a
* tie breaker that are describe in part (b)
* Precondition: guess 1 and guess 2 are all lowercase letters.
* guess 1 is not the same as guess 2
*/
public String findBetterGuess(String guess1, String guess2){
int x = scoreGuess(guess1);
int y = scoreGuess(guess2);
if (x > y) return guess1;
if (y > x) return guess2;
}
}