Question

Implement a new Generic class called a ShapeBox. This class will have two instance data, a...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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


}

Add a comment
Know the answer?
Add Answer to:
Implement a new Generic class called a ShapeBox. This class will have two instance data, a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • 1. Assume you have a Car class that declares two private instance variables, make and model....

    1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic...

    java Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic. The class has one class attribute that is an element. It will need 2 Constructors, a setter and getter for the class attribute, and a toString method. 2. Write a Lab10Driver that will: Instantiate a Node Integer object with no value. I. Il. Ill. IV. a. Then call the set method to set the value to 5. Instantiate a Node String object with...

  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...

  • Implement a Java class that is called RainFall that uses a double array to store the...

    Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class: 1. A constructor that creates and initializes all entries in the array to be -1. 2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the...

  • Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the followi...

    Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT