Dear All.
I have managed to make some more progress with my attempt to produce an ArrayList to demonstrate a random collection of trafficVehicles in a queue, I belive this is know as FIFO list. However although the programme works it does not seem to implement the TBS interface that I have been given. I have been told to do this using
public void simstep(){}
This method is designed to; "Perform a step of simulation. If this fails for some reason, a SimulationException is thrown. This has the effect of showing a dialog to the user containing the result of getMessage(). Therefore when the exception is thrown it should be initialised with a suitable message string, typically dirived from a TBS-specific exception."
What I need to do is
1.test if the queue is full
2. if it is remove the first vehicle
3. if the queue is not full, get a random vehicle from a list and add it to the end of the queue
4. then print a summary of the queue and the last vehicle added.
I have been able to
test this all in my main method so I know that the code does work. The problem I am having is getting this to work inside the simstep method.
public void simstep(){
if (isFull())
{queue.remove(0);
}
else
{queue.add(roadVehicle);
count ++;
}
}
The call to the add() method produces compilation errors.
---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" TrafficQueue.java
TrafficQueue.java:42: cannot find symbol
symbol : variable roadVehicle
location: class TrafficQueue
{queue.add(roadVehicle);
^
1 error
> Terminated with exit code 1.
I have seen othere programmes using the following incantation
queue.add(new roadVehicle());
But it simply produces a different error message.
---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" TrafficQueue.java
TrafficQueue.java:43: cannot find symbol
symbol : class roadVehicle
location: class TrafficQueue
queue.add(new roadVehicle());
^
1 error
> Terminated with exit code 1.
I have 4 classes which extend the class RoadVehicle and since I am being told to add things randomly I need to beable to instruct the add() method to add objects of the type RoadVehicle to my Trafficqueue.
I am not sure if this may be a problem of visiblity between may classes as the messages say can not find symbol, so I have tried to make as much as possible public, but this has not helped.
Can someone please explain the compilation errors messages and let me know what the correct call to insert a RoadVehicle into the ArrayList queue.
Best wishes
Dianne Gerrelli
PS I have pasted the TrafficQueue class below but not the other programmes
import java.util.ArrayList;
import java.util.Random;
import java.lang.Exception;
import java.awt.Graphics;
import java.lang.Object;
import simplesim.*;
public class TrafficQueue implements ThingBeingSimulated{
/** Methods from TBSModel*/
public void display (Graphics g) {};
public void setBounds(int maxX, int maxY) {};
/**Class variables*/
private ArrayList<RoadVehicle> queue;
private int count;
/**Constructor for class*/
public TrafficQueue(int size){
queue = new ArrayList<RoadVehicle> (size);
for (int i = 0; i < queue.size(); i ++)
queue.add(null);
count = 0;
}
/**Method to return empty arrayList*/
public ThingBeingSimulated reset(){
int size =0;
return new TrafficQueue (size);
}
/** Method to initiate simulation of TBS interface*/
public void simstep(){
if (isFull())
{queue.remove(0);
}
else
{/**queue.add(roadVehicle);*/
queue.add(new roadVehicle());
count ++;
}
}
/**Method to test if the TrafficQueue is empty */
public boolean isEmpty (){
return (count == 0);
}
/**Method to test if the TrafficQueue is full */
public boolean isFull (){
return (queue.size() > count);
}
/**Method to get the size of the TrafficQueue */
public int size(){
int size =0;
return size;
}
/**Method to get an object at a particular index in the TrafficQueue */
public Object get(int index){
return queue;
}
/** Method to add a roadVehicle from the arrayList*/
public void add( RoadVehicle roadVehicle)
throws QueueFullException{
if (isFull())
{throw new QueueFullException(queue.size());}
else
{queue.add(roadVehicle);
count ++;
}
}
/**Method to remove a roadVehicle from the arrayList*/
public RoadVehicle remove (int index)
throws RuntimeException {
if (isEmpty())
throw new RuntimeException ("RuntimeExceptio: attempting to remove from empty queue");
else{
count = count-1;
}
return queue.remove(0);
}
/**Method to prints the number of RoadVehicles in the TrafficQueue */
public String toString(){
int lastinqueue = queue.size();
String queueRoadVehicle = "The last RoadVehicle added is " + queue.get(lastinqueue-1) + "\n";
queueRoadVehicle+=this.queue;
return queueRoadVehicle;
}
/**Main method which creates and populates two arrays */
public static void main (String [] args){
/**Creating 10 RoadVehicle objects of type Car, Bus and GoodsVehicle*/
Car car1 = new Car ("C", "Ford", "Yellow");
Car car2 = new Car ("C", "Mini", "White");
Car car3 = new Car ("C", "Ferrari", "Cream");
Car car4 = new Car ("C", "Poche", "Orange");
Bus bus1 = new Bus ("B", "Blue", 30);
Bus bus2 = new Bus ("B", "Grey", 15);
FireEngine fireEngine1 = new FireEngine ("F", "Standard", " red ");
FireEngine fireEngine2 = new FireEngine ("F", "Green Goddess", " green ");
GoodsVehicle goodsVehicle1 = new GoodsVehicle ("G", "green", " van ",4200);
GoodsVehicle goodsVehicle2 = new GoodsVehicle ("G", "black", " lorry ",6000);
TrafficQueue queue1 = new TrafficQueue (20);
Random generator = new Random();
RoadVehicle[] someVehicles = {car1, car2, car3, car4, bus1, bus2, goodsVehicle1, goodsVehicle2,fireEngine1,fireEngine2};
try {for (int i =0; i<8; i++) {
int randomIndex = generator.nextInt( 9);
queue1.add(someVehicles[randomIndex]);
}
System.out.println(queue1);
}
catch(QueueFullException e){
System.out.println(e.getMessage ()+"queue1");
}
finally{
System.out.println("the end");
}
}
}