StudentAnswerSheet
is #3 from the from the 2007 AP Computer Science A Free Response problems.
Part (a) getScore
method
public double getScore(ArrayList<String> key)
{
double score = 0;
for(int i = 0; i < key.size(); i++)
{
if(answers.get(i).equals(key.get(i)))
score += 1;
else if( ! answers.get(i).equals("?") )
score -= 0.25;
}
return score;
}
Part (b) highestScoringStudent
method
public String highestScoringStudent(ArrayList<String> key)
{
int maxIndex = 0;
for(int i = 1; i < sheets.size(); i++)
{
if(sheets.get(i).getScore(key) >
sheets.get(maxIndex).getScore(key))
{
maxIndex = i;
}
}
return sheets.get(maxIndex).getName();
}
This is a standard find the max problem. The code above follows the advice at finding the minimum or maximum and starts maxIndex
at the index of the first value that could be the maximum. The loop starts at 1
, since there is no need to compare the sheet at 0
to itself.
2007 AP CS Exam Free Response Solutions
Help & comments
Get help from AP CS Tutor Brandon Horn