Answer:
class Rectangle{
//private attributes of length and width
private double givenLength;
private double givenWidth;
// constructor to initialize the length and width
public Rectangle(double length, double width){
givenLength = length;
givenWidth = width;
}
// setter method to set the givenlength
public void setGivenLength(double length){
givenLength = length;
}
// setter method to set the givenWidth
public void setGivenWidth(double width){
givenWidth = width;
}
// getter method to return the givenLength
public double getGivenLength(){
return givenLength;
}
// getter method to return the givenWidth
public double getGivenWidth(){
return givenWidth;
}
// method to calculate area of rectangle using A = L * B
public void calculateArea(){
System.out.println("The area of the rectangle is: " + getGivenLength() * getGivenWidth());
}
// method to calculate perimeter of rectangle using P = 2 * (L + B)
public void calculatePerimeter(){
System.out.println("The perimeter of the rectangle is: " + 2 * (getGivenLength() + getGivenWidth()));
}
}
public class MyRectangleClassProgram{
public static void main(String args[]){
//rectangle1 object is created
Rectangle rectangle1 = new Rectangle(10.0, 5.0);
//rectangle2 object is created
Rectangle rectangle2 = new Rectangle(7.0, 3.0);
//area for rectangle1 is calculated
rectangle1.calculateArea();
//perimeter for rectangle1 is calculated
rectangle1.calculatePerimeter();
//area for rectangle2 is calculated
rectangle2.calculateArea();
//perimeter for rectangle2 is calculated
rectangle2.calculatePerimeter();
}
}
Explanation:
Two file is attached: Rectangle.java and MyRectangleClassProgram.java
<span class="sg-text sg-text--link sg-text--bold sg-text--link-disabled sg-text--blue-dark">
java
</span>
<span class="sg-text sg-text--link sg-text--bold sg-text--link-disabled sg-text--blue-dark">
java
</span>