-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.java
More file actions
86 lines (75 loc) · 2.08 KB
/
practice.java
File metadata and controls
86 lines (75 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.List;
import java.util.ArrayList;
class ClimbInfo
{
private String name;
private int time;
/** 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)
{
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;
}
public String toString()
{
return "Peak name: " + name + " time: " + time;
}
}
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 end of 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)
{
int i = 0;
while (i < climbList.size() && climbList.get(i).getName().compareTo(peakName) < 0){
i++;
}
climbList.add(i, new ClimbInfo(peakName,climbTime));
}
public String toString()
{
String output ="";
for (ClimbInfo info : climbList)
{
output = output + info.toString() + "\n";
}
return output;
}
public static void main(String[] args)
{
// test a
ClimbingClub hikerClub = new ClimbingClub();
hikerClub.addClimb("Monadnock", 274);
hikerClub.addClimb("Whiteface", 301);
hikerClub.addClimb("Algonquin", 225);
hikerClub.addClimb("Monadnock", 344);
System.out.print(hikerClub);
System.out.println("The order printed above should be Algonquin, Monadnock, Monadnock, Whiteface");
}
}