TurkuBasicOOPinJava / Week 6: Methods of OO Programming /07B. Determine the Longest Route
KaiquanMah's picture
@Override public int compareTo(Route other)
8eecddc verified
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.