CS 127 Assignments

Note to self: this assignment is often a tough warmup because of the complex GUI interactions. It's a great assignment, but it's heavy for a first assignment. Perhaps next time have a text-stage and a GUI-stage, or something like that that lets them start from scratch?

Assignment 1: Shapes (inheritance practice)

Shapes

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 Shape127 interface in Shape127.java, and create three implementations of Shape127: Circle127, Rectangle127, and Square127. (There are already Shape and Rectangle classes in the standard Java library, so we'll just stick "127" onto the class names to avoid conflict.) Your three classes will need to implement the methods getColor, setColor, load, and draw.

You will also need to create a ShapeWindow class. Look at CircleWindow.java for starters. CircleWindow just draws a particular green circle on the screen; your code needs to draw all the shapes in the input file. The basic structure of ShapeWindow will be the same, but you will need to modify the main method to read in the data from the file and create an array of Shape127 objects. You will also need to add code to the DrawingPanel class so that you can pass your array of shapes to a DrawingPanel object. The paintComponent method can then use this array of shapes to draw the pictures.

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. As you work on this assignment, you will confront exceptions, number-to-string conversion, string-to-number conversion, and so on. You will also have to figure out how to get proper communication going between the graphics objects and the array in which you store your information.

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 before right away so you can bring questions to class.

Have fun!