Lab 9: First try at polymorphism

The goal of this lab is to write several classes that have the same interface and thus can be used interchangeably.

1. Setup

As in previous assignments, move into your cs235 directory. Make a directory for this assignment, and change into it.

cd cs235
mkdir lab9
cd lab9

Copy the files ShapeDriver.java and Shape.java from the public directory for this class:

cp /homeemp/tvandrun/pub/235/Shape.java .
cp /homeemp/tvandrun/pub/235/ShapeDriver.java .

2. Introduction

If two or more classes have a common set of public methods (that is, they share or have an overlapping interface), then in theory they can be used interchangeably. This lab will lead you through steps that will show you how Java allow this in practive.

The example in this lab is a program using various kinds of shapes. (Imagine a graphics editor, like MS Paint.) The program may have many shape objects which it will need to draw and calculate area and perimeter for. It will need a set of operations that will be defined for all kinds of shapes. This is where subtyping comes into good use.

Open the files you copied, if you haven't already. An interface Shape is already written for you. You will be writing several classes that implement this interface. You will not change the interface at all; the only changes you will make to the driver will be to un-comment code as you write new things to be tested.

Notice that the driver has the line

Shape currentShape = null;

This means the variable currentShape has type Shape, so it can refer to anything that is a Shape. Since Shape is not a class, there aren't any instances of it. However, any instance of any class that implements the Shape interface is also a shape, because those classes are subtypes of type Shape. At compile time, the variable currentShape is known to have type Shape (this is called its static type), but when you run the program, that variable will actually refer to an object that has a more specific type (this is called its dynamic type).

3. Circle class

Open a new file Circle.java And write a class Cricle. Declare it to implement the interface Shape.

public class Circle implements Shape {

This declaration does two things:

Think about what instance variables you'll need to keep information about a specific circle. The call to the constructor in the driver expects to be passing the radius to the Circle it's creating, if that gives any clue. Write an appropriate constructor. Finally, write methods area and perimeter, as the interface expects. (Disregard the fact that for circles, the perimeter is properly called the circumference).

Use proper access modifiers (that is, public and private).

Then un-comment the part of the switch statement that deals with circles and test (don't uncomment the rest of the switch--- it refers to rectangles and right triangles, which you haven't done yet.

Also... document before going on. Don't leave it all for the end.

4. Rectangle and RightTriangle classes

Once you have circle working right, do the same thing for new classes Rectangle and RightTriangle. Then un-comment the rest of the switch and test.

5. An array of shapes

This part doesn't require you to do any more programming. Just uncomment the next section in the driver, the part using an array of shapes. Notice that since every circle, rectangle, and right triangle is also a shape, a shape array can hold any of them. Test this out and make sure you understand what's going on before moving on.

6. A tri-shape

Now, what if we made a really complicated shape, one that is composed of other shapes, say

Call a tri-shape a shape that is composed of three other shapes. Write a new class TriShape to model such a shape. Its component shapes should be anything that implements the Shape interface. The constructor should receive three Shapes as parameters. In implementing area and perimeter, you can assume that the shapes do not overlap nor share any edge.

Uncomment the rest of the driver and test.

7. Turn in

Create the script file as before (cat, rm, compile, and run). Print it out and hand in a hard copy.

 > a2ps -P sp (the name of the script file)

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)


Thomas VanDrunen
Last modified: Mon Oct 30 16:36:02 CST 2006