6.8k views
4 votes
Implement a class Rectangle. Provide a constructor to construct a rectangle with a given width and height, member functions get_perimeter and get_area that compute the perimeter and area, and a member function void resize(double factor) that resizes the rectangle by multiplying the width and height by the given factor. (Page EX9-3).

1 Answer

2 votes

Answer:

//class declaration

public class Rectangle{

//declare the instance variables - width and height

double width;

double height;

//the constructor

public Rectangle(double width, double height){

//initialize the width of the rectangle

this.width = width;

//initialize the height of the rectangle

this.height = height;

}

//method get_perimeter to compute the perimeter of the rectangle

public double get_perimeter(){

//compute the perimeter

//by using the formula 2(width + height)

double perimeter = 2*(this.width + this.height);

//return the perimeter

return perimeter;

}

//method get_area to compute the area of the rectangle

public double get_area(){

//compute the area

//by multiplying the width and height of the rectangle

double area = this.width * this.height;

//return the area

return area;

}

//method resize to resize the rectangle

public void resize(double factor){

//resize the width of the rectangle

//by multiplying the width by the factor

this.width = this.width * factor;

//resize the height of the rectangle

//by multiplying the height by the factor

this.height = this.height * factor;

}

} //end of class declaration

Step-by-step explanation:

The code above has been written in Java. In contains comments explaining every part of the program.

User Hacksoi
by
4.5k points