# Unit 4B - Free Response - Climbing Club B

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

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 [Past AP CSA Exams](https://www.savemyexams.com/ap/computer-science/college-board/a/past-papers/).

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

```java
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

```java
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.

```java
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](images/climbClubB.png)

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

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

> Check your understanding: What type of loop should you use to find the first place that the new peakName is less than the current element’s peakName? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/climbClubB.html#fr_climbB_1

> Check your understanding: What ArrayList method allows you to add a new element at a specific index in a list? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/climbClubB.html#fr_climbB_2

> Check your understanding: What will be stored in value after running this code:
>
> ```java
> String s1 = "Bee";
> String s2 = "Kiwi";
> boolean value = false;
> if (s1.compareTo(s2) > 0){
>     boolean value = true;
> }
> ```
>
> Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/climbClubB.html#fr_climbB_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.

### Exercise 1

FRQ Climb Club B: complete the method addClimb.

> Open the `01-ClimbClubB` folder and edit `ClimbingClub.java`. Press **Run** to try it; **Run Tests** checks it (2 tests).

## 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](https://youtu.be/Fye33yPQk-g). It walks through coding a solution.
