public class TrafficLight
extends java.lang.Object
TrafficLight
object simulates a simple traffic light.
Java Source Code for this class:
http://www.cs.colorado.edu/~main/edu/colorado/simulations/TrafficLight.java
Constructor and Description |
---|
TrafficLight(int r,
int g)
Construct a
TrafficLight with a specified lengths of time
that the light stays red and green. |
Modifier and Type | Method and Description |
---|---|
boolean |
isGreen()
Check whether this
TrafficLight is green. |
boolean |
isRed()
Check whether this
TrafficLight is red. |
void |
simulateTime(int t)
Simulate the passage of a certain amount of time for this
TrafficLight . |
public TrafficLight(int r, int g)
TrafficLight
with a specified lengths of time
that the light stays red and green.r
- the number of seconds that this light stays redg
- the number of seconds that this light stays green
Precondition:
Both r and g are positive.
Postcondition:
This TrafficLight
has been initialized. It starts
red and stays red for r seconds; then it switches to green for
g seconds, back to red for r seconds, back to green for g
seconds, and so on.java.lang.IllegalArgumentException
- Indicates that one of the parameters is zero or negative.public void simulateTime(int t)
TrafficLight
.t
- the number of seconds to simulate during the call to this method
Precondition:
t >= 0
Postcondition:
This TrafficLight
has simulated the passage of
t more seconds of time.java.lang.IllegalArgumentException
- Indicates that t
is negative.public boolean isRed()
TrafficLight
is red.TrafficLight
is red, then the return
value is true; otherwise the return value is false.public boolean isGreen()
TrafficLight
is green.TrafficLight
is green, then the return
value is true; otherwise the return value is false.