• class: "blueprint" of attributes and methods for creating an object
  • object: instance of class with defined attributes
  • named with variables
  • two variables can refer to same object
  • signature: constructor name and parameter list

  • parameter list: lists type of values passed and their variable names (formal parameters)

  • parameter: value passed into a constructor (actual parameters), must match types specified in parameter list
  • passed using call by value
  • call by value: initializes formal parameters with copies of the actual parameters

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.

public class WordMatch
{

public int scoreGuess(String guess) //answer to 1a
{   
    int count = 0; //starts off the count of substring occurrences at 0
    
    for (int i = 0; i <= secret.length(); i++) { //assuming that length of guess is less than or equal to secret.length
        if (secret.substring(i, guess.length().equals(guess)); //checks if guess is within substring of secret
        count++;
    } //then increases the count
}

return count * guess.length() * guess.length(); //numbers of times guess is found within substring and multiplies it by square of guess 
}

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.

public class WordMatch
{
public String findBetterGuess(String guess1, String guess2)
{
    if (scoreGuess(guess1)>scoreGuess(guess2)) //if guess for guess1 is greater then guess1 is returned
    {
        return guess1;
    }
    if (scoreGuess(guess2)>scoreGuess(guess1)) //otherwise guess2 is returned
    {
        return guess2; 
    }
}