Spaces:
Running
Running
| The program has defined an enum for the direction as introduced in the previous exercise. | |
| Write a class Route with the following features: | |
| Constructor, which receives the length (floating-point number) and direction (direction-enum) as parameters. | |
| Get and Set methods for length and direction. | |
| import java.util.Random; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| final Random r = new Random(); | |
| System.out.println("Testing the class Route..."); | |
| double length = r.nextInt(200) + 10; | |
| CardinalDirection direction = CardinalDirection.values()[r.nextInt(4)]; | |
| System.out.println("Creating with values (" + length + ", " + direction + ")"); | |
| Route r1 = new Route(length, direction); | |
| System.out.println("Object created!"); | |
| System.out.println("Length: " + r1.getLength()); | |
| System.out.println("Direction: " + r1.getDirection()); | |
| System.out.println("Testing observation..."); | |
| for (int test = 1; test <= 3; test++) { | |
| length = r.nextInt(200) + 10; | |
| direction = CardinalDirection.values()[r.nextInt(4)]; | |
| System.out.println("Setting length to " + length); | |
| r1.setLength(length); | |
| System.out.println("Length: " + r1.getLength()); | |
| System.out.println("Setting direction to " + direction); | |
| r1.setDirection(direction); | |
| System.out.println("Direction: " + r1.getDirection()); | |
| } | |
| } | |
| } | |
| //enum defined on the backend, in an earlier qn | |
| //enum CardinalDirection { | |
| // // north, south, east, west | |
| // NORTH, SOUTH, EAST, WEST | |
| //} | |
| //ADD | |
| class Route { | |
| private double length; | |
| private CardinalDirection direction; | |
| //constructor | |
| public Route(double length, CardinalDirection direction) { | |
| this.length = length; | |
| this.direction = direction; | |
| } | |
| public double getLength() { | |
| return this.length; | |
| } | |
| public void setLength(double length) { | |
| this.length = length; | |
| } | |
| public CardinalDirection getDirection() { | |
| return this.direction; | |
| } | |
| public void setDirection(CardinalDirection direction) { | |
| this.direction = direction; | |
| } | |
| } | |
| Testing the class Route... | |
| Creating with values (80.0, WEST) | |
| Object created! | |
| Length: 80.0 | |
| Direction: WEST | |
| Testing observation... | |
| Setting length to 189.0 | |
| Length: 189.0 | |
| Setting direction to SOUTH | |
| Direction: SOUTH | |
| Setting length to 155.0 | |
| Length: 155.0 | |
| Setting direction to SOUTH | |
| Direction: SOUTH | |
| Setting length to 24.0 | |
| Length: 24.0 | |
| Setting direction to WEST | |
| Direction: WEST | |