Assignment: Shapes (inheritance practice)

For this assignment, you will write a program that will read a text file containing descriptions of a bunch of shapes, and then open a window displaying those shapes.

For example, suppose your text file contains the following:

3
circle
255 0 0
100 200 60
rectangle
180 0 180
100 400 80 120
square
0 255 0
300 450 150

The very first line of the file indicates how many shapes will be coming. For each shape, the first line identifies the type of shape, the second line describes the RGB (red/green/blue) color of the shape, and the third line contains shape-specific information. If your program encountered this example, it would need to display a red circle with radius 60 and centered at (100,200), a purple rectangle with upper left corner (100,400), width 80, and height 120, and a green square with upper left corner (300,450) with edge width 150. In addition, each shape's area should be displayed in the center (more or less) of the shape.

There are many ways to write such a program, of course. For this assignment, you're going to use an existing Shape201 interface in Shape201.java, and create three implementations of Shape201: Circle201, Rectangle201, and Square201. (There are already Shape and Rectangle classes in the standard Java library, so we'll just stick "201" onto the class names to avoid conflict.) Your three classes will need to implement the methods getColor, setColor, load, draw, and area.

For the actual drawing of these shapes, make use of the class Canvas, which we use at Carleton for making graphics programming more straightforward.. You may have used this class before if you have taken CS 117 here at Carleton. Download your own copy of Canvas.java to the directory where you will be doing your work. Here is the documentation for the Canvas, and here is a sample program that uses Canvas to do some drawing.

You will also need to create a ShapeMain class. This class has a main method that will construct and show a Canvas object, read in the data from the file, and draw each shape on the canvas while printing out its area in the center.

You may assume that the text file is correctly formatted.

Advice

In many ways, this is a very simple program. On the other hand, there are some tricky details. It may have been a while since you have done any Java programming. You may be unfamiliar with Canvas. And so on.

To manage the complexity of the task, you should start by making an incremental development plan. For example, the first version of your program might simply open the specified file and print its contents to standard output. The second version could include a loop that runs through the shapes in the file, printing only the shape-name line and terminating at the end of input. The third could parse the color integers into separate int variables, and then print those out. This kind of planned sequence of small steps, executed with discipline, can make a big job manageable. After each step, make sure to compile, test, and make backup copies of your code.

One more thing: get started right away so you can bring questions to class.

Have fun!

Turning in your work

Zip up all your code into a single file before submitting. On the department Linux systems, you can do this at a command prompt by typing

zip shapes.zip *

which will create a single file called shapes.zip. This is the file that you should submit.