Spaces:
Running
Running
route.getDirection() == direction
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/03. Routes in the Given Direction
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The program has defined the Route class written in the previous exercise.
|
| 2 |
+
|
| 3 |
+
Write a method:
|
| 4 |
+
|
| 5 |
+
public static ArrayList<Route> routesInDirection(ArrayList<Route> routes, CardinalDirection direction)
|
| 6 |
+
which receives a list of route objects and a direction as parameters.
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
The method returns a new list containing those routes that go in the given direction.
|
| 10 |
+
The ORDER of the routes in the new list should be the SAME as in the list provided as a parameter.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
import java.util.ArrayList;
|
| 16 |
+
import java.util.Random;
|
| 17 |
+
|
| 18 |
+
public class Test{
|
| 19 |
+
public static void main(String[] args){
|
| 20 |
+
final Random r = new Random();
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
ArrayList<Route> al = new ArrayList<>();
|
| 24 |
+
int count = r.nextInt(5) + 10;
|
| 25 |
+
|
| 26 |
+
for (int i = 0; i < count; i++) {
|
| 27 |
+
double length = r.nextInt(200) + 10;
|
| 28 |
+
CardinalDirection direction = CardinalDirection.values()[r.nextInt(4)];
|
| 29 |
+
al.add(new Route(length, direction));
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
System.out.println("All routes:");
|
| 33 |
+
al.stream().forEach(rt -> System.out.println("" + rt));
|
| 34 |
+
|
| 35 |
+
for (CardinalDirection direction : CardinalDirection.values()) {
|
| 36 |
+
System.out.println("");
|
| 37 |
+
System.out.println("Routes in the direction " + direction);
|
| 38 |
+
|
| 39 |
+
ArrayList<Route> al2 = routesInDirection(al, direction);
|
| 40 |
+
al2.stream().forEach(rt -> System.out.println("" + rt));
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
//add
|
| 47 |
+
public static ArrayList<Route> routesInDirection(ArrayList<Route> routes, CardinalDirection direction) {
|
| 48 |
+
// initialise empty ArrayList
|
| 49 |
+
// approach 1 - works
|
| 50 |
+
ArrayList<Route> filteredRoutes = new ArrayList<Route>();
|
| 51 |
+
// approach 2 - works
|
| 52 |
+
// in Java 7+
|
| 53 |
+
// ArrayList<Route> filteredRoutes = new ArrayList<>();
|
| 54 |
+
|
| 55 |
+
for (Route route: routes) {
|
| 56 |
+
if (route.getDirection() == direction) {
|
| 57 |
+
filteredRoutes.add(route);
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
return filteredRoutes;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
All routes:
|
| 79 |
+
Direction: NORTH, length: 125.0
|
| 80 |
+
Direction: WEST, length: 160.0
|
| 81 |
+
Direction: NORTH, length: 114.0
|
| 82 |
+
Direction: NORTH, length: 196.0
|
| 83 |
+
Direction: NORTH, length: 167.0
|
| 84 |
+
Direction: NORTH, length: 169.0
|
| 85 |
+
Direction: WEST, length: 120.0
|
| 86 |
+
Direction: EAST, length: 124.0
|
| 87 |
+
Direction: WEST, length: 34.0
|
| 88 |
+
Direction: NORTH, length: 173.0
|
| 89 |
+
Direction: EAST, length: 107.0
|
| 90 |
+
Direction: WEST, length: 167.0
|
| 91 |
+
Direction: NORTH, length: 95.0
|
| 92 |
+
|
| 93 |
+
Routes in the direction NORTH
|
| 94 |
+
Direction: NORTH, length: 125.0
|
| 95 |
+
Direction: NORTH, length: 114.0
|
| 96 |
+
Direction: NORTH, length: 196.0
|
| 97 |
+
Direction: NORTH, length: 167.0
|
| 98 |
+
Direction: NORTH, length: 169.0
|
| 99 |
+
Direction: NORTH, length: 173.0
|
| 100 |
+
Direction: NORTH, length: 95.0
|
| 101 |
+
|
| 102 |
+
Routes in the direction SOUTH
|
| 103 |
+
|
| 104 |
+
Routes in the direction EAST
|
| 105 |
+
Direction: EAST, length: 124.0
|
| 106 |
+
Direction: EAST, length: 107.0
|
| 107 |
+
|
| 108 |
+
Routes in the direction WEST
|
| 109 |
+
Direction: WEST, length: 160.0
|
| 110 |
+
Direction: WEST, length: 120.0
|
| 111 |
+
Direction: WEST, length: 34.0
|
| 112 |
+
Direction: WEST, length: 167.0
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|