The next piece of the traffic flow system is to write a class that creates the vehicle queues at the intersection.
This class should be named
VehicleQueue class should be sufficient with different instances for different directions. The basic structure of the class is shown in this diagram:
attribute is the creator class that is responsible for creating individual vehicles. It decides what the likelihood is of a particular vehicle appearing in this queue.
attribute is a floating point number between 0 and 1. It determines the likelihood that a vehicle (of any type) appears in this queue in any given second.
method is called each second. The
method is invoked whenever the corresponding light is green.
As well as the attributes and operations documented here, you will need to provide a means of storing and manipulating the list of cars.
In Java 1.1 the
class is useful, though not terribly efficient for this purpose.
In Java 1.2 the
class is a much safer bet.
In C++, the standard templates library can be helpful. I have deliberately left the type of the
unspecified so you can use whatever is convenient in your environment.
object knows the chance of a vehicle entering the queue at an arbitrary time slice. Each queue is further parameterized with a
object that understands the chances of the different vehicles entering the queue at any time.
method is invoked, a random number function is called to determine whether or not to add a new vehicle to the queue. When the light turns green, the
method is invoked as many times as possible.
We are not quite ready, yet, however, to actually invoke the queuing functions. That will have to wait for the
class and Observer pattern used later in the course.
getLength() method returns the total length in meters of the queue; that is the sum of the lengths of the individual vehicles.
getSize() method, by contrast, returns the number of vehicles in the queue. Finally,
list() is a method intended mostly for debugging that prints the list of vehicles in the queue on the console.
Type your answer in the text area below. Click the Submit button to see the result.