Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Create class Rectangle

Create instance variable to hold rectangle height and width


double height;
double width;

Provide constructor with two parameters used to initialize each instance variable

public Rectangle(double width,double height) {


if(width <0 || width > 20 || height <0 || height > 20){
throw new IllegalArgumentException("Invalid dimensions entered"); }

Provide get methods to return value of each instance variables

public double getHeight(){


return height;

public double getWidth(){


return width;

Provide set methods that set instance variables to new values.

Public void setHeight(doubleHeight){


if (height <0 || height > 20){
throw new IllegalArgumentException("Height out of range");

Public void setWidth(doubleWidth){


if(width <0 || width > 20){
throw new IllegalArgumentException("Width out of range");

Provide a method to calculate perimeter


public double calculatePerimeter(){
return 2*(height + width);

Provide a method to calculate area


public double calculateArea(){
return height*width;

Create class RectangleTest


use main method
Test for Invalid height
try{
Rectangle r1 = new Rectangle();
r1.setHeight(24);
r1.setWidth(10);
print(CalculateArea);
print(CalculatePerimeter)
}
catch(IllegalArgumentException e){
print(e.getMessage);
}

Test for invalid width


try{
Rectangle r2 = new Rectangle();
r2.setHeight(9);
r2.setWidth(123);
print(CalculateArea);
print(CalculatePerimeter)
}
catch(IllegalArgumentException e){
print(e.getMessage);
}

You might also like