I want to, within my abstract class
, define two constructors.
When create a new instance of the class
, i want the toString to return something different depending on what was called:
The FireEngine Class
public class FireEngine extends EmergencyVehicle {
private String colour;
public FireEngine(String colour) {
super (colour);
}
public FireEngine() {
this("red");
}
public String toString () {
if (colour == "red") {
return "red";
} else
return "no";
}
}
The EmergencyVehicle
class:
public abstract class EmergencyVehicle extends RoadVehicle {
public boolean codeBlue = false;
public EmergencyVehicle(String colour){
super(colour);
}
public boolean isEmergency () {
if (codeBlue == true) {
return true;
} else {
return false;
}
}
public void setEmergency(boolean newEmergency) {
codeBlue = newEmergency;
}
}
This is a homework exercise so I don't want the answer per se, but does the above code make sense?
For example, if I add a new EmergencyVehicle, I want an if statement depending on what colour the vehicle I add is.
1st Remark
Don't call
this("red");
in the default constructor, do
colour = "red";
unless the EmergencyVehicle(String colour)
RoadVehicle(String colour)
constructor is doing something else.
2nd Remark
Don't compare using
if (colour == "red")
use
if ("red".equals(colour))
3rd Remark
The method
public String toString()
is supposed to return a string representation of the instance. You implementation only returns red
or no
which is not very informative. Use something like
return("FireEngine(colour=" + colour + ")");