W1-Exercise OO Revision v3
W1-Exercise OO Revision v3
You need to develop a basic system that allows geometrical shapes to be contained in a
board. Imagine something like this:
The requirements are that a given board can only hold one type of shape (for example,
circle) but you could have a board containing circles and another board containing
rectangles. Other shapes (sub-classes of Shape) may be developed in the future and these
should also work with the system. The board is essentially a grid and shapes are held in
positions on the grid. There can be empty positions. You are given a project containing a
Shape superclass and a tester class. You must write a Circle, Rectangle and Board class to
pass the tests.
These are more detailed specifications of the classes. The tests and Shape class also contain
information that you need.
Classes:
Circle:
subclass of Shape
has a radius of type double (and of course colour)
equals: two circles are equal if their areas and colours are equal (see slides in w1-
pr-introduction.pdf)
Rectangle:
Subclass of Shape
Has a width and length (dimensions)
Equals: two Rectangles are equal if their dimensions and colours are equal (not area)
Board: is able to store any subclass of Shape in a two-dimensional (2D), square array. The 2D
array below is a representation of a circle in position x = 1, y = 1 (remember the array is
zero-based). This is just to help you imagine the board – your system will not do any
drawing or show a board and shapes on a screen. It purely stores the Shape objects in a
data structure.
y
x
Any given instantiation of a Board can only store one type of Shape (either Circle or
Rectangle or other sub-classes of Shape in the future). It does not store superclass Shape
directly, but you have to ensure that it only can store subtypes of Shape. You can do this by
make a Board class generic and by using the generics extends clause <S extends
Shape> .
Board fields:
A Board will be defined by its size. Knowing the size you can initialize a 2D array. A board is
always square. Tip: in the constructor you only need to pass the size and then you can
initialize the data structure to store the shapes.
You should use a 2D array of Shape that contains the actual shapes. It is complex to use
arrays of parameterized types in Java and to do so you have to disable some type checking
(not recommended). The way the Board class works prevents incorrect types being added
to the Shape array anyway. You will need other fields to maintain state and iterate over the
board.
Board methods:
add: you need to be able to add a Shape (subclass) to a given grid position on the
Board. When an object is added successfully then “true” should be returned from the
method. An unsuccessful add (out of bounds) should not crash but just return false.
remove: this method should return a subclass (specified by a generic type) of Shape.
It does not return the supertype Shape. Hint: if you are using an array of Shape then you
will need to cast a given element stored in the array to an instance of the generic-type
object (S). The remove method returns the object (Square or Rectangle) found at the
supplied position (x, y) or null if the object at that position is null or x and/or y are illegal
values.
getGrid(): this returns a 2D array of Shapes
iterator: your Board class should implement the Iterable interface. Please look
at the Java documentation for this. You should ignore the spliterator() and forEach
methods. It is probably a good idea to create an Iterator class (implements Iterator) that is
an inner class of Board. An instance of this class will be returned when the method
iterator() (as specified by Iterable) is called. Look at the lecture notes or other sources on
Iterator and its methods. You will see that you need to maintain the state between calls to
hasNext() and next().