Part 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.

WordMatch game = new WordMatch("mississippi");

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 described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
// public int scoreGuess(String guess)
public int scoreGuess(String guess)
{
    int result = 0;

    for (int i = 0; i < secret.length(); i++)
    {
        if(secret.substring(i).indexOf(guess) == 0)
        {
            result++; 
        }
    }

    return result * guess.length() * guess.length();
}
// { /* to be implemented in part (a) */ }

Part b) 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 higher score is returned. If the scoreGuess method returns the same value for guess1 and guess2, then the alphabetically greater guess is returned.

The following example shows a declaration of a WordMatch object and the outcomes of some possible calls to the scoreGuess and findBetterGuess methods.

WordMatch game = new WordMatch("concatenation");

/** Returns the better of two guesses, as determined by scoreGuess and the rules for a
* tie-breaker that are described in part (b).
* Precondition: guess1 and guess2 contain all lowercase letters.
* guess1 is not the same as guess2.
*/
public String findBetterGuess(String guess1, String guess2)
{
    if(scoreGuess(guess1) > scoreGuess(guess2))
    {
        return guess1; 
    } 

    if(scoreGuess(guess2) > scoreGuess(guess1))
    {
        return guess2; 
    }
    
    if(guess1.compareTo(guess2) > 0 )
    {
        return guess1;
    }
    
    return guess2; 

}

Code As A Whole

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 described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess(String guess)
{
    int result = 0;

    for (int i = 0; a < secret.length(); i++)
    {
        if(secret.substring(i).indexOf(guess) == 0)
        {
            result++; 
        }
    }

    return result * guess.length() * guess.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-breaker that are described in part (b).
* Precondition: guess1 and guess2 contain all lowercase letters.
* guess1 is not the same as guess2.
*/
public String findBetterGuess(String guess1, String guess2)
{
    if(scoreGuess(guess1) > scoreGuess(guess2))
    {
        return guess1; 
    } 

    if(scoreGuess(guess2) > scoreGuess(guess1))
    {
        return guess2; 
    }
    
    if(guess1.compareTo(guess2) > 0 )
    {
        return guess1;
    }
    
    return guess2; 
    }
}