Spaces:
Running
Running
objList.stream().filter(obj -> obj.method() == value1).forEach(obj -> ...);
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/10B. Print North Routes
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The program assumes that we have a 'Route' class available, with the following 'get' methods needed for the task:
|
| 2 |
+
- getDirection(), which returns the direction of the route.
|
| 3 |
+
The return value is an enum of type 'CardinalDirection', with four possible values (NORTH, SOUTH, EAST, and WEST).
|
| 4 |
+
- getLength(), which returns the route's length as a floating-point number.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
Write a class 'method'
|
| 10 |
+
public static void printNorthRoutes(ArrayList<Route> routes)
|
| 11 |
+
|
| 12 |
+
which receives a list of Route objects as a parameter.
|
| 13 |
+
The method prints all the routes whose direction is north.
|
| 14 |
+
Use the Route class's toString method for printing.
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
import java.util.Random;
|
| 21 |
+
import java.util.ArrayList;
|
| 22 |
+
import java.util.Collections;
|
| 23 |
+
|
| 24 |
+
public class Test{
|
| 25 |
+
public static void main(String[] args){
|
| 26 |
+
final Random r = new Random();
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
ArrayList<Route> routes = new ArrayList<>();
|
| 30 |
+
// r.nextInt(3) => from 0 (incl) to 3 (excl)
|
| 31 |
+
int m = r.nextInt(3) + 2;
|
| 32 |
+
for (int i = 0; i < m; i++) {
|
| 33 |
+
routes.add(new Route(r.nextInt(100), CardinalDirection.NORTH));
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
m = r.nextInt(5) + 5;
|
| 37 |
+
CardinalDirection[] nonNorth = {CardinalDirection.SOUTH, CardinalDirection.EAST, CardinalDirection.WEST};
|
| 38 |
+
for (int i = 0; i < m; i++) {
|
| 39 |
+
routes.add(new Route(r.nextInt(100), nonNorth[r.nextInt(3)]));
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
Collections.shuffle(routes, r);
|
| 43 |
+
|
| 44 |
+
System.out.println("All routes:");
|
| 45 |
+
routes.stream().forEach(re -> System.out.println(re));
|
| 46 |
+
|
| 47 |
+
System.out.println("North routes:");
|
| 48 |
+
printNorthRoutes(routes);
|
| 49 |
+
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
//ADD
|
| 54 |
+
public static void printNorthRoutes(ArrayList<Route> routes) {
|
| 55 |
+
// print
|
| 56 |
+
routes.stream().filter(route -> route.getDirection() == CardinalDirection.NORTH).forEach(route -> System.out.println(route));
|
| 57 |
+
// store in a new ArrayList
|
| 58 |
+
// ArrayList<Route> northRoutes = routes.stream().filter(route -> route.getDirection() == CardinalDirection.NORTH).collect(Collectors.toCollection(ArrayList::new));
|
| 59 |
+
// return northRoutes;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
class Route {
|
| 77 |
+
private double length;
|
| 78 |
+
private CardinalDirection direction;
|
| 79 |
+
|
| 80 |
+
public Route(double length, CardinalDirection direction) {
|
| 81 |
+
this.length = length;
|
| 82 |
+
this.direction = direction;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
public double getLength() {
|
| 89 |
+
return length;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
public void setLength(double length) {
|
| 93 |
+
this.length = length;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
public CardinalDirection getDirection() {
|
| 100 |
+
return direction;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
public void setDirection(CardinalDirection direction) {
|
| 104 |
+
this.direction = direction;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
public String toString() {
|
| 111 |
+
return "Direction: " + direction + ", length: " + length;
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
enum CardinalDirection {
|
| 118 |
+
NORTH, SOUTH, EAST, WEST
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
All routes:
|
| 132 |
+
Direction: SOUTH, length: 16.0
|
| 133 |
+
Direction: NORTH, length: 77.0
|
| 134 |
+
Direction: NORTH, length: 95.0
|
| 135 |
+
Direction: WEST, length: 7.0
|
| 136 |
+
Direction: SOUTH, length: 33.0
|
| 137 |
+
Direction: NORTH, length: 96.0
|
| 138 |
+
Direction: WEST, length: 90.0
|
| 139 |
+
Direction: SOUTH, length: 29.0
|
| 140 |
+
Direction: SOUTH, length: 10.0
|
| 141 |
+
Direction: WEST, length: 7.0
|
| 142 |
+
Direction: EAST, length: 7.0
|
| 143 |
+
Direction: WEST, length: 79.0
|
| 144 |
+
Direction: NORTH, length: 19.0
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
North routes:
|
| 148 |
+
Direction: NORTH, length: 77.0
|
| 149 |
+
Direction: NORTH, length: 95.0
|
| 150 |
+
Direction: NORTH, length: 96.0
|
| 151 |
+
Direction: NORTH, length: 19.0
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|