Spaces:
Running
Running
File size: 3,467 Bytes
8eecddc |
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
The program defines a Route class that implements the Comparable interface.
Write a method
public static Route longestRoute(ArrayList<Route> routes)
which returns the longest route in the list.
The method should not have side effects - the list must not be changed! Note also that the Route class does not have get methods.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
String[] c1 = "London Manchester Liverpool Leeds Sheffield Bristol Birmingham Leicester Newcastle".split(" ");
String[] c2 = "Hogwarts Camelot Avalon El Dorado Shangri-La Atlantis Utopia Eden Olympus".split(" ");
ArrayList<String> a1 = new ArrayList<>(Arrays.asList(c1));
ArrayList<String> a2 = new ArrayList<>(Arrays.asList(c2));
ArrayList<Integer> distances = new ArrayList<>();
int distance = r.nextInt(50) + 10;
for (int i = 0; i < 9; i++) {
distances.add(distance);
distance += r.nextInt(50) + 1;
}
ArrayList<Route> routes = new ArrayList<>();
for (int i = 0; i < 8; i++) {
String city1 = a1.remove(r.nextInt(a1.size()));
String city2 = a2.remove(r.nextInt(a2.size()));
routes.add(new Route(city1, city2, distances.remove(r.nextInt(distances.size()))));
}
System.out.println("All routes: ");
routes.stream().forEach(route -> System.out.println("" + route));
System.out.println("Longest route: ");
System.out.println(longestRoute(routes));
System.out.println("List now:");
routes.stream().forEach(route -> System.out.println(route));
}
//ADD
public static Route longestRoute(ArrayList<Route> routes) {
// https://www.baeldung.com/java-copy-list-to-another
// copy the list
ArrayList<Route> copy = new ArrayList<>(routes);
// sort the list
Collections.sort(copy);
// https://stackoverflow.com/questions/687833/how-to-get-the-last-value-of-an-arraylist
// return the last element
return copy.get(copy.size() - 1);
}
}
class Route implements Comparable<Route> {
private String startPoint;
private String endPoint;
private int distance;
// CONSTRUCTOR
public Route(String startPoint, String endPoint, int distance) {
this.startPoint = startPoint;
this.endPoint = endPoint;
this.distance = distance;
}
// UPDATE HOW WE USE 'compareTo' WHEN SORTING
@Override
public int compareTo(Route other) {
return this.distance - other.distance;
}
@Override
public String toString() {
return startPoint + " - " + endPoint + ": " + distance + " km.";
}
}
All routes:
Bristol - El: 108 km.
London - Shangri-La: 155 km.
Birmingham - Atlantis: 66 km.
Manchester - Eden: 240 km.
Liverpool - Olympus: 258 km.
Sheffield - Avalon: 27 km.
Leicester - Camelot: 103 km.
Newcastle - Hogwarts: 238 km.
Longest route:
Liverpool - Olympus: 258 km.
List now:
Bristol - El: 108 km.
London - Shangri-La: 155 km.
Birmingham - Atlantis: 66 km.
Manchester - Eden: 240 km.
Liverpool - Olympus: 258 km.
Sheffield - Avalon: 27 km.
Leicester - Camelot: 103 km.
Newcastle - Hogwarts: 238 km.
|