Question
Provide a BetterRectangle sub-class that extends the Rectangle class of the standard Java library by adding methods to compute the area, perimeter, slope and mid-point
Provide a BetterRectangle sub-class that extends the Rectangle class of the standard Java library by adding methods to compute the area, perimeter, slope and mid-point of the rectangle, as well as valid constructors for the new sub-class. Provide a Tester program that will execute and validate the BetterRectangle sub-class
Output: Output will provide validation that all new features in the sub-class are functioning properly - presented in a clear, readable and attractive manner. All output should be handled by the Tester class, not the super or sub-class. Use the toString() method to display the characteristics of each rectangle.
Input: No user input required.
No switch or breaks statements allowed. No Magic numbers!
The Tester class will create four (4) better rectangles, using each of the required constructors. Next, selecting one of the rectangles, the Tester will execute all of the added accessor methods. Then, using two of the remaining rectangles, the Tester will execute the utility methods (and the equals() method) listed below, comparing the two rectangles. Finally, using the remaining rectangle, the Tester will execute the mutator method. Expected values should be displayed for each result to confirm correct results.
The BetterRectangle sub-class may NOT add any new instance variables; however, it can provide new constants. In the sub-class constructors, use the setLocation() and setSize() methods of the Rectangle class. Include an override for the toString() and equals() methods, as appropriate. The sub-class must implement all of the below listed methods/constructors, overriding as appropriate.
Constructors - must use setLocation() and setSize() methods
BetterRectangle(): creates a unit rectangle at Origin
BetterRectangle(int w, int h): creates rectangle at Origin
BetterRectangle(int x, int y, int w, int h): create rectangle
BetterRectangle(BetterRectangle r): creates a copy if rectangle r
Override - as appropriate
toString() to also include area, perimeter, slope and mid-point; in
addition to the lower left coordinates, width and height.
equals(r)
Added Accessor - no new instance variables
getArea() return (int) - width * height
getPerimeter() return (int) - (width + height) * 2
getSlope() return (float) - height / width (float)
getMidPoint() return (Point) - center of rectangle + lower left corner
(rounded off to int)
Added Utilities - (r) is another rectangle
congruent(r) return true if (width + height) is same for both
equivalent(r) return true if perimeter is same for both
similar(r) return true if area is same for both concentric(r) return true if midpoint is same for both
Added Mutator
boolean ScaleBy(scale) multiply height, width by fixed + int scale,
returns true upon success, returns false upon failure.
Use of the Point class is required
JAVA
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started