Question: Create a class called Rectangle with two instance variables width and height that are of type double. Provide a method called calculatePerimeter that calculates the

Create a class called Rectangle with two instance variables width and height that are of type double. Provide a method called calculatePerimeter that calculates the perimeter of the rectangle, and provide a method called calculateArea that calculates the area of the rectangle. Both methods should return doubles. The class should also have set and get methods for setting and getting the width and height instance variables. The set methods should verify that the length and width specified should be within the range of 0.0 and 20.0. Ensure you code is commented well, especially the set methods explaining the input restrictions and what happens if the restrictions are not met.

Create a second class called RectangleTest that will test the Rectangle Class.

Rectangle Class -

public class Rectangle {

private double width; private double height;

public double getWidth() { return this.width; }

public void setWidth(double w) throws Exception { checkDimensions(w); // check that width parameter is in range. this.width = w; }

public double getHeight() { return this.height; }

public void setHeight(double h) throws Exception { // check height parameter and update the // height instance variable.

checkDimensions(h); this.height = h; }

public void checkDimensions(double dimension) throws Exception { if (dimension <= 0.0 || dimension > 20.0) { throw new Exception("The dimensions should be between 0.0 and 20.0"); } }

public double calculatePerimeter() { return 2 * (getWidth() + getHeight()); }

public double calculateArea() { return getWidth() * getHeight(); }

}

PLEASE HELP!!!

(JAVA PROGRAMMING)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!