-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficLight.java
More file actions
50 lines (44 loc) · 914 Bytes
/
Copy pathTrafficLight.java
File metadata and controls
50 lines (44 loc) · 914 Bytes
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
package com.lun.enumerated;
//: enumerated/TrafficLight.java
// Enums in switch statements.
// Define an enum type:
enum Signal {
GREEN, YELLOW, RED,
}
public class TrafficLight {
Signal color = Signal.RED;
public void change() {
switch (color) {
// Note that you don't have to say Signal.RED
// in the case statement:
case RED:
color = Signal.GREEN;
break;
case GREEN:
color = Signal.YELLOW;
break;
case YELLOW:
color = Signal.RED;
break;
}
}
public String toString() {
return "The traffic light is " + color;
}
public static void main(String[] args) {
TrafficLight t = new TrafficLight();
for (int i = 0; i < 7; i++) {
System.out.println(t);
t.change();
}
}
}
/*
The traffic light is RED
The traffic light is GREEN
The traffic light is YELLOW
The traffic light is RED
The traffic light is GREEN
The traffic light is YELLOW
The traffic light is RED
*/