Create a class called Rectangle with two instance variables width and height tha
ID: 3600514 • Letter: C
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 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)
Explanation / Answer
RectangleTest.java
public class RectangleTest {
public static void main(String[] args) throws Exception {
Rectangle r = new Rectangle();
r.setHeight(12.0); //valid range height
r.setWidth(11.0);//valid range width
System.out.println("Area: "+r.calculateArea());
System.out.println("Perimeter: "+r.calculatePerimeter());
try{
Rectangle r1 = new Rectangle();
r1.setHeight(24.0); //Invalid range height
r1.setWidth(11.0);//valid range width
System.out.println("Area: "+r1.calculateArea());
System.out.println("Perimeter: "+r1.calculatePerimeter());
}catch(Exception e){
System.out.println(e);
}
try{
Rectangle r2 = new Rectangle();
r2.setHeight(2.0); //valid range height
r2.setWidth(112.0);//Invalid range width
System.out.println("Area: "+r2.calculateArea());
System.out.println("Perimeter: "+r2.calculatePerimeter());
}catch(Exception e){
System.out.println(e);
}
}
}
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();
}
}
Output:
Area: 132.0
Perimeter: 46.0
java.lang.Exception: The dimensions should be between 0.0 and 20.0
java.lang.Exception: The dimensions should be between 0.0 and 20.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.