2012 Climb Club

<< 2013 SkyView | APQuestionsTrailIndex | 2012 RetroBug >>

Click here to see the questions from 2012

ClimbInfo.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
	 */
	private String name;
	private int time;
	public ClimbInfo(String peakName, int climbTime)
	{
		name=peakName;
		time=climbTime;
	}
	/** @return the name of the mountain peak */
	public String getName()
	{ return name; }
	/** @return the number of minutes taken to complete the climb */
	public int getTime()
	{ return time; }
	// There may be instance variables, constructors, and methods that are not shown.
}

ClimbingClub.java

import java.util.ArrayList;
import java.util.List;

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 addClimbPartA(String peakName, int climbTime)
	{ 
		/* to be implemented in part (a) with ClimbInfo objects in the order they were added */ 

	}
	public void addClimbPartB(String peakName, int climbTime)
	{ 
		/* to be implemented in part (b) with ClimbInfo objects in alphabetical order by name */

	}
	/** @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;
	}
	// There may be instance variables, constructors, and methods that are not shown. 
	public String toString()
	{	
		String s1="", s2="";
		for (ClimbInfo c:climbList){
			s1=s1+String.format("[ %9s ]", c.getName());
			s2=s2+String.format("[ %9d ]", c.getTime());			
		}
		return s1+"\n"+s2;
	}
}

ClimbingClubTester.java

public class ClimbingClubTester 
{
	public static void main(String[] args)
	{
		ClimbingClub hikerClub = new ClimbingClub();
	    hikerClub.addClimbPartA("Monadnock", 274);
	    hikerClub.addClimbPartA("Whiteface", 301);
	    hikerClub.addClimbPartA("Algonquin", 225);
	    hikerClub.addClimbPartA("Monadnock", 344);
	    System.out.println("Part A - should look like");
	    System.out.println("[ Monadnock ][ Whiteface ][ Algonquin ][ Monadnock ]");
	    System.out.println("[       274 ][       301 ][       225 ][       344 ]");
	    System.out.println("Your code produces:");
	    System.out.print(hikerClub+"\ndistinctPeakNames returns ");
	    System.out.println(hikerClub.distinctPeakNames()+"\n");

	    hikerClub = new ClimbingClub();
	    hikerClub.addClimbPartB("Monadnock", 274);
	    hikerClub.addClimbPartB("Whiteface", 301);
	    hikerClub.addClimbPartB("Algonquin", 225);
	    hikerClub.addClimbPartB("Monadnock", 344);
	    System.out.println("Part B - should look like");
	    System.out.println("[ Algonquin ][ Monadnock ][ Monadnock ][ Whiteface ]");
	    System.out.println("[       225 ][       344 ][       274 ][       301 ]");
	    System.out.println("or ");
	    System.out.println("[ Algonquin ][ Monadnock ][ Monadnock ][ Whiteface ]");
	    System.out.println("[       225 ][       274 ][       344 ][       301 ]");
	    System.out.println("Your code produces:");
	    System.out.print(hikerClub+"\ndistinctPeakNames returns ");
	    System.out.println(hikerClub.distinctPeakNames()+"\n");
	}
}