Climb Solution

The Climb Info free response question from the 2012 AP Computer Science exam was a bit of an oddball. Parts A and B had you implementing the same method in two different ways. One unsorted and one sorted. And part C was two true false questions about the implementations you wrote for A and B.

For this you’re working with a list of an arbitrary class, a pretty common setup for FRQs. This time you need the following two class stubs.

public class ClimbInfo {
   public ClimbInfo(String peakName, int climbTime) {}
   public String getName() {}
   public int getTime() {}
public class ClimbingClub {
   private List<ClimbInfo> climbList;
   public void addClimb(String peakName, int climbTime) {}
   public int distinctPeakNames() {}

For both parts A and B you’re implementing the addClimb method.

Part A

First time through your code should implement the addClimb method in such a way that a new climb for peakName taking climbTime is added to the end of climbList.

For most of my students the hardest part of this one is that it was a really simple solution.

public void addClimb( String peakName, int climbTime ) {
    climbList.add(new ClimbInfo(peakName, climbTime)); 

You did need to understand that you’re creating a new ClimbInfo reference and adding that to the list. If you’d rather, you can also create a new variable before adding it to the list.

public void addClimb( String peakName, int climbTime ) { 
    ClimbInfo newInfo = new ClimbInfo(peakName, climbTime); 

Part B

The second time around you’ll need to implement addClimb in such a way that the climbs stay sorted by the peak name.

The solution I came up with that is closest to what I would expect a first year student to do is this.

public void addClimb( String peakName, int climbTime ) {
    int i = 0;
    while (i < climbList.size() && climbList.get(i).getName().compareTo(peakName)<=0) {
    climbList.add(i, new ClimbInfo(peakName, climbTime)); 

Starting at 0 it loops until it either gets to the end or the compareTo method is positive, which means that the new ClimbInfo should insert before.

At the end it inserts the new ClimbInfo at i so that it’s in the right spot.

This solution makes more sense in my mind, but it uses the break command which is not part of the AP Java subset.

public void addClimb( String peakName, int climbTime ) {
    int i=0; 
    for (i=0; i<climbList.size(); i++) {
        if (climbList.get(i).getName().compareTo(peakName) > 0) {
    climbList.add(i, new ClimbInfo(peakName, climbTime));

Part C

This was the weird one. Instead of writing code you’re answering two true false questions based on an implementation of the distinctPeakNames method. You can see their implementation on the College Board website.

The gist of the question is that the implementation shown only works if the list is sorted. So it does not work correctly for part A but does for part B.

This site contains affiliate links. If you click an affiliate link and make a purchase we may get a small commission. It doesn't affect the price you pay, but it is something we must disclose.