Intuitively, every square is a rectangle, so it is tempting to use inheritance for implementing a square:

public class Square extends Rectangle 

However, the devil is in the details. The Square class inherits all mutators of Rectangle, and some of them can mutate a square into a non-square shape. All these mutators need to be reimplemented so that they ensure that no such mutation occurs.

Your task is to implement the Square constructor and one mutator, the setSize method. In the setSize method, you should use the assert operator to check that the new size is again a square.

Complete the following file:

Square.java

import java.awt.Rectangle; public class Square extends Rectangle { /** Constructs a square with a given position and side length. @param x the x-coordinate of the top left corner @param y the y-coordinate of the top left corner @parameter sideLength the length of each side */ public Square(int x, int y, int sideLength) { // TODO } /** Sets the size of this method. Overrides Rectangle.setSize. @param width the new side length @param height the new side length (Precondition: width equals height) */ public void setSize(int width, int height) { // TODO } // This method tests your work. public static Square check(int x, int y, int sideLength, int width, int height) { Square sq = new Square(x, y, sideLength); try { sq.setSize(width, height); } catch (AssertionError ex) { } return sq; } }