-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathTrafficlight.java
45 lines (36 loc) · 1.07 KB
/
Trafficlight.java
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
//TrafficLight.java
public class TrafficLight {
private String color;
private int duration;
public TrafficLight(String color, int duration) {
this.color = color;
this.duration = duration;
}
public void changeColor(String newColor) {
color = newColor;
}
public boolean isRed() {
return color.equals("red");
}
public boolean isGreen() {
return color.equals("green");
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}//Main.java
public class Main {
public static void main(String[] args) {
TrafficLight light = new TrafficLight("red", 30);
System.out.println("The light is red: " + light.isRed());
System.out.println("The light is green: " + light.isGreen());
light.changeColor("green");
System.out.println("The light is now green: " + light.isGreen());
System.out.println("The light duration is: " + light.getDuration());
light.setDuration(20);
System.out.println("The light duration is now: " + light.getDuration());
}
}