/** * TrafficLight class to control the state of the lights. A single class * controls the lights in by NS and EW directions. * @author mckaydim * */public class TrafficLight{ private boolean nsLight; private boolean ewLight; private int counter; private int gValue; private int stepCount; /** * TrafficLight constructor. Sets the initial values for stage 1 of the * traffic light functions. */ public TrafficLight(int gVal) { nsLight = false; ewLight = true; gValue = gVal; counter = gValue; stepCount = 0; } /** * At each 'stage' the counter is reset to the appropriate value. */ public void tick() { switch( stepCount ) { case 0: stepCount++; counter = gValue; nsLight = false; ewLight = true; break; case 1: stepCount++; counter = 12; nsLight = false; ewLight = false; break; case 2: stepCount++; counter = gValue; nsLight = true; ewLight = false; break; case 3: stepCount = 0; counter = 12; nsLight = false; ewLight = false; break; } counter--; } /** * Method for NSLight. If green then returns true. Red returns false. * @return boolean */ public boolean nsLight() { return nsLight; } /** * Method for EWLight. If green then returns true. Red returns false. * @return boolean */ public boolean ewLight() { return ewLight; } }