I am working on a homework task for implementing interfaces, and I'm a little lost. I need to implement a comparable interface and use the compareTo () method. Here is the code for my superclass, it has three subclasses, which are different forms of vehicles. In this case, I am trying to set the number of doors that they have.
Below is the code for the "Car" superclass
package vehicle;
abstract public class Vehicle implements Comparable {
private String color;
private int numberOfDoors;
public Vehicle(String aColor, int aNumberOfDoors) {
this.color = aColor;
this.numberOfDoors = aNumberOfDoors;
}
public String getColor() {return(this.color);}
public int getNumberOfDoors() {return(this.numberOfDoors);}
public void setColor(String colorSet) {this.color = colorSet;}
public void setNumberOfDoors(int numberOfDoorsSet) {this.numberOfDoors = numberOfDoorsSet;}
public int compareTo(Object o) {
if (o instanceof Vehicle) {
Vehicle v = (Vehicle)o;
}
else {
return 0;
}
}
@Override
public String toString() {
String answer = "The car color is "+this.color
+". The number of doors is"+this.numberOfDoors;
return answer;
}
}
This is currently a work in progress, and I'm not sure where to go from here using the compareTo method. Any help is greatly appreciated.
Thank!
Edit Once I get the compareTo () method working in the superclass, is there anything I need to add to the subclasses to execute this function?
Thank!
source