Question 1A: Write the WordMatch method scoreGuess. To determine the score to be returned, scoreGuess finds the number of times that guess occurs as asubstring of secret and then multiplies that number by the square of the length of guess.Occurrences of guess may overlap within secret

  • The main method checks the FRQ returns the correct answers
public class WordMatch {
  private String secret;

  public WordMatch (String word) {
    this.secret = word;
  }

  public int scoreGuess (String guess) {
    String word_copy = secret;
    int score = 0;
    for (int i = 0; i<secret.length(); i++) {
      if (word_copy.indexOf(guess) == 0) score++;
      word_copy = word_copy.substring(1, word_copy.length());
    }
    return score*guess.length()*guess.length();
  }

  public static void main (String[] args) {
    WordMatch game1 = new WordMatch("mississippi");
    System.out.println(game1.scoreGuess("i"));
    System.out.println(game1.scoreGuess("iss"));
    System.out.println(game1.scoreGuess("issipp"));
    System.out.println(game1.scoreGuess("mississippi"));
    System.out.println();
    WordMatch game2 = new WordMatch("aaaabb");
    System.out.println(game2.scoreGuess("a"));
    System.out.println(game2.scoreGuess("aa"));
    System.out.println(game2.scoreGuess("aaa"));
    System.out.println(game2.scoreGuess("aabb"));
    System.out.println(game2.scoreGuess("c"));
  }
}

WordMatch.main(null);
4
18
36
121

4
12
18
16
0

Question 1B: Write the WordMatch method findBetterGuess which returns the better guess of its two String parameters, guess1 and guess2. If the scoreGuess method returns different values for guess1 and guess2, then the guess with the higherscore is returned. If the scoreGuess method returns the same value for guess1 andguess2, then the alphabetically greater guess is returned.

  • The prints check that the FRQ returns the correct answers
public class WordMatch {
  private String secret;

  public WordMatch (String word) {
    this.secret = word;
  }

  public int scoreGuess (String guess) {
    String word_copy = secret;
    int score = 0;
    for (int i = 0; i<secret.length(); i++) {
      if (word_copy.indexOf(guess) == 0) score++;
      word_copy = word_copy.substring(1, word_copy.length());
    }
    return score*guess.length()*guess.length();
  }

  public String findBetterGuess (String guess1, String guess2) {
    int score1 = scoreGuess(guess1);
    int score2 = scoreGuess(guess2);
    if (score1 > score2) return guess1;
    if (score1 < score2) return guess2;
    return (guess1.compareTo(guess2) > 0 ? guess1 : guess2);
  }

  public static void main (String[] args) {
    WordMatch game1 = new WordMatch("concatenation");
    System.out.println(game1.findBetterGuess("ten", "nation"));
    System.out.println(game1.findBetterGuess("con", "cat"));
  }
}

WordMatch.main(null);
nation
con