7.4.3. 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.

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.

Picture of the list after the above code executes

7.4.3.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.

7.4.3.2. How To Solve This

Click to reveal the algorithm and problems to help you write your solution.

7.4.3.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.

FRQ Climb Club B: complete the method addClimb below.

7.4.3.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. It walks through coding a solution.

You have attempted of activities on this page