Spaces:
Running
Running
File size: 3,421 Bytes
03741f4 | 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | The program assumes that we have a 'Route' class available, with the following 'get' methods needed for the task:
- getDirection(), which returns the direction of the route.
The return value is an enum of type 'CardinalDirection', with four possible values (NORTH, SOUTH, EAST, and WEST).
- getLength(), which returns the route's length as a floating-point number.
Write a class 'method'
public static void printNorthRoutes(ArrayList<Route> routes)
which receives a list of Route objects as a parameter.
The method prints all the routes whose direction is north.
Use the Route class's toString method for printing.
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
public class Test{
public static void main(String[] args){
final Random r = new Random();
ArrayList<Route> routes = new ArrayList<>();
// r.nextInt(3) => from 0 (incl) to 3 (excl)
int m = r.nextInt(3) + 2;
for (int i = 0; i < m; i++) {
routes.add(new Route(r.nextInt(100), CardinalDirection.NORTH));
}
m = r.nextInt(5) + 5;
CardinalDirection[] nonNorth = {CardinalDirection.SOUTH, CardinalDirection.EAST, CardinalDirection.WEST};
for (int i = 0; i < m; i++) {
routes.add(new Route(r.nextInt(100), nonNorth[r.nextInt(3)]));
}
Collections.shuffle(routes, r);
System.out.println("All routes:");
routes.stream().forEach(re -> System.out.println(re));
System.out.println("North routes:");
printNorthRoutes(routes);
}
//ADD
public static void printNorthRoutes(ArrayList<Route> routes) {
// print
routes.stream().filter(route -> route.getDirection() == CardinalDirection.NORTH).forEach(route -> System.out.println(route));
// store in a new ArrayList
// ArrayList<Route> northRoutes = routes.stream().filter(route -> route.getDirection() == CardinalDirection.NORTH).collect(Collectors.toCollection(ArrayList::new));
// return northRoutes;
}
}
class Route {
private double length;
private CardinalDirection direction;
public Route(double length, CardinalDirection direction) {
this.length = length;
this.direction = direction;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public CardinalDirection getDirection() {
return direction;
}
public void setDirection(CardinalDirection direction) {
this.direction = direction;
}
public String toString() {
return "Direction: " + direction + ", length: " + length;
}
}
enum CardinalDirection {
NORTH, SOUTH, EAST, WEST
}
All routes:
Direction: SOUTH, length: 16.0
Direction: NORTH, length: 77.0
Direction: NORTH, length: 95.0
Direction: WEST, length: 7.0
Direction: SOUTH, length: 33.0
Direction: NORTH, length: 96.0
Direction: WEST, length: 90.0
Direction: SOUTH, length: 29.0
Direction: SOUTH, length: 10.0
Direction: WEST, length: 7.0
Direction: EAST, length: 7.0
Direction: WEST, length: 79.0
Direction: NORTH, length: 19.0
North routes:
Direction: NORTH, length: 77.0
Direction: NORTH, length: 95.0
Direction: NORTH, length: 96.0
Direction: NORTH, length: 19.0
|