Unit 2 Homework Practice FRQ 2021 (Question 1)
Part a
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.
public int scoreGuess (String guess)
{
int count = 0
for (int i = 0; i <= secret.length() - guess.length(); i++)
{
if (secret.substring(i, i + guess.length()).equals(guess))
{
count++;
}
}
return count * guess.length() * guess.length();
}
Part b
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");
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;
}
Scoring Guidelines
Part A
- compare guess to substring of secret
- Uses of substring secret with correct length for comparison with guess
- Loops through all necessary substrings of secret
- Counts number of identified occurences of guess within secret
- Calculate and returns correct final score (algorithm)
Part B
- Calls scoreGuess to get scores for guess1 and guess2
- Compares the scores
- Determines which guess1 and guess2 is alphabetically greater
- Returns the identified guess1 or guess2 (algorithm)
Takeaways
Part A
- We believe we would earn a 5/5, full credit, for this part because we completed all of the elements of the point guidlines. At first, we forgot the return statement, but we were able to problem solve and fix this. It was also hard to get the correct syntax down while we were working with new elements in Java
Part B
- Notes After some problem solving we believe we earned full credit. We found this part to be a little easier since it was simple if statements that we had learned previously.