The ClimbingClub
problem from the 2012 AP Computer Science Exam is typical of free response problems that test lists.
ClimbingClub
is #1 from the 2012 AP Computer Science A Free Response.
https://secure-media.collegeboard.org/apc/ap_frq_computerscience_12.pdf
Part (a) addClimb
method in order run
public void addClimb(String peakName, int climbTime)
{
climbList.add(new ClimbInfo(peakName, climbTime));
}
Part (b) addClimb
in alphabetical order
public void addClimb(String peakName, int climbTime)
{
int i = 0;
while(i < climbList.size() &&
peakName.compareTo(climbList.get(i).getName()) > 0)
i++;
climbList.add(i, new ClimbInfo(peakName, climbTime));
}
The approach above is a standard algorithm for insertion into a sorted list. See Insertion into a sorted list for an explanation of the algorithm and additional resources.
Part (c): distinctPeakNames
method
(i) No
(ii) Yes
The implementation checks adjacent elements to identify distinct peak names. The implementation does not actually require the list to be sorted but it does require that all climbs with the same peak name be adjacent to each other.
2012 AP CS Exam Free Response Solutions
Help & comments
Get help from AP CS Tutor Brandon Horn