File size: 2,602 Bytes
34c83e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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