CSC 333
Homework 3
Assigned Sept. 28, due Oct. 7
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.
- get your applet's paint() method to draw a box on the screen.
- 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.
- 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.
- 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.
- Since Box and Arrow have in common the ability
to paint() themselves, define an interface (named, say,
GraphicObject or Paintable) to represent this fact.
- 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.
- 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