Comp 170 Lab 8 - Children of the Shape

Waaay back in Lab 0, we worked with the Shapes project, which contained classes Circle, Rect, and Triangle. Your job for this lab is to convert this to using inheritance: you are to complete a MyShape class, and adjust Circle, Rect, and Triangle so they all inherit from (extend) class MyShape. (Note that you must use the name "MyShape" rather than "Shape" because the latter name is used somewhere within class Canvas and it's waaay too much work to resolve the name conflict.) The MyShape class is to have "as much as possible" moved to it (but no more). You are also to add a class Square (that does the appropriate thing) that derives from Rect

Here is the original version of shapes (with some minor changes), which you are to use as your starting point.

MyShape should contain he following common fields

You will need to create an accessor in class MyShape for each of these fields: getXPos(), getYPos(), getColor(), and getVisible(). All of these will be needed below (in particular, in the specific-shape implementations of draw()).

MyShape should contain the following methods; these are the ones that the specific-shape child classes will not redefine:

The draw() method must be present in class MyShape for polymorphism to work, although the body should be empty. Each of the specific-shape child classes must redefine it (to draw that specific shape!). You must also be sure that its access is changed to public:
      public void draw() {}

The specific-shape classes will each need the following shape-specific fields:

Also, the specific-shape classes will need to retain the following specific-shape methods (that will not be in MyShape):

Finally, the specific-shape classes will need to contain the "real" implementation of draw() for each shape (retained from the original classes).

You don't have as much to do here as it might at first appear. To create the MyShape class, create the class with the new class button, delete everything BlueJ gives you, and paste in the entire contents of Rect (or one of the others). Rename the class to MyShape, delete the specific-shape field(s) (for Rect those would be width and height), and delete any unneeded methods (any methods where the MyShape version suffices). You also need to fix the constructor. The existing constructor takes the x and y coordinates as parameters; these should be parameters to the MyShape constructor (called as super(x,y)). Mutators are provided to change the remaining fields.

Then, go into Circle, Rect, and Triangle, and change so they each extends MyShape, and delete everything unneeded. Don't delete draw, or the shape-specific fields and methods listed above.

The specific-shape classes as written all refer to fields xPosition, yPosition, color, and isVisible (in the draw method if nowhere else). These fields are now private to MyShape; in the specific-shape child classes you will therefore have to replace these field names with calls to the corresponding accessors that you created in MyShape (eg getXPosition()). 

You will also need to tweak the constructors. The specific-shape-class constructors should, like MyShape, take x and y as parameters, and use them to call super(x,y).

Finally, for class Square, define it to extend Rect (I've started it for you). It will have no new data fields. It will, however, contain a constructor, and one method: changeSize(int s). A Square is just a Rectangle with height and width created equal.

I've given you a Picture class. Note that it creates an Arraylist of MyShape, using specific shapes, and then draws the picture as a unit, using the fact that most of the methods are inherited. It will not compile until you implement the changes above to MyShape and the specific-shape child classes. You should not make any changes to Picture itself.

Email me your completed project.