Java - implementation of interfaces

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;

    // 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;}

    @Override
    public int compareTo(Object o) {
        if (o instanceof Vehicle) {
            Vehicle v = (Vehicle)o;
            return this.numberOfDoors - v.getNumberOfDoors();
        }
        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;
    }
}

And I will also send one of my subclasses

package vehicle;

abstract public class Convertible extends Vehicle {
    private int topSpeed;

    // Constructor
    /**
     * Creates a convertible with a color, number of doors, and top speed
     * @param aColor The color of the convertible
     * @param aNumberOfDoors The number of doors of the convertible
     * @param aTopSpeed The top speed of the convertible
     */
    public Convertible (String aColor, int aNumberOfDoors, int aTopSpeed) {
        super(aColor, aNumberOfDoors);
        this.topSpeed = aTopSpeed;
    }

    // Getters
    /**
     * Gets the top speed of the convertible
     * @return The top speed of the convertible
     */
    public int getTopSpeed() {return(this.topSpeed);}

    // Setters
    /**
     * Sets the top speed of the convertible
     * @param topSpeedSet The top speed to set to the convertible
     */
    public void setTopSpeed(int topSpeedSet) {this.topSpeed = topSpeedSet;}

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

+5
source share
4

, , , .

?

, , , " ". , , "", . , :

interface Bounceable{
  public void setBounce(int bounce);
  public int getBounce();
}

-, . , , , , . , , .

?

, , . .

public class FootBall implements Bounceable{
private int bounce;

public void setBounce(int bounce){
   this.bounce = bounce;
}

public int getBounce(){
  return this.bounce;
}

}

public class BaseBall implements Bounceable{
private int bounce;

public void setBounce(int bounce){
   this.bounce = bounce;
}

public int getBounce(){
  return this.bounce;
}

}

. Bounceable. , , ? , ? , , . , , , , .

EDIT: .

...

public void slamDunk(Bounceable bouncyThing){
  System.out.println("You scored three points!");
}

slamDunk ...

slamDunk(new BaseBall());

slamDunk(new FootBall());

slameDunk .

+12

Comparable http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html :

int compareTo(T o)
Compares this object with the specified object for order. 
Returns a negative integer, zero, or a positive integer as this object is less than, 
equal to, or greater than the specified object. 

:

public class Vehicle implements Comparable {
//...
}

, . , compareTo(). , . , , "", "" . , "" - , , - . - , , .

, Comparable.

0

, . , , , (, Comparable, , , compareTo.)

, - Comparable. null compareTo.

equals(Object o). , compareTo(Object o) - true, , , , false, . ( null - o null, false).

toString(), toString() . , "toString() , , - (, ..)

0

( ( ) )

. - 1. 2 , 2 , 2 ( = 2 ) 2. , 10 , 10 13 .

, , β„– 1 2, . β„– 9 4, 1 6 , .... .. IMP - 3 5 . .

, !

, -, . , .


, . , (int string) , .

, , .

0

All Articles