CSC 333

Homework 3

Assigned Sept. 28, due Oct. 7

[sample program output] Part of Homework 2 was an "information inventory" listing the kinds of objects your program will need to deal with. Some of these objects (like boxes and arrows) need to be depicted visually on the screen; in this assignment you'll define classes for each of these kinds of objects and specify how they appear on the screen. To test the classes you've defined, write a main method that simply creates a couple of boxes and arrows at specific locations; the paint() method of your main class should then tell them all to draw themselves (e.g. by calling a paint() method on each of the objects). The result should be something similar to the picture at right.

I recommend doing this project in small steps, e.g.

  1. get your applet's paint() method to draw a box on the screen.
  2. move the box-drawing code into the paint() method of a newly-defined class Box, have your applet's init() method create a Box and store it in an instance variable, and have your applet's paint() method tell that Box to paint itself.
  3. have your applet's init() method create two instances of Box (at different locations), and have your applet's paint() method tell both of them to paint themselves.
  4. add a class named Arrow, have your applet's init() method create one of these as well (pointing from one Box to the other), and have your applet's paint() method tell it to paint itself too.
  5. Since Box and Arrow have in common the ability to paint() themselves, define an interface (named, say, GraphicObject or Paintable) to represent this fact.
  6. replace the three instance variables (two Boxes and an Arrow) with a single Vector which will contain all the objects that need to be told to paint themselves. Note that all the elements of the Vector will satisfy the interface defined above.
  7. Define some different kinds of Boxes, different kinds of Arrows, etc. Watch out for natural inheritance relationships and opportunities to factor out duplicate code from two related classes into their common ancestor.

Last modified:
Stephen Bloch / sbloch@adelphi.edu