Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a new Generic class called a ShapeBox. This class will have two instan

ID: 3894969 • Letter: I

Question

Implement a new Generic class called a ShapeBox. This class will have two instance data, a Shape object (as in the graphics Shape like Circle, Rectangle, Ellipse), and a double to store the Shape's area. The specific type of Shape will be specified via Generics so that your class will need a placeholder for the type of Shape. This placeholder must extend Shape. Additionally, ShapeBox will implement the Comparable interface requiring that you implement a compareTo method to compare this ShapeBox to another ShapeBox passed as a parameter. Remember that compareTo expects an Object, so you will have to use proper downcasting. The compareTo will compare this ShapeBox's area to the parameter's area to determine the comparison. As the areas are doubles, you will have to convert them to ints. Aside from compareTo, the class will consist of two constructors, a no-arg constructor (which initializes the Shape to null and the double to 0) and a 2-arg constructor which receives both values, and getters and setters for both instance data. NOTE: you do not need to implement a "computeArea" method.

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

ShapeBox.java
=========
import java.awt.Shape;

public class ShapeBox<T extends Shape> implements Comparable {
private T shape;
private double area;

public ShapeBox() {
shape = null;
area = 0.0;
}

public ShapeBox(T shape, double are) {
this.shape = shape;
this.area = area;
}


public T getShape() {
return shape;
}

public void setShape(T shape) {
this.shape = shape;
}

public double getArea() {
return area;
}

public void setArea(double area) {
this.area = area;
}

@Override
public int compareTo(Object o) {
if(o instanceof ShapeBox)
{
double area2 = ((ShapeBox)o).area;
if(area > area2)
return 1;
else if(area < area2)
return -1;
else
return 0;
}
return 1;
}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote