CPSC 120A -- Assignment #3
More Fish (Sorry!!)
Due Thursday, November 20, 2003 by 2:50 p.m.(lab)
In this assignment you will write a Java applet to display an ocean full
of fish. Obviously the objects this program will deal with are fish so
you also need to write a class to model the fish.
The Java Fish class should model a "Pac-man" style fish similar to the ones
you drew in project 2 (without the bubbles!). The only requirements for
the fish are that they have a body, with a mouth, and a tail. To model
this your class needs the following instance data:
- an x coordinate for the position of the fish - it is up to
you to decide exactly what the x represents (it could be where the tail
starts or where the body starts, for example)
- a y coordinate for the position of the fish
- a "size" variable - I recommend just one size variable (so the
body part of the fish is circular or if, you want fish that are not
round you could make either the length or width proportional to the
one size rather than generating two different numbers for size)
- a color
- a mouth size (this should be the number of degrees of the mouth
opening)
The methods in the class should be the following:
- Two constructors
- One with no parameters - this one should generate a random size
between 30 and 100, a random x and y that guarantee the fish to be
in the applet window (you should use the size to do this - do not use
the loop technique from pre-lab 10 because it is inefficient), and
a random shade of green for the color (to do this generate a random
integer between 56 and 256 (not including 256) for the amount of
green in the color, then use the constructor in the Color class that
takes three parameters - the amount of red (use 0), the amount of
green (use the value you generated), and the amount of blue (use 0).
The constructor should set the mouth to 40 degrees.
- One with 4 parameters - the x, y, size, and color. The constructor
should initialize the object using the parameters and set the mouth
size to 40 degrees.
- Three accessor methods - one to return the x coordinate of the
position, one for the y and one for the size. Note: What you return
may not be exactly what you store (for example, if your instance
data for size stores the size of the body of the fish and if the
tail is half that size then the size of the fish is 1.5 times what
you store for size). You need to return
position information so the program using the Fish class can use
that information to place other fish in relation to the fish (without
having to do unusual calculations).
- A method setColor to set the color to a color passed in as
a parameter (so the method has one parameter - a Color object)
- A method to draw the fish
- A method openMouth that sets the mouth size to wide
open (80 degrees). The method has no parameters.
- A method closeMouth that sets the mouth size to a
partially closed mouth (16 degrees).
You need to create an applet to display the ocean full of fish. To
avoid the problem of double images, use an init method in your applet.
There is an example on page 257 but yours will be even simpler - it
should set the background color and the applet size. Everything else
is in the paint method as usual.
The applet should do the following:
- Draw at least 20 randomly sized and placed fish that are random shades of
green.
- The largest fish among the 20 (or more) should be colored red.
- The smallest fish among the 20 should be colored yellow.
- A magenta fish, twice the size of the smallest fish, should be drawn
just behind the smallest fish. The fish should have a wide open mouth.
- An orange fish, half the size of the largest fish, should be drawn
just in front of the largest fish. This fish should have a partially
closed mouth.
- A fish should be drawn in the lower left hand corner with your name
in it. Make the fish big enough for your name to fit. You may choose the
color.
You will need to construct an html file to display your applet.
Other Requirements
As usual, use good programming techniques. You should use good
variable names, constants where appropriate, whitespace, and
correct indentation and alignment of statements (emacs will do this
for you if you let it!). Documentation must include a header with
the file name, your name, the date, and a clear, complete description
of what the program does. Both the Fish class and the class
that draws the applet must be documented.
Hand In
Turn in a printed copy of your program (the source code for both
the Fish class and your applet - no need to turn in the HTML file).
Tar your
directory containing the project and email the tar file to ingram@roanoke.edu.
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 code (you may
view each other's applets, just not the code that made the applet)
or share ideas with anyone about how to write the program.