Java using a comparable interface

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;

    // Constructor
    /**
     * Creates a vehicle with a color and number of doors
     * @param aColor The color of the vehicle
     * @param aNumberOfDoors The number of doors
     */
    public Vehicle(String aColor, int aNumberOfDoors) {
        this.color = aColor;
        this.numberOfDoors = aNumberOfDoors;
    }

    // Getters
    /**
     * Gets the color of the vehicle
     * @return The color of the vehicle
     */
    public String getColor() {return(this.color);}
    /**
     * Gets the number of doors the vehicle has
     * @return The number of doors the vehicle has
     */
    public int getNumberOfDoors() {return(this.numberOfDoors);}

    // Setters
    /**
     * Sets the color of the vehicle
     * @param colorSet The color of the vehicle
     */
    public void setColor(String colorSet) {this.color = colorSet;}
    /**
     * Sets the number of doors for the vehicle
     * @param numberOfDoorsSet The number of doors to be set to the vehicle
     */
    public void setNumberOfDoors(int numberOfDoorsSet) {this.numberOfDoors = numberOfDoorsSet;}

    public int compareTo(Object o) {
        if (o instanceof Vehicle) {
            Vehicle v = (Vehicle)o;
        }
        else {
            return 0;
        }
    }

    /**
     * Returns a short string describing the vehicle
     * @return a description of the vehicle
     */
    @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!

+3
source
4

Vehicle Comparable<Vehicle>, compareTo Vehicle , .

, compareTo, , ; , , , 0. , , color.compareTo(otherVehicle.color) , String s.

!

+4

, :

public int compareTo(Object o) {
    if (o instanceof Vehicle) {
        Vehicle v = (Vehicle) o;
        return getNumberofDoors() - v.getNumberOfDoors();
    } else {
        return 0;
    }
}
+2

compareTo() int, , (). , "" , .

+1
source

Put your vehicle first Comparable<Vehicle>, as Louis said. Then, in the compareTo method, you want to return a value comparing the aspect of the vehicle to which you want to compare.

public int compareTo(Vehicle v) {
        return this.getNumberOfDoors() - v.getNumberOfDoors();
    }

This will return zero if the number of doors is the same. Negative if v has more doors and positive if v has fewer doors.

You can also compare something else (e.g. make a car if you add this)

public int compareTo(Vehicle v) {
            return this.make.compareTo(v.make);
        }
+1
source

All Articles