CSC220 Lab Number 6
Due: 11:59PM, Tuesday, October 20
In this lab, we will develop a program for simulating traffic at a traffic light. The problem statement is as follows:
|
The three queues of traffic.
|
-
There are three lanes going in the same direction.
One lane, is for cars making a left turn, one for cars going straight, and one for cars making a right turn.
-
The traffic light alternates between the red light and the green light.
For simplicity, there will be no Yellow light.
-
During the green light the cars in each line proceeds to its intended direction so long as the time permits.
-
During the red light new cars may arrive at the intersection and line up behind the three lanes.
-
Beyond the intersection the traffic is so light that there will be no further accumulation of cars.
-
Each car has an intended direction (left, straight, or right) and speed (a rational number between 0 and 1.0, which represents the time that it takes for the car to cross the light).
-
The class RandomCar is for producing a new car.
Inside RandomCar, a class called Car is defined.
Each Car object has a data field for direction and a data field for speed.
The method getNew() of RandomCar produces a new car at a certain possibility.
If it chooses not to produce a car, it returns null; otherwise, it returns a Car object.
-
RandomCar uses some parameters.
There are default settings for these parameters.
It is possible to construct a RandomCar object with particular selections
of these parameters.
The simulation should be conducted as follows:
-
First, the duration of green light and the duration of red light are set to some integer values.
-
Next, the number of red-green cycles to be repeated is determined.
-
Create a queue object that holds Car objects for each of the three directions.
For the queue, use the MyQueues from last week.
-
During each second of the red light period, a Car is randomly generated and each car generated is added at the end of the corresponding queue.
-
In the green light period, for each queue, the cars in that queue are removed one at a time so long as the time remaining is at least the speed of the car at the head of the queue.
The time required for a car to go through the intersection is its speed.
There will no time gap between two cars crossing the intersection one after the other.
Your task is to write a program that receives input from the user about how the simulation is done and compute the maximum size and the average size of queue during the simulation.
A sample program is here.