Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(The Rectangle class) Following the example of the Circle class in Section 8.2,

ID: 3555309 • Letter: #

Question

(The Rectangle class) Following the example of the Circle class in Section 8.2,

design a class named Rectangle to represent a rectangle. The class contains:

- Two double data fields named width and height that specify the width and

height of the rectangle. The default values are 1 for both width and height.

- A no-arg constructor that creates a default rectangle.

- A constructor that creates a rectangle with the specified width and height.

- A method named getArea() that returns the area of this rectangle.

- A method named getPerimeter() that returns the perimeter.

Draw the UML diagram for the class and then implement the class. Write a test

program that creates two Rectangle objects

Explanation / Answer

Please Rate: Thanks

//Craete class

public class Rectangle {
    private double Width;
private double Height;

//Create empty constructor and define default values
Rectangle()
{
    Width=1.0;
    Height=1.0;
}

//Parameterized constructor
Rectangle(double newWidth, double newHeight)
{
Width=newWidth;
Height=newHeight;
}

//Method to get area
public double getArea()
{
return Width*Height;
}

/Method to get parementer
public double getPerimeter()
{
return 2*(Width+Height);
}

//Now we write our fetching functions:
public double getWidth(){
return Width; //simply return the width
}
public double getHeight(){
return Height; //simply return the height
}
//Now we write the functions that return area and perimiter
}

-----------------------------------------------------------------------------------------------------

import java.util.Scanner;

//create class to test Rectangle class
public class TestRectangle extends Rectangle{

public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
double width;
double height;

char custom; //a control variable for determining default

System.out.println("Would you like to customize your rectangle?");
custom=input.next().charAt(0);

if(custom == 'y')

{

//Display the customized values

Rectangle r2=new Rectangle();

System.out.println("The height is: " +r2. getHeight());
System.out.println("The width is: " + r2.getWidth());
System.out.println("The perimeter is: " +r2.getPerimeter());
System.out.println("The area is : " + r2.getArea());
}
else
{

Rectangle r=new Rectangle(4,40);
Rectangle r1=new Rectangle(3.5,35.9);
System.out.println("The height is: " + r.getHeight());
System.out.println("The width is: " + r.getWidth());
System.out.println("The perimeter is: " +r.getPerimeter());
System.out.println("The area is : " + r.getArea());

System.out.println("The height is: " + r1.getHeight());
System.out.println("The width is: " + r1.getWidth());
System.out.println("The perimeter is: " +r1.getPerimeter());
System.out.println("The area is : " + r1.getArea());

}
}
}

-------------------------------------------------------------------------------------------------------

Output1:

Normal 0 Clean Clean false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 Would you like to customize your rectangle?
n
The height is: 40.0
The width is: 4.0
The perimeter is: 88.0
The area is : 160.0
The height is: 35.9
The width is: 3.5
The perimeter is: 78.8
The area is : 125.64999999999999
BUILD SUCCESSFUL (total time: 3 seconds)

Output2:

Would you like to customize your rectangle?
y
The height is: 1.0
The width is: 1.0
The perimeter is: 4.0
The area is : 1.0
BUILD SUCCESSFUL (total time: 3 seconds)
-------------------------------------------------------------------------------------------------------

Normal 0 Clean Clean false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

UML Diagram:

-          Width of rectangle

-          Height of rectangle

+Rectangle()

+ Rectangle( height, width)

+getwidth()

+setwidth()

+getHeight()

+setHeight()

+getPerimeter()

+getarea()

-                      Create constructor with default values width and height is 1

   Create parameterized constructor stores the width and height

            Get width of rectangle

            Set width of rectangle

Get Height of rectangle

Set Height of rectangle

Get perimeter of rectangle

            Get area of rectangle