In: Computer Science
Java Code - Please explain each step through comments. Also, make sure Main (P3_13) is separate from your class BetterRectangle. Lastly, post a picture of your code inside an IDE along with your output. My code is below, needs modification.
E9.13The java.awt.Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle ofthe Rectangle class that has getPerimeter and getArea methods. Do not add any instance variables. In the constructor, call the setLocation and setSizemethods of the Rectangleclass. In E9_13 class, provide 3test cases that tests the methods that you supplied printing expected and actual results. The test cases are: 1) One positive test case testing your method with valid inputs 2) Two negative test cases, testing your methods for invalid inputs, and printing proper error message indicating proper error handling.
Please Fix my code with comments and with instructions.
/BetterRectangle.java
import java.awt.Rectangle;
public class BetterRectangle extends Rectangle{
// default constructor
public BetterRectangle()
{
super();
}
public BetterRectangle(int x, int y, int width, int height)
{
super();
setSize(width,height);
setLocation(x,y);
}
public double getArea()
{
return(getWidth()*getHeight());
}
public double getPerimeter()
{
return(2*(getWidth()+getHeight()));
}
public static void main(String[] args) {
// test the class
BetterRectangle rect1 = new BetterRectangle(0,0,20,30);
System.out.println("Area : "+rect1.getArea());
System.out.println("Perimeter : "+rect1.getPerimeter());
}
}
//end of BetterRectangle.java
// BetterRectangle.java
import java.awt.Rectangle;
public class BetterRectangle extends Rectangle{
// default constructor that sets the left-top
coordinate to (0,0) and height and width to 1
public BetterRectangle()
{
super(); // call default
constructor of Rectangle class
setSize(1,1); // set width and
height of rectangle to 1
}
// parameterized constructor that sets the left-top
coordinate to (x,y) and width and height to passed values
public BetterRectangle(int x, int y, int width, int
height)
{
super(); // call default
constructor of Rectangle class
// validate both width and height
of the Rectangle > 0, if not throw exception
if(width <= 0 || height <=
0)
throw new
IllegalArgumentException("ERROR: Width or/and height of the
rectangle must be greater than 0.");
setSize(width,height); // set width
and height of rectangle to width and height respectively
setLocation(x,y); // set left-top
coordinate to (x, y)
}
// method that calculates the area of the rectangle
and return it
public double getArea()
{
return(getWidth()*getHeight());
}
// method that calculates the perimeter of the
rectangle and return it
public double getPerimeter()
{
return(2*(getWidth()+getHeight()));
}
}
//end of BetterRectangle.java
// BetterRectangleMain.java
public class BetterRectangleMain {
public static void main(String[] args) {
// create a rectangle with valid
inputs
BetterRectangle rect1 = new
BetterRectangle(0,0,20,30);
System.out.println("Expected area:
600.0 Actual area: "+rect1.getArea());
System.out.println("Expected
perimeter: 100.0 Actual perimeter: "+rect1.getPerimeter());
// create a rectangle with invalid
width
System.out.println("Expected error
message: ERROR: Width or/and height of the rectangle must be
greater than 0.");
try {
rect1 = new
BetterRectangle(0,0,-20,30);
}catch(IllegalArgumentException
e)
{
System.out.println("Actual: "+e.getMessage());
}
// create a rectangle with invalid
height
System.out.println("Expected error
message: ERROR: Width or/and height of the rectangle must be
greater than 0.");
try {
rect1 = new
BetterRectangle(0,0,20,0);
}catch(IllegalArgumentException
e)
{
System.out.println("Actual: "+e.getMessage());
}
}
}
//end of BetterRectangleMain.java
Output: