0

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.

Fildor
  • 14,510
  • 4
  • 35
  • 67
Alex Walton
  • 173
  • 3
  • 17
  • 2
    where is your EmergencyVehicle class ? Post it here – Vasu Nov 28 '16 at 11:37
  • Well, not really makes sense, although your intention was probably clear... http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – OneCricketeer Nov 28 '16 at 11:37
  • 1
    Instead of String maybe you might want to use a predefined Color type? https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html – HRgiger Nov 28 '16 at 11:40
  • 2
    Your `isEmergency` method could be hugely simplified to `return codeBlue;` – JonK Nov 28 '16 at 11:44
  • What is `RoadVehicle(String colour)` doing? –  Nov 28 '16 at 11:52

1 Answers1

4

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 + ")");