Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Using The Comparable Interface
    text
    copied!<p>I am working on a homework assignment on implementing interfaces, and am a little lost. I need to implement the comparable interface and use the compareTo() method. Here is the code of my super class, it has three subclasses that are all different forms of vehicles. In this case I am trying to campare the number of doors that they have.</p> <p>Below is the code for the "Vehicle" superclass</p> <pre><code>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's color is "+this.color +". The number of doors is"+this.numberOfDoors; return answer; } } </code></pre> <p>Currently it is a work in progress, and I'm not to sure where to go from here on the compareTo method. Any help is much appreciated.</p> <p>Thanks!</p> <p><em>Edit</em> Once I get the compareTo() method working in the superclass, is there anything that I need to add to the subclasses to make this function?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload