CPSC 120 Assignment #2
Due, Friday October 14, 2004 by 5:00 p.m.
In Lab 3, you created an applet to play a game of darts. Clearly that
implementation was very simplistic - it provided a visual representation,
but didn't even keep score. For this assignment, you are going to rework
the darts applet to work with classes and conditional statements to provide
a more robust (and fun!) game.
There are two major types of objects that we want to use for this game:
Darts and DartBoards.
The Dart Class:
Just like the lab, we will represent the dart with an "X"
on the screen. Hence the Dart class should have the following:
The DartBoard Class:
For this program, a dartboard consists of 4 concentric circles,
with a bullseye (inner-most circle) that has a radius of at least 40.
The DartBoard class should have:
- Instance data that specifies the size of the board (the diameter
of the largest ring) and the interval
(difference in size of rings).
- A constant representing the minimum radius of the inner circle (in
this case the 40).
- A constructor that initializes the instance data. Additionally,
the constructor should verify that the interval is not too big.
(The interval is too big if the smallest circle has a radius smaller than 40.)
If the interval specified is too big, the constructor should set the
interval to the largest interval. For example, if the size of the
board is 300 and the interval is 70 then the inner circle will be
size 90 (radius 45) which is ok. On the other hand, if the size of the
board is 300 and the interval is 80 then the inner circle will be
size 60 (radius 30) which is too small. Hence in that case, the
interval should be set to 73 (figure out how this is calculated in
general).
- A method to draw the board. Your board should have have 4 concentric
circles that are different colors.
- A method that takes a location (x, y) and returns a score.
The scoring works as follows. Innermost ring = 50 points,
2nd ring = 25 points, 3rd ring = 10 points, 4th ring = 5 points,
off the board = 0 points.
Click here for a note on positioning of the
dart board.
Your applet should draw a dartboard, throw 3 darts, and report the
cumulative score.
Some Hints: Do your work in small chunks testing each before going
to the next. A suggested order is as follows.
-
First write the Dart class and an applet that draws
the 3 random darts.
- Next write a basic DartBoard class that has a
simple constructor (with no testing for correct size) and draws the board.
Add statements in your applet to test the class (try several different
values for size and interval).
-
Next add the conditional statements to the DartBoard constructor to
test for correct size. Be sure
to test this by modifying the applet to try to construct dart boards
that violate the size criteria.
- Finally add the method to the DartBoard class that
calculates the score for a given position.
Note that which ring the dart is in is determined by its distance
to the to the origin. So you need to compute the distance
of the dart from the center of the dart board (you had the distance
formula in an earlier lab).
Your statement to assign points (determine which ring the
dart is in) should be a cascading if.
- Add calls to this method
in your applet to compute the number of points for each dart. Your
applet will also need to add the points up and draw the result on
the applet window, appropriately labeled.
Additional requirements:
- Use good programming style -- meaningful variable names, named constants,
use of white space, proper indentation (note the proper style for
indenting if statements). See the requirements for the last assignment.
- Document your program. Have header documentation at the top of each file
that gives your name, the date, the file name, and a description of the
purpose of the program or the class. Have header documentation at the
beginning of each method describing what the method does, including what
it takes as parameters and what it returns. Have internal documentation that
delineates and succinctly (and clearly) indicates what each major part of
the code is doing.
Grading:
Your program will be graded both on style and correctness. Style includes
adherence to rules for naming of identifiers and use of white space;
it also includes coding style such as the absence of unnecessary code
(extra comparisons in if's or extra assignments); it includes appropriate
documentation. Style will be 20% of the grade on the assignment.
Correctness includes meeting all the specifications of the assignment.
A program that does not compile has an automatic deduction of 70%;
one that does not run has an automatic deduction of 40% no matter how "close"
it is to compiling or running. Come see me if you have problems with
the assignment. It is important that you get in the habit of producing
correct programs!
Academic Integrity Reminder!!! Programming assignments are to be your own work.
You may get help on the specifics of the assignment from no one except the
instructor. You may not show your program to anyone or look at anyone
else's program or share ideas with anyone about how to write the program.
Hand IN: A printed copy of your program files (the 3 Java files -
you do not need to hand in the HTML file). Tar the .class, .html, and .java
files and email the .tgz file to your instructor with a subject of
cpsc120 project2.