WordPairList
is #2 from the from the 2018 AP Computer Science A Free Response problems.
https://secure-media.collegeboard.org/ap/pdf/ap18-frq-computer-science-a.pdf
Part (a) WordPairList
constructor
public WordPairList(String[] words)
{
allPairs = new ArrayList<WordPair>();
for(int i = 0; i < words.length; i++)
for(int j = i + 1; j < words.length; j++)
allPairs.add(new WordPair(words[i], words[j]));
}
It is very common for students to miss the first line of this constructor. Adding to a list (or setting elements in an array) is not the same thing as initializing the variable.
Part (b) numMaches
method
public int numMatches()
{
int matches = 0;
for(WordPair wp : allPairs)
if(wp.getFirst().equals(wp.getSecond()))
matches++;
return matches;
}
2018 AP CS Exam Free Response Solutions
- FrogSimulation Free Response Solution
- CodeWordChecker Free Response Solution
- ArrayTester Free Response Solution
Additional ArrayList
resources
Help & comments
Get help from AP CS Tutor Brandon Horn