CPSC 120 Assignment #3
Critters and Flowers
Due Friday October 12, 2007 by 4:00 p.m.
In this assignment you will create classes for two graphical objects:
Critters and Flowers. These objects will be combined into a Garden scene.
Create a subdirectory of your Assignments directory named assign3
for your work.
The Critter Class
Critter.java should define a class that models a bug.
A critter will be defined by the following attributes:
- A location - the x and y coordinates of the position of the
bug (upper left corner) on the graphics page
- A width and a height
- A color
- An eye size (diameter of each eye)
- Size of the gap between the eyes
The Critter class should have the following methods:
- A constructor that takes only two parameters - the x and y coordinates
for the location. The constructor will need to randomly generate
other attributes as follows:
- The width should be a random value between 50 and 70 pixels.
- The height should be a random value between 60 and 100 pixels.
- The color should be a random color.
- The eye size should be a random value between 10 and half the
width of the critter.
- The gap between the eyes should be a random value between 0 and
half the critter's width.
Because you are using random numbers you need to add the following
up where your instance variables are declared (not in the constructor)
private static Random generator = new Random();
Of course you also need to import java.util.Random at the beginning
of the file (after the package statement).
- A draw method that draws the critter.
Draw the critter in the following order. (Note: One way to do this is to write
a separate method to draw each of these body parts - your draw
method would just call each of the other methods. The graphics
page would need to be a parameter to each method.)
- First draw the critter's legs.
- Second draw the critter's body in the appropriate color.
- Last draw the critter's eyes.
To draw a critter's legs:
- A critter has six black legs, three to each side of its body.
- For the purposes of drawing, all of the legs start at the
center of the critter. (Note: part of each leg will be covered
when you draw the body.)
- The critter's legs extend 10 units to either side of the
bounding rectangle of the critter's body.
- There are equal amounts of space between the critter's legs and
from the top and bottom of the critter's bounding rectangle.
Draw the critter's eyes
- The critter's eyes are white with black pupils.
- The size of the critter's eyes are determined by the instance
- The pupil size is always 3/4 the size of the eye.
- The critter's eyes are separated by a gap (an instance variable).
- The critter's eyes are always positioned exactly 10 units from the
top of the critter.
- The critter's eyes are centered across the width of the critter.
- The critter's eyes are round.
- Getter methods for each instance variable.
- Setter methods for the width, height, the color, eye size,
and gap between the eyes. The setter methods for width, height,
eye size and gap should ensure that the values are at least as
large as the minimum values used in the constructor (the width
should be at least 50, the height at least 60, the eye size at
least 10, and the gap at least 0) If
the parameter passed in is too small the attribute should be set
to the minimum (the best way to do this is using a method in the Math class
but there are other ways).
Following these guidelines, critters should look something like one of
these three examples:
The Flower Class
Flower.java defines a class that models a flower. A flower is
specified as follows:
- When created, the client should specify a bounding box for the
flower (x,y, width, height) as well as a color for the flower.
- Drawing the flower should produce at least 4 partially overlapping
circles that extend to all 4 borders of the bounding box.
- Drawing the flower should also produce a circle that is a noticeably
different color than the rest of th flower and is
centered in the bounding box, taking up half the area. NOTE: To
get a different color for the center you can look at some methods
in the Color class (use Java Docs link posted on the course web page)
or you can look back at lab 4 for ideas.
- Finally, the flower should also have a stem, drawn in a shade of green,
that is horizontally centered on the flower and extends twice the height below the
Critters sitting in the Garden
To create the garden you need both a panel class (GardenPanel.java) and
an application with a main method (GardenApp.java).
GardenPanel.java should create a custom panel that can be used for
displaying your critters (model your panel after the Splat panel from p187, or
one of the panels from lab5). The panel will display at least two flowers
of different colors and sizes and
three critters - two "parents" and one "child." The child's attributes
will be an average of the two parents. So, the constructor for the
panel should do the following:
The paintComponent method should call draw methods in the classes
to draw the critters and flowers. Use the drawString method to label
the child critter so it is clear which one is the child. Also write
your name in a corner of the garden scene.
- Set the size and background color of the panel.
- Create two "parent" critters and one "child" critter.
- Use the getter method to get the height of each parent critter and
compute the average height. Do the same for the weight, color, eye
size, and gap between eyes. (NOTE: Think about how you will find
the "average" color!) Use the setter methods to set the attributes
of the child to these average values.
GardenApp.java is an application that puts the GardenPanel
into a Frame and displays it. It should be modelled after Splat.java (p186) or
one of the applications from lab5.
Suggestions for Doing the Assignment You should do the assignment
in parts, getting each part right before going to the next. You should not
do this all at once!! A suggested
order is as follows:
- Write GardenApp.java.
- Write the Critter class.
- Write part of GardenPanel.java. Start by just instantiating one
critter and drawing it. Be sure you like the way the critter looks
- Add the other two critters to the panel.
- Add code to get the heights of the two parent critters, find
the average, and set the height of the child critter.
Label the child. Run this
several times to see if the height appears to be the average of the
- Now add code to find and set the averages of the other attributes
(do this one at a time!). Be sure to also add your name to the scene.
- Finally write the Flower class.
- Add flowers to the panel.
- Thoroughly test your program!! Be sure it meets all specifications.
Your project will be graded on style, documentation and correctness.
Some stylistic aspects you should pay attention to include the use of
constants (where appropriate), identifier names, and the use of whitespace
and proper formatting.
You should also make sure that your submission does not contain uneccessary
code, e.g. extra assignments or lines of code
that have been commented out. Twenty percent of the
final assignment grade will be for style and
documentation. Documentation is a critical portion of any
computer program. Each method is expected to have an overall
description as well as
a description of each parameter and the return value - use javadoc
style documentation as we did in lab (use Eclipse to help you!).
Larger methods should
be internally documented with descriptions of the major steps.
The balance of the grade will come from correctness, which
includes meeting all of the specifications of the assignment.
A printed copy of your program files
(the Java files - Critter.java, Flower.java, GardenPanel.java,
Tar all the files in your assign3 subdirectory and email the .tgz
file to your instructor with a subject of cpsc120 assign3.
Academic Integrity Reminder: Assignments are to be done on
your own. You may get help on the specifices of the assignment
from no one but the instructor. You may not show your program to
anyone else or look at anyone else's program or share ideas about
how to write the program.