Subj : intersection design help To : comp.programming From : zaebos Date : Fri Sep 16 2005 11:53 am Hi everyone, I am trying to implement a simulation of a traffic intersection with threads, from a textbook exercise, however I have run into some problems, and seek assitance with the design..not so much at the algorithm level, just as to which classes and basic methods I would have first..I have tried a few ideas but none seem correct. The details are to implement a program that simulates the behaviour of an intersection used by a number of cars, with four traffic lights and a number of sensors in the road, and a controller that controls the traffic lights based on the sensor information. =B7 The program should include the following timing requirements (all the constants below should be easy to change). o After the traffic lights have changed to a certain colour (green, yellow or red), they must stay that way for a minimum of 2 seconds. o If there are cars waiting in one direction (e.g. north-south), then the lights in the "other" direction (e.g. east-west) must change within approximately 10 seconds, even if there are more cars approaching/entering the intersection from this other direction. o After entering an intersection, cars must spend between 1-3 seconds in the intersection (the exact time must be randomly determined when the car enters the intersection). You do not need to worry about how cars avoid hitting each other in the intersection. =B7 Cars, which must be implemented as threads, should be suspended until the light in the direction from which they have approached has turned green. =B7 Instead of three sensors for each direction, there are only two: the approach and enter sensors are combined into a single sensor. =B7 Cars must enter the intersection in the order of arrival and there must be at least 1 second between successive cars entering the intersection from the same direction (cars from opposite directions may enter the intersection at approximately the same time). Your simulation may define a maximum number of cars, in which case this maximum should be at least 8 and easy to change. The main events under the control of the user should be when in the simulation a car approaches the intersection, from what direction it approaches, and in which direction it leaves. After a car approaches the intersection, a car must enter the intersection when the appropriate light is green and then leave the intersection after between 1-3 seconds (no further user input should be required for this). =B7 Each car must be implemented as a thread; there may be other threads as well, but this is not necessary. =B7 Threads must suspend by either calling sleep (if they have to wait for a certain amount of time) or by using wait/notify (if they have to wait for a certain condition to hold). There should not be any busy-wait loops in your implementation. =B7 There should be a way to terminate the simulation gracefully (controlled by a user event, not using CTRL-C or the Java System.exit or Thread.stop methods). I am unsure of any ideas for the sensors at this stage, but what I have so far is below(in java) import java.util.*; public class Traffic{ public static void main(String[] args){ Random random =3D new Random(); int n =3D 0; while (++n < 100){ new Car().start(); try{ Thread.sleep(500 + random.nextInt(1000)); (1000)); //car arriving intervals } catch (InterruptedException e){ e.printStackTrace(); } } } } class InterSection{ static String[] nums =3D {"First", "Second", "Third", ", "Fourth", "Fifth", "Sixth", "Seventh", "Eigth", "Nineth", "Tenth", enth", "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", enth", "Fifteenth", "Sixteenth", "", "", "", "", "", "", "", "", ""}; TraficLight tt; int carNum; // temporary car number at theintersection public InterSection(){ tt =3D new TraficLight(this); carNum =3D 0; } public boolean carCanGo(){ return ! tt.isRed(); } public void carArrived(){ System.out.println(nums[carNum] + " car car arrived"); ++carNum; } public void carPassed(){ System.out.println(nums[carNum - 1] + " car car passed"); --carNum; } } class Car extends Thread{ static InterSection is; static int num =3D 0; // serial number of passing ng cars boolean printed; public Car(){ ++num; if (is =3D=3D null){ is =3D new InterSection(); } printed =3D false; } public void run(){ is.carArrived(); //story starts at a car arriving ving the intersection while (! is.carCanGo()){ if (! printed){ System.out.println("...and waiting(car serial r serial num: " + num + ")"); printed =3D true; } } is.carPassed(); //story ends at car leaving the the intersection } } class TraficLight{ boolean red; Timer timer; TimerTask ttask; long delay; InterSection is; public TraficLight(InterSection s){ timer =3D new Timer(); ttask =3D new TimerTask(){ public void run(){ if (is !=3D null && is.carNum =3D=3D 1){ setRed(false); //first arriving car has ng car has privilege cancel(); } else{ setRed(! isRed()); } } }; delay =3D 8000; // eight second interval setRed(true); timer.schedule(ttask, delay, delay); } public boolean isRed(){ return red; } public void setRed(boolean value){ red =3D value; } } I am particulary unsure about the sensors idea..., If someone could hint me towards which classes I should have and maybe just the basic framework, I would have a better idea and could get to attempt the coding.. any assitance is greatly appriciated .