123k views
5 votes
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 then implement the class. Write a test program that create two Rectangle objects - one with width 4 and height 40, and the other with width 3.5 and height 35.9.
Display the width, heigth, area, and perimeter of each rectangle in this order.

1 Answer

3 votes

Answer:

function Rectangle(width=1, height= 1 ) {

this.width= width;

this.height= height;

this.getArea= ( ) = > {return width * height};

this.getPerimeter= ( ) = >{ return 2( height + width)};

}

var rectangleOne= new Rectangle(4, 40)

var rectangleTwo= new Rectangle(3.5, 35.9)

console.log(rectangleOne.width, rectangleOne.height, rectangleOne.getArea( ), rectangleOne.getPerimeter( ) )

console.log(rectangleTwo.width, rectangleTwo.height, rectangleTwo.getArea( ), rectangleTwo.getPerimeter( ) )

Step-by-step explanation:

This is a Javascript class definition with a constructor specifying arguments with default values of one for both width and height, and the getArea and getPerimeter methods.

User John Wheal
by
8.3k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.