Question: Exercise: Search & Sort In this assignment, you will investigate how to compare objects, implement a typical sorting algorithm, and implement a binary search algorithm.

Exercise: Search & Sort

"In this assignment, you will investigate how to compare objects, implement a typical sorting algorithm, and implement a binary search algorithm. As usual, we will practice using both arrays and collections in Java, and you should reflect on the benefits and costs of each approach. It may assist you to create helper methods that create lists of Box objects when testing, e.g. Box[] generateArrayOfBoxes(int n) and List generateListOfBoxes(int n)."

Requirements

- You must use the same method names as specified below. The test classes will not compile otherwise.

- Methods should be appropriately documented and tested (see the testing section)

- You may also include a main method if desired

A Box class has been provided for you in src/Box.java. Make yourself familiar with the source code, then expand upon this class so that it implements the Comparable interface. You must override the compareTo method so that instances of Box can be compared by their volume.

How do I solve this? I'm so confused.

Box.java :

/**
* The Box class models a three-dimensional box
*/
public class Box {
private final int height;
private final int width;
private final int depth;
/**
* Create a new Box with the specified dimensions (height, width, depth).
*
* @param height the height of the box
* @param width the width of the box
* @param depth the depth of the box
*/
public Box(int height, int width, int depth) {
this.height = height;
this.width = width;
this.depth = depth;
}
/**
* Create a copy of box.
*
* @param box A Box to copy.
*/
public Box(Box box) {
this.height = box.height;
this.width = box.width;
this.depth = box.depth;
}
/**
* Get this box's volume
*
* @return the box's volume
*/
public int volume() {
return height * width * depth;
}
/**
* @return The box's height
*/
public int getHeight() {
return height;
}
/**
* @return The box's width
*/
public int getWidth() {
return width;
}
/**
* @return The box's depth
*/
public int getDepth() {
return depth;
}
/**
* Defines if two Boxes should be considered equal according to their volume.
*
* @param o an object
* @return true if the given object has equal volume to this Box
*/
@Override
public boolean equals(Object o)
{
if (!(o instanceof Box)) {
return false;
}
Box other = (Box) o;
return this.volume() == other.volume();
}
/**
* Defines the hash code of this Box.
*
* This is required by the contract of hashCode, which states that if for
* two objects x and y, x.equals(y) is true,
* then x.hashCode() == y.hashCode() must also be true. So, as we override
* the Object.equals(Object o), we must also override Object.hashCode().
*
* For a good explanation, see Effective Java Recipe Item 9
* @return the hash code of this Box
*/
@Override
public int hashCode(){
int result = 13;
result = 31 * result + height;
result = 31 * result + width;
result = 31 * result + depth;
return result;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!