I am working on homework for my participation in the programming class, which includes the implementation of interfaces. The problem here is that I just donβt understand the interfaces or what they are used for (the professor did not explain this very well).
The purpose was to create a superclass of the Vehicle class and three subclasses, something like Truck or Jeep, each of which would have its own features. The class "Vehicle" should implement a comparable interface, which, I think, I understand (I have a method compareTo()written that compares the number of doors on vehicles), and another class should also implement the class "Hybrid" (I donβt know what it is means). Then we must implement the methods toString(), equals(Object o)and compareTo(Object o).
I think I'm compareTo()down, but equals()I have no idea. The last thing we need to do is write Main for testing, this includes creating an array of Vehicle objects, printing them out, sorting and reprinting them. He also needs to iterate over the array and print the premium premium for hybrids and use the method equals(Object o)to compare 2 vehicles.
Here is the code for my "Vehicle" supercar
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;}
@Override
public int compareTo(Object o) {
if (o instanceof Vehicle) {
Vehicle v = (Vehicle)o;
return this.numberOfDoors - v.getNumberOfDoors();
}
else {
return 0;
}
}
@Override
public String toString() {
String answer = "The car color is "+this.color
+". The number of doors is"+this.numberOfDoors;
return answer;
}
}
And I will also send one of my subclasses
package vehicle;
abstract public class Convertible extends Vehicle {
private int topSpeed;
public Convertible (String aColor, int aNumberOfDoors, int aTopSpeed) {
super(aColor, aNumberOfDoors);
this.topSpeed = aTopSpeed;
}
public int getTopSpeed() {return(this.topSpeed);}
public void setTopSpeed(int topSpeedSet) {this.topSpeed = topSpeedSet;}
@Override
public String toString() {
String answer = "The car color is "+super.getColor()
+", the number of doors is "+super.getNumberOfDoors()
+", and the top speed is "+this.topSpeed+" mph.";
return answer;
}
}
I definitely do not ask anyone to do this work for me, but if someone can shed a little more light on how the interfaces work, and that this homework really asks what would be great.
All help is much appreciated
Thank!