I am basically using Java Constructor chaining, to try and add vehicles to my TrafficQueue. If I run the below code:
The part of code I am struggling to get working is
It wont allow me to run the method isEmergency() on vehicle.
FireEngine class:
and EmergencyVehicl e class:
So, using extends, I am trying to add the vehicle to the front of the queue, if it isEmergency returns true.
The error I get is:
error, cannot find symbol.
I don't need the exact solution, but I think it should work as I have got the extends in each class. Can anyone point out where it goes wrong? I realise that the code needs neatening up slighty!
When adding a vehicle to the queue, I use
The part of code I am struggling to get working is
Code:
public void add(RoadVehicle vehicle) {
if (isFull()) {
System.out.println("Queue is full! We cannot add any more vehicles to the Queue!");
System.exit(1);
} else {
if (vehicle instanceof EmergencyVehicle && vehicle.isEmergency()) {
addToFront(vehicle);
} else {
addToBack(vehicle);
}
}
}
FireEngine class:
Code:
public abstract class EmergencyVehicle extends RoadVehicle {
public boolean codeBlue = true;
public EmergencyVehicle(String colour){
super(colour);
}
public boolean isEmergency() {
return codeBlue;
}
public void setEmergency(boolean newEmergency) {
codeBlue = newEmergency;
}
}
Code:
public class FireEngine extends EmergencyVehicle {
public FireEngine(String colour) {
super (colour);
}
public FireEngine() {
this("red");
}
public String toString () {
if ("red".equals(colour)) {
return "Fire Engine : CODEBLUE";
} else
return colour + " FireEngine.";
}
}
The error I get is:
error, cannot find symbol.
I don't need the exact solution, but I think it should work as I have got the extends in each class. Can anyone point out where it goes wrong? I realise that the code needs neatening up slighty!
When adding a vehicle to the queue, I use
Code:
FireEngine car1 = new FireEngine("red");