Unit 4B - Free Response - Climbing Club C
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period1 exerciseNot autograded
Files
Unit 4B - Free Response - Climbing Club C
Each coding exercise has its own folder. Open its .java file and press Run to try it.
The following is part c 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.
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 c. The ClimbingClub method distinctPeakNames is intended to return the number of different names in climbList. For example, after the following code segment has completed execution, the value of the variable numNames would be 3.
ClimbingClub hikerClub = new ClimbingClub();
hikerClub.addClimb("Monadnock", 274);
hikerClub.addClimb("Whiteface", 301);
hikerClub.addClimb("Algonquin", 225);
hikerClub.addClimb("Monadnock", 344);
Consider the following implementation of method distinctPeakNames.
/** @return the number of distinct names in the list of climbs */
public int distinctPeakNames()
{
if (climbList.size() == 0)
{
return 0;
}
ClimbInfo currInfo = climbList.get(0);
String prevName = currInfo.getName();
String currName = null;
int numNames = 1;
for (int k = 1; k < climbList.size(); k++)
{
currInfo = climbList.get(k);
currName = currInfo.getName();
if (prevName.compareTo(currName) != 0)
{
numNames++;
prevName = currName;
}
}
return numNames;
}
Check your understanding: Does this implementation of the
distinctPeakNamesmethod work as intended when theaddClimbmethod stores theClimbInfoobjects in the order they were added as described in part (a)? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/climbClubC.html#frqccc_1
Check your understanding: Does this implementation of the
distinctPeakNamesmethod work as intended when theaddClimbmethod stores theClimbInfoobjects in alphabetical order by name as described in part (b)? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/climbClubC.html#frqccc_2
Try it Out
Exercise 1
FRQ ClimbClub C: Try the code.
Open the
01-ClimbClubCfolder and runClimbingClub.java. It is an example, so it has no tests.
For teachers
Unit 4B - Free Response - Climbing Club C
Students read CSAwesome2's “Unit 4B - Free Response - Climbing Club C” in CS Room and work through its 1 Java coding exercise (examples, not graded).
- Time: 1 class period
- Tests: none
Goals
- Practice for Unit 4: Data Collections.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft.
- Publish it.
- The original page, with its multiple-choice questions, is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/climbClubC.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. Questions that need CS Awesome's interactive widgets link back to it. - Students run the example in exercise 01 in its folder.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-ClimbClubC | ClimbingClub.java | Example, not graded | 0 |