Question
Java The idea is to implement a class to manage very basic rectangle objects in the Cartesian plane. For each rectangle, you must keep the
Java
The idea is to implement a class to manage very basic rectangle objects in the Cartesian plane. For each rectangle, you must keep the coordinates of the lower-left corner, the width, and the height. The width and the height must be greater than or equal to 0.0.
The class must be called Rectangle. A basic skeleton of the class is given as a starting point. Your class must be complete enough for a professional use. For example, your class must provide:
a constructor with 2 parameters, the width and the height. In this case, the lower-left corner of the rectangle will be the origin of the Cartesian plane
a constructor with 4 parameters, representing the coordinates of two opposite corners of the rectangle (x1, y1, x2, and y2).
accessor and mutator methods
methods such as area, perimeter, diagonalLength, deltaMove, scale, toString, etc.
Two static methods (getDecPlaces and setDecPlaces) as a way to control the number of decimal places used in method toString to represent real numbers. By default, the number of decimal places will be 2.
Method diagonalLength returns the length of the diagonals of the rectangle. Method deltaMove has two parameters (deltaX and deltaY) and moves the rectangle, deltaX units horizontally and deltaY units vertically. Method scale has one parameter (scaleFactor) and proportionally changes the width and height of the rectangle. If scaleFactor is 1.0, then the size of the rectangle will not be affected by a call to method scale. If scaleFactor is 1.5, then the width and the height of the rectangle will be increased in 50%. The toString method returns a string representing the rectangle. According to the number of decimal places selected, the real numbers are represented with more or less precision. For example, for a rectangle of width 1.4, height 7.5, and with a lower-left corner in the origin, the toString method returns "Rectangle(0.0000, 0.0000, 1.4000, 7.5000)" if the number of decimal places is 4, and "Rectangle(0.00, 0.00, 1.40, 7.50)" if the number of decimal places is 2.
To test your Rectangle class, a user will be allowed to enter the following commands from the keyboard:
createWithTwoPoints
createWithLowerLeft
show
showWidth
showHeight
showArea
showPerimeter
showDiagonalLength
showLowerLeftCorner
deltaMove
scale
decimal
Write a second class called TestRectangle that will read the commands from the keyboard and display the result on the standard output.
Input Format
The input will consist of several lines. In each line, there is a valid command. The commands have to be processed until reaching the end-of-file.
Constraints
Unfortunately, Hackerrank does not allow us to create 2 files. In the ideal solution, we should have a file called Rectangle.java for the class that manages the rectangle objects, and another file called TestRectangle.java for the test class. Here, we will just have one file with both classes.
Output Format
The output of the show* commands. For the exact format, see the test case.
Sample Input 0
createWithTwoPoints r1 5.5 2.4 3.3 4.1 show r1 showWidth r1 showHeight r1 showArea r1 showPerimeter r1 showDiagonalLength r1 showLowerLeftCorner r1 decimal 4 createWithLowerLeft r2 -2.5 -1.3 5.78 2.54 show r2 showArea r2 showDiagonalLength r2 scale r2 0.8 show r2 deltaMove r2 0.9 -0.5 show r2
Sample Output 0
r1 = Rectangle(3.30, 2.40, 2.20, 1.70) width(r1) = 2.20 height(r1) = 1.70 area(r1) = 3.74 perimeter(r1) = 7.80 diagonalLength(r1) = 2.78 lowerLeftCorner(r1) = (3.30, 2.40) r2 = Rectangle(-2.5000, -1.3000, 5.7800, 2.5400) area(r2) = 14.6812 diagonalLength(r2) = 6.3135 r2 = Rectangle(-2.5000, -1.3000, 4.6240, 2.0320) r2 = Rectangle(-1.6000, -1.8000, 4.6240, 2.0320)
Sample Input 1
createWithTwoPoints r1 -1.245 -2.562 6.865 7.1155 createWithTwoPoints r2 -5.5 2.4 3.3 4.1 createWithTwoPoints r3 -2.56 -7.44 -0.53 -5.12 show r1 show r2 show r3 decimal 4 scale r1 1.2 deltaMove r2 0.9 -0.5 scale r3 1.25 show r1 show r2 show r3
Sample Output 1
r1 = Rectangle(-1.25, -2.56, 8.11, 9.68) r2 = Rectangle(-5.50, 2.40, 8.80, 1.70) r3 = Rectangle(-2.56, -7.44, 2.03, 2.32) r1 = Rectangle(-1.2450, -2.5620, 9.7320, 11.6130) r2 = Rectangle(-4.6000, 1.9000, 8.8000, 1.7000) r3 = Rectangle(-2.5600, -7.4400, 2.5375, 2.9000)
CODE: class Rectangle { /* Enter your code here for the Rectangle class. */ private double lowerLeftX, lowerLeftY; private double width, height; private static int decPlaces = 2; public static int getDecPlaces() { /* To be completed */ } public static void setDecPlaces(int decPlaces) { /* To be completed */ } } }
class TestRectangle { /* Enter your code here for the TestRectangle class. */ public static void main(String[] args) { /* To be completed */ } }
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