-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathTrafficSignal.java
More file actions
60 lines (56 loc) · 1.7 KB
/
TrafficSignal.java
File metadata and controls
60 lines (56 loc) · 1.7 KB
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
package Programs;
class TrafficSignal
{
private String colour;
private TrafficSignal(String colour)
{
this.colour=colour;
}
private boolean isRed()
{
return colour.equals("red");
}
private boolean isYellow()
{
return colour.equals("yellow");
}
private boolean isGreen()
{
return colour.equals("green");
}
//Driver Code
public static void main(String[] args)
{
TrafficSignal light=new TrafficSignal("red");
while(true)
{
if(light.isRed())
{
System.out.println("The Signal is red.");
light.colour = "yellow";
}
else if(light.isYellow())
{
System.out.println("The Signal is yellow.");
light.colour = "green";
}
else if(light.isGreen())
{
System.out.println("The Signal is green.");
light.colour = "red";
}
for(int i = 10; i > 0; i--)
{
try{
System.out.print(" "+i);
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println("Delay not applied.");
}
}
System.out.println();
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
}