Question:
Consider the following class definition:
public class Rectangle {
private double length;
private double width;
// Constructor
// Getter and Setter methods
// Method to calculate the area of rectangle
// Method to calculate the perimeter of rectangle
// Method to check if the rectangle is a square
// Main method for testing
}
a) Complete the class definition above by implementing the following:
A constructor that takes in two parameters length
and width
and initializes the respective instance variables.
Getter and Setter methods for the instance variables length
and width
.
A method named calculateArea
that calculates and returns the area of the rectangle using the formula area = length * width
.
A method named calculatePerimeter
that calculates and returns the perimeter of the rectangle using the formula perimeter = 2 * (length + width)
.
A method named isSquare
that checks if the rectangle is a square. A rectangle is considered a square if its length
and width
are equal. The method should return true
if the rectangle is a square, and false
otherwise.
b) Write the code for the Rectangle
class's constructor
, getter
and setter
methods, calculateArea
method, calculatePerimeter
method, and isSquare
method.
c) Write a complete main
method to test the Rectangle
class. Create two objects of Rectangle
class, one with length 4 and width 5, and the other with length 6 and width 6. Print the area and perimeter of both rectangles, and check if they are squares.
Answer:
The modified class definition is as follows:
public class Rectangle {
private double length;
private double width;
// Constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Getter and Setter methods
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
// Method to calculate the area of rectangle
public double calculateArea() {
return length * width;
}
// Method to calculate the perimeter of rectangle
public double calculatePerimeter() {
return 2 * (length + width);
}
// Method to check if the rectangle is a square
public boolean isSquare() {
return length == width;
}
// Main method for testing
public static void main(String[] args) {
// Create two objects of Rectangle class
Rectangle rectangle1 = new Rectangle(4, 5);
Rectangle rectangle2 = new Rectangle(6, 6);
// Print the area and perimeter of both rectangles
System.out.println("Rectangle 1:");
System.out.println("Area: " + rectangle1.calculateArea());
System.out.println("Perimeter: " + rectangle1.calculatePerimeter());
System.out.println("\nRectangle 2:");
System.out.println("Area: " + rectangle2.calculateArea());
System.out.println("Perimeter: " + rectangle2.calculatePerimeter());
// Check if the rectangles are squares
System.out.println("\nRectangle 1 is a square: " + rectangle1.isSquare());
System.out.println("Rectangle 2 is a square: " + rectangle2.isSquare());
}
}
Explanation:
a) The class definition has been modified to include the necessary elements:
A constructor is added which receives length
and width
as parameters and initializes the respective instance variables length
and width
.
Getter and Setter methods are added to access and modify the values of length
and width
.
The calculateArea
method calculates the area of the rectangle by multiplying the length
and width
and returns the calculated value.
The calculatePerimeter
method calculates the perimeter of the rectangle using the formula 2 * (length + width)
and returns the calculated value.
The isSquare
method checks if the rectangle is a square by comparing the length
and width
. If they are equal, it returns true
, otherwise, it returns false
.
b) The code for the Rectangle
class's constructor
, getter
and setter
methods, calculateArea
method, calculatePerimeter
method, and isSquare
method is written as shown above.
c) The main
method is implemented to test the Rectangle
class. Two objects of the Rectangle
class are created, one with a length of 4 and width of 5, and the other with a length of 6 and width of 6. The area and perimeter of both rectangles are printed, and then it is checked if they are squares by calling the isSquare
method. The results are printed accordingly.