L06

Due by the end of Tuesday, 23 February 2021


SUBMIT   /   CHECK

Today's Activities

Activity 1
Download the following files: Examine the code and run the program (TestRectangles) to see how it works.

Note that one of the messages about equals will be obviously wrong!

5x3 Rectangle is not equals to 5x3 Rectangle.
Override the equals method for Rectangle so that it returns the correct answers for whether the three given objects are equals.
Rectangles should be equal if they have the same height and width.

Run the program again to make sure it's giving the proper answers.

5x3 Rectangle is not equals to 2x25 Rectangle. 5x3 Rectangle is not equals to 3x5 Rectangle. 5x3 Rectangle is equals to 5x3 Rectangle.
Activity 2
Create a new class, Square, that extends Rectangle. A Square is a Rectangle, but its height and width must be equal.
NOTE: If a Square is a Rectangle, then it has a width and a height, because Rectangles have a width and a height.

Create a constructor for the class. It should be given only one value -- the length of its sides.

Remember, it is a Rectangle, so it has a width and height; they just need to be the same.

Override the toString method so that it makes an appropriate String (such as "5x5 Square").

Do not override any methods you don't need to!

Test your code by un-commenting the code for Activity 2 in TestRectangles. You'll get a bit more output, starting with:

My argument is 5x5 Square - its height is 5 - its width is 5 - its area is 25 - its perimeter is 20 And here it is: * * * * * * * * * * * * * * * * * * * * * * * * *

Note that the Squares get broken in the last bit of the program.

My argument is 5x10 Square - its height is 10 - its width is 5 - its area is 50 - its perimeter is 30 And here it is: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ...press enter... My argument is 4x2 Square - its height is 2 - its width is 4 - its area is 8 - its perimeter is 12 And here it is: * * * * * * * *
NOTE: your program might say "5x5 Square" or "10x10 Square" instead of "5x10 Square". That's fine. The important thing is that the measurements reported match the ones shown.

The next activity will fix that problem.

Activity 3
Override setHeight and setWidth for Square so that changing a Square's width will also change its height (so that it stays a square), and vice versa.
Hint: a Rectangle can set its height and width separately, so a Square just has to avail itself of those inherited methods. (But without going into an infinite recursion!) If you ask a square to change its height, then it needs to change both its height and its width. Where are its height and width stored? Which methods can manipulate them directly?

Run the program again to make sure that the Squares at the end of the program actually are squares.

My argument is 10x10 Square - its height is 10 - its width is 10 - its area is 100 - its perimeter is 40 And here it is: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ...press enter... My argument is 4x4 Square - its height is 4 - its width is 4 - its area is 16 - its perimeter is 16 And here it is: * * * * * * * * * * * * * * * * ...press enter... My argument is 5x10 Rectangle - its height is 10 - its width is 5 - its area is 50 - its perimeter is 30 And here it is: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ...press enter... 10x10 Square is not equals to 4x4 Square. 10x10 Square is not equals to 5x3 Rectangle. 10x10 Square is not equals to 5x10 Rectangle. ...press enter...

Submit this/these files:

You will be graded on the following:

  1. Rectangle @Overrides the equals method
  2. ...so that it rejects nulls...
  3. ...and non-Rectangles...
  4. ...and Rectangles with different dimensions...
  5. ...but accepts Rectangles with the same dimensions
  6. Square inherits from Rectangle
  7. ...it has no instance variables
  8. ...its constructor calls super correctly
  9. ...it has a toString method that includes its dimensions and says it's a square
  10. ...its setWidth and setHeight methods correctly change its dimensions
  11. ...it does not @Override any methods it doesn't need to
  12. All code conforms to the style guidelines for the course AND the code is concise and avoids any unwanted actions

SUBMIT   /   CHECK