Squares public class Square extends Rectangle { In this exercise, we are going to model some behaviors // Call to the Rectangle constructor of a square. Since the square object extends the pubtic Square(double sideLength) { Rectangle object, we see that o lot of the information } we need is stored in the superclass and we will need to //Set both the width and height in the superclass access it using the super keyword, public void setSidelength(double sidelength) { Your job is to complete the Square class, as specified } within the class. Upon completion, thoroughly test out your code using the Squaretester class: // Get the width and/or the height from the superclass pubtic double area() } // Get the width and/or the height from the superclass public double perimeter() } Square } } 5 points 5 Status: Not Submitted In this exercise, we are going to model some behaviors of a square. Since the Square object extends the Rectangle object, we see that a lot of the information we need is stored in the superclass and we will need to access it using the super keyword. Your job is to complete the Square class, as specified within the class. Upon completion, thoroughly test out your code using the SquareTester class.
The Correct Answer and Explanation is:
To complete the Square class that extends the Rectangle class in Java, we need to implement the necessary constructors and methods while leveraging the properties of the superclass. Below is a suggested implementation of the Square class:
public class Square extends Rectangle {
// Constructor for Square, calling the Rectangle constructor
public Square(double sideLength) {
super(sideLength, sideLength); // Set both width and height
}
// Method to set the side length of the square
public void setSideLength(double sideLength) {
// Call the superclass's setWidth and setHeight methods
super.setWidth(sideLength);
super.setHeight(sideLength);
}
// Method to calculate the area of the square
@Override
public double area() {
return super.getWidth() * super.getHeight(); // or sideLength * sideLength
}
// Method to calculate the perimeter of the square
@Override
public double perimeter() {
return 4 * super.getWidth(); // or 4 * sideLength
}
}
Explanation
- Inheritance: The
Squareclass extends theRectangleclass, meaning that it inherits the properties and methods ofRectangle. In a square, the width and height are equal, so any method that interacts with these properties should reflect this relationship. - Constructor: The
Squareconstructor takes a single argumentsideLength. It invokes the constructor of the superclass (Rectangle) usingsuper(sideLength, sideLength)to set both the width and height to the same value, reflecting the square’s properties. - Set Side Length: The
setSideLength(double sideLength)method updates both the width and height of the square. This is important because a change in the side length should simultaneously affect both dimensions. - Area and Perimeter: The methods
area()andperimeter()override the methods from theRectangleclass. The area of a square is calculated by multiplying the width by the height (or simply squaring the side length), while the perimeter is four times the length of a side. These methods utilize the inheritedgetWidth()method from theRectangleclass to ensure accurate calculations. - Testing: To ensure that the
Squareclass behaves as expected, it should be thoroughly tested using aSquareTesterclass. This class can create instances ofSquare, invoke the methods, and verify that the area and perimeter are calculated correctly based on various side lengths.
By following this structure, the Square class effectively utilizes inheritance, ensuring clean and maintainable code that respects the principles of object-oriented programming.