AP CSA 2021 Q1 - WordMatch Class
<-- Back to AP CSA Exam Solutions | Next to Solution of Q2 (CombinedTable) - FRQ - 2021 --> |
Solution of Q1 (WordMatch) - Free Response Question - 2021
The original question can be found at: https://apcentral.collegeboard.org/media/pdf/ap21-frq-computer-science-a.pdf
Part (a)- public int scoreGuess(String guess)
public int scoreGuess(String guess) { int count=0; int lenGuess = guess.length(); for (int i=0; i<= secret.length() - lenGuess ; i++) { if ((secret.substring(i, i+lenGuess).equals(guess))) { //match found count ++; } } return count * lenGuess * lenGuess; }
Part (b)- public String findBetterGuess(String guess1, String guess2)
public String findBetterGuess(String guess1, String guess2) { int count1=0, count2=0; count1 = scoreGuess(guess1); count2 = scoreGuess(guess2); if (count1 > count2 ) return guess1; else if (count1 < count2) return guess2; else if (count1 == count2) { //return the greater string; if (guess1.compareTo(guess2) > 0) return guess1; else return guess2; } return guess2; }
Java project files (with Runner code):
<-- Back to AP CSA Exam Solutions | Next to Solution of Q2 (CombinedTable) - FRQ - 2021 --> |