Spaces:
Running
Running
Create 02. Class Route
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/02. Class Route
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The program has defined an enum for the direction as introduced in the previous exercise.
|
| 2 |
+
|
| 3 |
+
Write a class Route with the following features:
|
| 4 |
+
Constructor, which receives the length (floating-point number) and direction (direction-enum) as parameters.
|
| 5 |
+
Get and Set methods for length and direction.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
import java.util.Random;
|
| 12 |
+
|
| 13 |
+
public class Test{
|
| 14 |
+
public static void main(String[] args){
|
| 15 |
+
final Random r = new Random();
|
| 16 |
+
|
| 17 |
+
System.out.println("Testing the class Route...");
|
| 18 |
+
|
| 19 |
+
double length = r.nextInt(200) + 10;
|
| 20 |
+
CardinalDirection direction = CardinalDirection.values()[r.nextInt(4)];
|
| 21 |
+
|
| 22 |
+
System.out.println("Creating with values (" + length + ", " + direction + ")");
|
| 23 |
+
|
| 24 |
+
Route r1 = new Route(length, direction);
|
| 25 |
+
System.out.println("Object created!");
|
| 26 |
+
|
| 27 |
+
System.out.println("Length: " + r1.getLength());
|
| 28 |
+
System.out.println("Direction: " + r1.getDirection());
|
| 29 |
+
|
| 30 |
+
System.out.println("Testing observation...");
|
| 31 |
+
|
| 32 |
+
for (int test = 1; test <= 3; test++) {
|
| 33 |
+
length = r.nextInt(200) + 10;
|
| 34 |
+
direction = CardinalDirection.values()[r.nextInt(4)];
|
| 35 |
+
|
| 36 |
+
System.out.println("Setting length to " + length);
|
| 37 |
+
r1.setLength(length);
|
| 38 |
+
System.out.println("Length: " + r1.getLength());
|
| 39 |
+
|
| 40 |
+
System.out.println("Setting direction to " + direction);
|
| 41 |
+
r1.setDirection(direction);
|
| 42 |
+
System.out.println("Direction: " + r1.getDirection());
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
//enum defined on the backend, in an earlier qn
|
| 49 |
+
//enum CardinalDirection {
|
| 50 |
+
// // north, south, east, west
|
| 51 |
+
// NORTH, SOUTH, EAST, WEST
|
| 52 |
+
//}
|
| 53 |
+
|
| 54 |
+
//ADD
|
| 55 |
+
class Route {
|
| 56 |
+
private double length;
|
| 57 |
+
private CardinalDirection direction;
|
| 58 |
+
|
| 59 |
+
//constructor
|
| 60 |
+
public Route(double length, CardinalDirection direction) {
|
| 61 |
+
this.length = length;
|
| 62 |
+
this.direction = direction;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
public double getLength() {
|
| 66 |
+
return this.length;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
public void setLength(double length) {
|
| 70 |
+
this.length = length;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
public CardinalDirection getDirection() {
|
| 74 |
+
return this.direction;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
public void setDirection(CardinalDirection direction) {
|
| 78 |
+
this.direction = direction;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
Testing the class Route...
|
| 85 |
+
Creating with values (80.0, WEST)
|
| 86 |
+
Object created!
|
| 87 |
+
Length: 80.0
|
| 88 |
+
Direction: WEST
|
| 89 |
+
|
| 90 |
+
Testing observation...
|
| 91 |
+
Setting length to 189.0
|
| 92 |
+
Length: 189.0
|
| 93 |
+
Setting direction to SOUTH
|
| 94 |
+
Direction: SOUTH
|
| 95 |
+
Setting length to 155.0
|
| 96 |
+
Length: 155.0
|
| 97 |
+
Setting direction to SOUTH
|
| 98 |
+
Direction: SOUTH
|
| 99 |
+
Setting length to 24.0
|
| 100 |
+
Length: 24.0
|
| 101 |
+
Setting direction to WEST
|
| 102 |
+
Direction: WEST
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|