Skip to main content
Logo image

Section 4.37 Free Response - Climbing Club B

The following is part b of a free response question from 2012. It was question 1 on the exam. You can see all the free response questions from past exams at https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
 1 
https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year
.
Question 1. A mountain climbing club maintains a record of the climbs that its members have made. Information about a climb includes the name of the mountain peak and the amount of time it took to reach the top. The information is contained in the ClimbInfo class as declared below.
public class ClimbInfo
{
    /**
     * Creates a ClimbInfo object with name peakName and time climbTime.
     *
     * @param peakName the name of the mountain peak
     * @param climbTime the number of minutes taken to complete the climb
     */
    public ClimbInfo(String peakName, int climbTime)
    {
        /* implementation not shown */
    }

    /**
     * @return the name of the mountain peak
     */
    public String getName()
    {
        /* implementation not shown */
    }

    /**
     * @return the number of minutes taken to complete the climb
     */
    public int getTime()
    {
        /* implementation not shown */
    }

    // There may be instance variables, constructors, and methods
    // that are not shown.
}
The ClimbingClub class maintains a list of the climbs made by members of the club. The declaration of the ClimbingClub class is shown below. You will write two different implementations of the addClimb method. You will also answer two questions about an implementation of the distinctPeakNames method
public class ClimbingClub
{
    /**
     * The list of climbs completed by members of the club. Guaranteed not to be
     * null. Contains only non-null references.
     */
    private List<ClimbInfo> climbList;

    /** Creates a new ClimbingClub object. */
    public ClimbingClub()
    {
        climbList = new ArrayList<ClimbInfo>();
    }

    /**
     * Adds a new climb with name peakName and time climbTime to the list of
     * climbs.
     *
     * @param peakName the name of the mountain peak climbed
     * @param climbTime the number of minutes taken to complete the climb
     */
    public void addClimb(String peakName, int climbTime)
    {
        /* to be implemented in part (a) */
    }

    /**
     * @return the number of distinct names in the list of climbs
     */
    public int distinctPeakNames()
    {
        /* implementation shown in part (c) */
    }

    // There may be instance variables, constructors, and methods
    // that are not shown.
}
Part b. Write an implementation of the ClimbingClub method addClimb that stores the elements of climbList in alphabetical order by name (as determined by the compareTo method of the String class). This implementation of addClimb should create a new ClimbInfo object with the given name and time and then insert the object into the appropriate position in climbList. Entries that have the same name will be grouped together and can appear in any order within the group. For example, consider the following code segment.
ClimbingClub hikerClub = new ClimbingClub();
hikerClub.addClimb("Monadnock", 274);
hikerClub.addClimb("Whiteface", 301);
hikerClub.addClimb("Algonquin", 225);
hikerClub.addClimb("Monadnock", 344);
When the code segment has completed execution, the instance variable climbList would contain the following entries in either of the orders shown below.

Subsection 4.37.1 Walk Through the Example

  1. First you will create a new ClimbInfo object with a peakName of Monadnock and a climbTime of 274 and insert it in the empty climbList.
  2. Next you will create a new ClimbInfo object with a peakName of Whiteface and a climbTime of 301. You will compare the peakName of Whiteface to Monadnock and since it is greater you will try to continue but you will have reached the end of the climbList so you will insert it there.
  3. Next you will create a new ClimbInfo object with a peakName of Algonquin and a climbTime of 225. You will compare Algonquin to Monadnock and since Algonquin is less than Monadnock you will insert it at position 0.
  4. Next you will create a new ClimbInfo object with a peakName of Monadnock and a climbTime of 334. You will compare Monadnock to Algonquin and since it is greater you will continue. You will next check Monadnock to Monadnock and since they are equal you can insert it there.

Subsection 4.37.2 How To Solve This

Loop through the elements of climbList until you find the index where the new peakName is less than the peakName of the ClimbInfo object at the current index. Insert the new ClimbInfo object there.

Activity 4.37.1.

What type of loop should you use to find the first place that the new peakName is less than the current element’s peakName?
  • while
  • Correct! While loops are perfect when you don’t always need to loop through the whole list.
  • Try again. For loops are typically used when it is necessary to access every element in a list.
  • for-each
  • Try again. For each loops automatically loop through every element in a list, but this problem doesn’t require that.

Activity 4.37.2.

What ArrayList method allows you to add a new element at a specific index in a list?
  • add()
  • There are two versions of the add method for ArrayLists. Both require at least one argument.
  • add(ClimbInfo elmt)
  • This will add the element to the end of the list rather than at a specific index.
  • add(int i, ClimbInfo elmt)
  • Correct! This will add elmt at the ith index in your list.

Activity 4.37.3.

What will be stored in value after running this code:
String s1 = "Bee";
String s2 = "Kiwi";
boolean value = false;
if (s1.compareTo(s2) > 0){
    boolean value = true;
}
  • s1 starts with "B", so it is less than s1, which starts with "K". This means compareTo would return a negative number, not a positive number.
  • false
  • Correct! "Bee" is less than "Kiwi", so value would be false after running this code.

Subsection 4.37.3 Try and Solve It

Complete the method addClimb in the ClimbingClub class in the code below. It should create a new ClimbInfo object and insert it in alphabetical order by peakName in the climbList. The code includes a main method that will test the addClimb method.

Activity 4.37.4.

FRQ Climb Club B: complete the method addClimb below.

Subsection 4.37.4 Video - One way to code the solution

There are many possible solutions to this problem. The video below shows one solution.
The following video is also on YouTube at https://youtu.be/Fye33yPQk-g
 2 
https://youtu.be/Fye33yPQk-g
. It walks through coding a solution.
You have attempted of activities on this page.