Spaces:
Running
Running
TurkuBasicOOPinJava / Week 7: Enum, Generic Type, Streams, write to file, class diagram /10B. Print North Routes
| 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) { | |
| 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 | |