All pastes #980306 Raw Edit

idea

public java v1 · immutable
·published 2008-04-10 22:32 UTC
rendered paste body
package trafficlight;public class TrafficLight {	private boolean green = false;	private boolean red = false;	private int g = 0;	private int time = 12;	private int counter = 0;	public TrafficLight(int g) {		this.g = g;		counter = g + time;		red = true;	}		public void tick() {		if( red && counter == 0 ) {			green = true;			red = false;			counter = g;		} else if( green && counter == 0 ) {			green = false;			red = true;			counter = g + ( 2 * time );		} else {			counter--;		}	}		public boolean isGreenLit() {		return green;	}		public boolean isRedLit() {		return red;	}	public static void main(String[] args) {		TrafficLight tl = new TrafficLight( 10 );		for( int i = 0; i < 500; i++ ) {			tl.tick();			System.out.println( i + " => G: " + tl.isGreenLit() + ", R: " + tl.isRedLit() );		}	}}