(a) Write the WordMatch method scoreGuess. To determine the score to be returned, scoreGuess finds the number of times that guess occurs as a substring of secret and then multiplies that number by the square of the length of guess. Occurrences of guess may overlap within secret.

Assume that the length of guess is less than or equal to the length of secret and that guess is not an empty string. The following examples show declarations of a WordMatch object. The tables show the outcomes of some possible calls to the scoreGuess method.

Complete a ScoreGuess Method:

public class WordMatch {
    private String secret;

    public WordMatch(String word) {
        secret = word;
    }
    public int scoreGuess(String guess) {
        int guessLength = guess.length(); //Will be used to calculate score later

        //Count occurrences of substring
        int count = 0;
        for (int i = 0; i < secret.length() - guessLength + 1; i++) {
            if (secret.substring(i, i + guessLength).equals(guess)) {
                count++;
            }
        }

        return count * guessLength * guessLength; //Calculate and return score
    }
}

WordMatch wordMatch = new WordMatch("mississippi");
System.out.println(wordMatch.scoreGuess("i"));
4