Yes.
This is left as an exercise.
Here is a class Box that represents a cardboard box.
public class Box implements Comparable<Box>
{
private double length, height, depth;
public Box( double length, double height, double depth )
{
this.length = length;
this.height = height;
this.depth = depth;
}
public double volume()
{
return length*height*depth;
}
// compare this Box to another Box
public int compareTo( Box other )
{
}
public String toString()
{
return( "length: " + length + ", height: " + height + ", depth: " + depth + ", volume: " + volume() );
}
}
The compareTo() method needs to be finished.
To implement the Comparable interface it should look like this:
public int compareTo( Object other )Compare the Box that is running the method withother. Return a negative integer, zero, or a positive integer, when the Box is less than, equal, or greater thanother.
The Comparable interface requires that the parameter be of type Object,
that is, reference to an object.
Think of a reasonable to write comparTo
Fill in the method.
The typecast is used to tell the compiler that the object that other
points to is a Box and that the instance variables
of Box can be used.