Assume you are writing an application that needs to work with the following types of shapes:
- ellipse (the size of which can be defined by its semimajor and semiminor axes)
- circle (the size of which can be defined by its radius)
- rectangle (the size of which can be defined by its width and height)
- square (the size of which can be defined by the length of its side)
Create a set of Java classes that represents these shapes. Your representation should enable the following functionality:
- For each shape, it should be possible to retrieve and change its size
- Given a set of shapes, it should be possible to identify all squares
- Given a set of shapes, it should be possible to identify all circles
Hint: This question is not as obvious as it may seem. It is natural to represent
rectangles with a class Rectangle
which contains fields width
and height
. Consider now what happens if someone creates an instance of
Rectangle
whose width is 60 and height is 70; however, he/she later changes
the height to be 60.