Lab 5 Post-Lab Assignment: A Dart Class
Due Friday, October 6, 2006
Write a Dart class that represents a graphical dart object (represented
by an "X" as we did in lab 3). The attributes of
the dart will be its position (the x and y coordinates of the center
- where the two lines in the X intersect - this is probably different from
what you did in lab) and
color. Hence, the class should store
the following information as
instance variables:
- x coordinate of the center of the "X"
- y coordinate of the center of the "X"
- color (this must be of type Color)
Your Dart class should provide the following
methods:
- A constructor that takes just one parameter for the color of
the dart (this should be of type Color). The constructor should
initialize the color instance variable to be the color passed in
as a parameter. The
position coordinates should default to 0.
- A toss method that "tosses" the dart - that is, it
assigns the dart a random position (random x and y) on a dart board.
The method needs to have one integer parameter which represents
the size of the dart board (the assumption is that the board is
a square). The method will just generate random values for the
x and y so the the "dart" will be on the board (so both the
x and y values should be in the range 0 to the board size). It will
not return any values. Hence the heading for the method is
as follows (you fill in the blanks):
public _________ toss ( _________ _______________ )
^ ^ ^
| | |
return parameter parameter
type (or type name
void)
Because you need to generate random numbers your
class needs a random number generator. The generator object should be
a class (static) variable declared and instantiated up with your
instance variables (not inside the toss method) as follows:
private static Random generator = new Random();
Note that all this methd does is toss the dart - it doesn't draw it.
- A draw method that draws the dart at its x,y coordinate in its
color. Remember that the x and y denote the center
of the dart. Make your dart 9 pixels high and 9 pixels wide.
Note that your draw method
should have one parameter - a Graphics object (the page you
are to draw the dart on). See the Snowman and Balloon example.
- getX() and getY() accessor methods that return the values
of x and y, respectively.
When your Dart class compiles, write an applet DartBoard that does
the following:
- draws a dart board (as you did in lab 3)
- uses your Dart class to create 3 dart objects of different colors
(you can either create your own Color objects or use some of
the Color objects, such as Color.blue, defined in the Color class)
- for each dart -- toss the dart, draw it,
get its x and y coordinates and use
those to compute the distance from the dart to the center of the
dartboard (you did distance between points in lab 3). Use
drawString to write the 3 distances somewhere on the dartboard in a
color that shows up!
Of course,
you also need an html file so you can run the applet. In the html
file make the applet width 400 and height 400 (use constants!).
SUGGESTION: First
just get your dartboard drawn in your applet then go back and add
the darts.
Be sure your program is appropriately documented.
Hand-in: Hard copy of your Dart class and your DartBoard applet.
Also tar your post lab directory that contains all of your files (.java,
.class, and .html) and email the tar file to your instructor with
cpsc120 post5 in the subject line.