KaiquanMah commited on
Commit
8eecddc
·
verified ·
1 Parent(s): 79054f0

@Override public int compareTo(Route other)

Browse files
Week 6: Methods of OO Programming/07B. Determine the Longest Route ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The program defines a Route class that implements the Comparable interface.
2
+
3
+ Write a method
4
+ public static Route longestRoute(ArrayList<Route> routes)
5
+
6
+ which returns the longest route in the list.
7
+ The method should not have side effects - the list must not be changed! Note also that the Route class does not have get methods.
8
+
9
+
10
+
11
+
12
+
13
+
14
+ import java.util.ArrayList;
15
+ import java.util.Arrays;
16
+ import java.util.Collections;
17
+ import java.util.Random;
18
+
19
+ public class Test{
20
+ public static void main(String[] args){
21
+ final Random r = new Random();
22
+
23
+
24
+ String[] c1 = "London Manchester Liverpool Leeds Sheffield Bristol Birmingham Leicester Newcastle".split(" ");
25
+ String[] c2 = "Hogwarts Camelot Avalon El Dorado Shangri-La Atlantis Utopia Eden Olympus".split(" ");
26
+
27
+ ArrayList<String> a1 = new ArrayList<>(Arrays.asList(c1));
28
+ ArrayList<String> a2 = new ArrayList<>(Arrays.asList(c2));
29
+
30
+ ArrayList<Integer> distances = new ArrayList<>();
31
+ int distance = r.nextInt(50) + 10;
32
+ for (int i = 0; i < 9; i++) {
33
+ distances.add(distance);
34
+ distance += r.nextInt(50) + 1;
35
+ }
36
+
37
+ ArrayList<Route> routes = new ArrayList<>();
38
+ for (int i = 0; i < 8; i++) {
39
+ String city1 = a1.remove(r.nextInt(a1.size()));
40
+ String city2 = a2.remove(r.nextInt(a2.size()));
41
+ routes.add(new Route(city1, city2, distances.remove(r.nextInt(distances.size()))));
42
+ }
43
+
44
+ System.out.println("All routes: ");
45
+ routes.stream().forEach(route -> System.out.println("" + route));
46
+ System.out.println("Longest route: ");
47
+ System.out.println(longestRoute(routes));
48
+ System.out.println("List now:");
49
+ routes.stream().forEach(route -> System.out.println(route));
50
+ }
51
+
52
+
53
+ //ADD
54
+ public static Route longestRoute(ArrayList<Route> routes) {
55
+ // https://www.baeldung.com/java-copy-list-to-another
56
+ // copy the list
57
+ ArrayList<Route> copy = new ArrayList<>(routes);
58
+
59
+ // sort the list
60
+ Collections.sort(copy);
61
+
62
+ // https://stackoverflow.com/questions/687833/how-to-get-the-last-value-of-an-arraylist
63
+ // return the last element
64
+ return copy.get(copy.size() - 1);
65
+ }
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+ }
77
+
78
+
79
+
80
+
81
+
82
+
83
+ class Route implements Comparable<Route> {
84
+ private String startPoint;
85
+ private String endPoint;
86
+ private int distance;
87
+
88
+ // CONSTRUCTOR
89
+ public Route(String startPoint, String endPoint, int distance) {
90
+ this.startPoint = startPoint;
91
+ this.endPoint = endPoint;
92
+ this.distance = distance;
93
+ }
94
+
95
+ // UPDATE HOW WE USE 'compareTo' WHEN SORTING
96
+ @Override
97
+ public int compareTo(Route other) {
98
+ return this.distance - other.distance;
99
+ }
100
+
101
+ @Override
102
+ public String toString() {
103
+ return startPoint + " - " + endPoint + ": " + distance + " km.";
104
+ }
105
+ }
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+ All routes:
115
+ Bristol - El: 108 km.
116
+ London - Shangri-La: 155 km.
117
+ Birmingham - Atlantis: 66 km.
118
+ Manchester - Eden: 240 km.
119
+ Liverpool - Olympus: 258 km.
120
+ Sheffield - Avalon: 27 km.
121
+ Leicester - Camelot: 103 km.
122
+ Newcastle - Hogwarts: 238 km.
123
+
124
+ Longest route:
125
+ Liverpool - Olympus: 258 km.
126
+
127
+ List now:
128
+ Bristol - El: 108 km.
129
+ London - Shangri-La: 155 km.
130
+ Birmingham - Atlantis: 66 km.
131
+ Manchester - Eden: 240 km.
132
+ Liverpool - Olympus: 258 km.
133
+ Sheffield - Avalon: 27 km.
134
+ Leicester - Camelot: 103 km.
135
+ Newcastle - Hogwarts: 238 km.
136
+
137
+
138
+
139
+