import java.util.Random; import java.awt.*; /**A class representing a general notion of traffic *queues that know the number of road vehicles they contain *and can add and remove road vehicles from their lanes */ public class TrafficQueue implements ThingsBeingSimulated { //Arrays show the intial array size and holding the // number of vehicles private ArrayList vehicle; private int size; private int maxCapacity; /** Constructor which takes the required size of * the queue as well as its maximum capacity */ public TrafficQueue(int size, int maxCapacity) { this.vehicle= newRoadVehicle[size]; this.maxCapacity= maxCapacity; } /** adds a road vehicle to the queue or throw an exception it is full */ public void add(RoadVehicle roadVehicle) throws QueueFullException { if (this.isFull()) { System.out.println("the queue is full"); System.exit(1); } else { vehicle[size] = roadVehicle; size++; } } /** return the maximum capacity of vehicles the queue can hold */ public int getMaxCapacity(){ return maxCapacity;} /** check whether the queue is currently empty */ public boolean isEmpty() { return (this.size == 0); } /** check whether the queue is currently full */ public boolean isFull() { return (this.vehicles.length == this.size); } /** remove a road vehicle from the front of the queue lanes */ public RoadVehicle remove() { RoadVehicle roadVehicle1 = null; if (this.isEmpty()) { throw new RuntimeException("removing from empty queue"); } else { roadVehicle1= this.vehicle[0]; this.size--; for(int x=0; x < this.size;x++) { this.vehicle[x]=this.vehicle[x+1];} } return roadVehicle1; } //dummy implementation of TBSView public void display(Graphics g) {} public void setBounds (int maxX, int maxY) {} // implement TBSmodel public Array reset(){ return Array=0;} public void simstep(){ if (this.isFull()){ return roadVehicle1} else{ try(this.add(ArrayList someVehicles)); } } /** show all individual road vehicle objects */ public String toString() { for(int x=0; x someVehicles = {"red Jaguar","red Honda","blue van" 800, "red van" 3200, "red bus" 45, "red bus" 20}; //take a random vehicle and add it to the queue Random randomVehicle = new Random(); if (randomVehicle.nextInt(int ArrayList.length)){ queueLane1 = ArrayList someVehicles; } System.out.println(queueLane1); //catch the exception when adding a vehicle to the queue catch(QueueFullException e) { System.out.println("queue full"); throw new SimulationException ("simulation problems have occured"); //catch simulation exceptions catch(SimulationException e){ System.out.println(e.getMessage()); } } //catch the exception when removing vehicles catch(RuntimeException e){ System.out.println(e.getMessage()); } //Do this regardless finally{System.out.println("Execution complete"); } } }