edu.colorado.simulations
Class TrafficLight

java.lang.Object
  extended by edu.colorado.simulations.TrafficLight

public class TrafficLight
extends java.lang.Object

A 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 Summary
TrafficLight(int r, int g)
          Construct a TrafficLight with a specified lengths of time that the light stays red and green.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TrafficLight

public TrafficLight(int r,
                    int g)
Construct a TrafficLight with a specified lengths of time that the light stays red and green.

Parameters:
r - the number of seconds that this light stays red
g - 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.
Throws:
java.lang.IllegalArgumentException - Indicates that one of the parameters is zero or negative.
Method Detail

simulateTime

public void simulateTime(int t)
Simulate the passage of a certain amount of time for this TrafficLight.

Parameters:
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.
Throws:
java.lang.IllegalArgumentException - Indicates that t is negative.

isRed

public boolean isRed()
Check whether this TrafficLight is red.

Parameters:
- - none
Returns:
If this TrafficLight is red, then the return value is true; otherwise the return value is false.

isGreen

public boolean isGreen()
Check whether this TrafficLight is green.

Parameters:
- - none
Returns:
If this TrafficLight is green, then the return value is true; otherwise the return value is false.