8.4.2. Free Response - Climbing Club A¶
The following is part a 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 a. Write an implementation of the ClimbingClub
method addClimb
that stores the ClimbInfo
objects in the order they were added. This implementation of addClimb
should create a new
ClimbInfo
object with the given name and time. It appends a reference to that object to the end of
climbList. 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 executing, the instance variable climbList
would contain the
following entries.
![Picture of the list after the above code executes](../_images/climbClubA.png)
8.4.2.1. How To Solve This¶
Click to reveal the algorithm and practice problems to help you write your solution.
In the addClimb
method you need to create a new ClimbInfo
object and initialize the peakName
and climbTime
. How do you create a new object of a class and initialize the fields?
Once you have created the ClimbInfo
object you want to add it in the order they were created. To do this you can add it to the end of the climbList
. How do you add an object to the end of a list?
- ClimbInfo newClimb = new ClimbInfo("Everest", 600);
- Correct!
- new ClimbInfo("Everest", 600);
- This answer is missing the instantiation of the variable newClimb. Try again!
- ClimbInfo newClimb = new ClimbInfo();
- The constructor of a ClimbInfo object requires two arguments. Try again!
7-4-2-1: How would you create a new object newClimb of the ClimbInfo class, with a peakName of Everest and climbTime of 600?
- list.add(0, 7);
- This would add 7 to the beginning of the list. Try again!
- list.add(7);
- Correct!
- add(7);
- You must reference the ArrayList list using a dot operator to use the add() method. Try again!
7-4-2-2: How do you append a new item, 7, to the end of a non-empty ArrayList<Integer> list?
8.4.2.2. Try and Solve It¶
Complete the method addClimb
in the ClimbingClub
class in the code below. The code includes a main
method that will test the addClimb
method.
8.4.2.3. 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/dAbU9_Qn92I. It walks through coding a solution.