All pastes #823354 Raw Edit

Unnamed

public java v1 · immutable
·published 2007-12-19 09:15 UTC
rendered paste body
  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 <RoadVehicle> 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 TBSViewpublic void display(Graphics g) {}public void setBounds (int maxX, int maxY) {}// implement TBSmodelpublic Array<RoadVehicle> reset(){  return Array<RadVehicle>=0;}public void simstep(){    if   (this.isFull()){     return  roadVehicle1}    else{    try(this.add(ArrayList<RoaVehicle> someVehicles));  }}/** show all individual road vehicle objects */      public String toString() {    for(int x=0; x<this.size;x++)  return "The number of cars on this queue is"+ this.size;    }/** Test the program*/  public static void main(String [] args) {    TrafficQueue queueLane1 = new TrafficQueue(10);// this code was replaced by a block that is able // to add a random vehicle to the queue    /** queueLane1.add(new roadvehicle("red", "Jaguar"));  queueLane1.add(new roadVehicle("red", "Honda"));  queueLane1.add(new roadvehicle("blue", "van", 800));        queueLane1.add(new roadvehicle("red", "van", 3200));        queueLane1.add(new roadvehicle("red", "bus", 45));        queueLane1.add(new roadvehicle("red", "bus", 20));  System.out.println(queueLane1); */// an array of vehicle contained in the first queue lane  public ArrayList <RoadVehicle> 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<RoadVehicle>.length)){  queueLane1 = ArrayList <RoadVehicle> 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"); }}}