Question: JAVA You are to create another class called Box, which will be defined as a subclass of Rectangle. It should have the following:Data:It should inherit
JAVA
You are to create another class called Box, which will be defined as a subclass of Rectangle. It should have the following:Data:It should inherit 2 dimensions (so you will have to change how Rectangles data is declared) and then declare a 3rd dimension which will be the depth. All dimensions will be ints.Constructors:There should be 2 constructors-It should have a default constructor that sets its 3 dimensions (width, height, and depth) to 5, 2, and 6.(Note that Rectangle has a default constructor that also sets the first 2 dimensions to 5 and 2 so you can call super() to set the first 2 dimensions if you want).-It should also have a parameterized constructor that receives 3 ints (width, then height, then depth) and sets the data to whatever is received. The parameterized constructor should check to see if any of the dimensions are negative if so, it shouldthrow new IllegalArgumentException(
Methods:there should be 5 methods declared:-toString() method that will return a String that describes it: For example, if the width/height/depth are 4,5,6, then the toString() will return Box: 4x5x6 (Optional since Rectangles toString() will return automatically put in the class name and then 2 dimensions, you could (if you want), write Boxs toString by returning super.toString() and then put in the third dimension.-getPerimeter() method that will justthrow new IllegalStateException(your String).This isnecessary since Box will inherit a getPerimeter() method from Rectangle, but it should never be called since Box does not have a perimeter!-getWidth() method that will receive nothing and return the width. Actually...you do not even have to code this since Rectangle already as a getWidth that will be inherited!-getSumOfDimensions()method which will receive nothing and return the sum of all 3 dimensions. If you want, you could use Rectangles getSumOfDimensions()method in your calculation by calling super.getSumOfDimensions() and then just adding in the depth. (or you could specifically add up all 3 dimensions).-getSurfaceArea() method which will return its surface area (as an int).
/*This class will represent a rectangle.It has 2 dimensions, which arecalled width and height (so it does not matter which is larger).*/
public class Rectangle
{
//-------- dataprotected int width;protected int height;
//-------- constructors//default constructorpublic Rectangle(){this.width = 5;this.height = 2;}
//parameterized constructorpublic Rectangle(int theWidth, int theHeight){if (theWidth < 0 || theHeight < 0)throw new IllegalArgumentException("a dimension cannot be negative");this.width = theWidth;this.height = theHeight;}
//-------- methods//toString - returns its representation as a String, including width then heigh
t// (the this.getClass().getName() will evaluate to "Rectangle"herepublic String toString(){return this.getClass().getName() + ": " + width + "x" + height;}
//getWidth - returns the widthpublic int getWidth(){return this.width;}
//getPerimeter - returns the perimeter
public int getPerimeter(){return 2*width + 2*height;}//getSumOfDimensions - returns the sum of its 2 dimensionspublic int getSumOfDimensions(){return this.width + this.height;
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
