Comp 170 Lab 9 - Children of the Shape, March 29

Waaay back in Lab 0, we worked with the Shapes project, which contained classes Circle, Square, and Triangle. Your job for this lab is to convert this to using inheritance: you are to complete a MyShape class, and adjust Circle, Square, 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 fix.) The MyShape class is to have "as much as possible" moved to it.

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 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 belong to class MyShape for polymorphism to work, but each of the specific-shape child classes must redefine it. You must also be sure that its access is changed to public.

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.

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 Square (or one of the others). Rename the class to MyShape, delete the extra field (for Square that would be size), 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. Mutators are provided to change the other two fields.

Then, go into Circle, Square, 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 have to replace these field names with calls to the corresponding accessors that you created in MyShape.

Finally, you will 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).

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 MyShape.java, Circle.java, Square.java, and Triangle.java files.