11:07 instructure-uploads.s3.amazonaws.cc C AVA HA9.0: The Rectangle Class Desig
ID: 3908822 • Letter: 1
Question
11:07 instructure-uploads.s3.amazonaws.cc C AVA HA9.0: The Rectangle Class Design a class named Rectangle (Rectangle.java) to represent a rectangle. The class contains: Two private double data fields named width and height that specify the width and height of the rectangle. The default value for width is 2.0, and 1.0 for height. A public no-arg constructor that creates a default rectangle. A public constructor that creates a rectangle with the specified width and height. The public accessor and mutator methods for width and height A public method named getArea0 that returns the area of this rectangle. A public method named getPerimeter0 that returns the perimeter of this rectangle. Draw the UML diagram (RectangleUML.docx) for the class and then implement the class. Write a test program (RectangleTest.java) that creates a Rectangle object with width 40.0 and height 4.0 and display its width, height, area, and perimeter. Then change its width to 35.9 and height to 3.5 and display its width, height, area, and perimeter again The output should look exactly like the following: rectangle's width 40.00 and its height 4.00 rectangle' width -90 and it haight 3.10 Submit the UML diagram file RectangleUML.docx and both the source code files Rectangle.java and RectangleTest.java to the drop box HA9.0Explanation / Answer
class Rectangle
{
private double width;
private double height;
// no argument constructor
Rectangle()
{
width = 2.0;
height = 1.0;
}
// two parameter constructor
Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
// getters and setters
double getWidth(){
return width;
}
void setWidth(double width){
this.width = width;
}
double getHeight(){
return height;
}
void setHeight(double height){
this.height = height;
}
// perimeter and area
double getPerimeter()
{
return 2*(height+width);
}
double getArea()
{
return height*width;
}
public String toString()
{
return String.format("The rectangle's width is %.2f and its height is %.2f. Its area is %.2f and its perimeter is %.2f ",getWidth(), getHeight(), getArea(), getPerimeter());
}
}
class RectangleTest {
public static void main(String[] args) {
// CREATING object and printing
Rectangle a = new Rectangle(40, 4);
System.out.println("Before the change: "+a.toString());
// setting width and height and then printing
a.setWidth(35.9);
a.setHeight(3.5);
System.out.println("After the change: "+a.toString());
}
}
/*SAMPLE OUTPUT
Before the change:
The rectangle's width is 40.00 and its height is 4.00.
Its area is 160.00 and its perimeter is 88.00
After the change:
The rectangle's width is 35.90 and its height is 3.50.
Its area is 125.65 and its perimeter is 78.80
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.