CPE 101 (Turner) --- Lab 7 --- Spring 2003
Laboratory Exercise for Week 6 (Thanks are
due to Prof. Hitchner and
to Kevin O'Gorman.)
This lab is to be done individually
Lab Objectives
Lab Requirements
This lab exercise will help you learn about using
object reference variables. You will use reference variables as method
parameters, as a method return value, and as local variables in the main
of an application class. You will also learn to design an overloaded
method, a method that has more than one version with the same name, but with
different parameter lists and different algorithms.
You must design and write the code for classes that
represent three kinds of imaginary creatures: Wacket, Wocket,
and Widget.
A Wacket is an object that contains a count
of male gremlins. It also stores their total weight.
A Wocket is an object that contains a count
of female gremlins. It also stores their total weight.
A Widget is an object that contains a count
of male gremlins and female gremlins. It also stores the total
weight of the males and total weight of the females.
- Learn
to design methods that use reference variables as parameters and learn to
design overloaded methods.
- Create a new class named Wacket.
- Define two instance variables: count (type int) and weight (type
double).
- Define the constructor with two parameters for the initial
count and weight values. The constructor must validate the parameter
values. If the count is positive and the weight is not negative, assign
their values. Otherwise, assign zero to both instance variables (note
this implies it is possible to have Wackets that do not weigh anything at
all, which is fine because they are imaginary creatures anyway!). Do
not print an error message.
- Define three accessor methods:
- getCount, getWeight, and getAvgWeight.
The last method returns a double value if the count is > zero. Otherwise
it returns zero.
- Define the toString method. It must return the string,
"Wacket: nn male gremlins, xx.xx weight" where nn and xx.xx are the values
of the count and weight instance variables.
- Define three mutator methods (three overloaded versions
of the method add):
- add. This method has one parameter: the weight
of one male gremlin that must be added to the Wacket. The algorithm
for this method is: add the weight parameter's value to the object's weight,
and increment the count by 1. However, if the weight is negative,
do not add anything to the weight or to the count.
- add. This method has two parameters: the count
of the number of male gremlins to be added and their total weight. The
algorithm for this method is: add the count parameter's value to the object's
count and add the weight parameter's value to the object's weight. However,
if the count is not positive or if the weight is negative, do not add anything
to the weight or to the count.
- add. This method has one parameter: a reference
to anotherWacketobject. The algorithm for this method is: add
the Wacket parameter object's weight and count to this object's weight
and count.
- Create a new class named Wocket.
- Define the same instance variables, constructor, and instance
methods as for the Wacket class. The only difference is that
this class's objects store female gremlin information. So the toString
method should return "Wocket: nn female gremlins, xx.x weight".
- Create a new class named Widget.
- Define four instance variables: male count (type int), male weight
(type double), female count (int), and female weight (double).
- Define the constructor with four parameters for the initial
count and weight values. The constructor must validate the parameter
values. If the male count is positive and the male weight is not negative,
assign their values. Otherwise, assign zero to the two male instance
variables. If the female count is positive and the female weight is
not negative, assign their values. Otherwise, assign zero to the two
female instance variables.
- Define six accessor methods:
- getMaleCount, getMaleWeight, getMaleAvgWeight,
getFemaleCount,getFemaleWeight, and getFemaleAvgWeight
Each average weight method returns a double value if the count is > zero.
Otherwise it returns zero.
- Define the toString method. It must return the string,
"Widget: nn male gremlins, xx.xx weight, mm female gremlins, yy.yy weight"
where nn, mm, xx.xx, and yy.yy are the values of the count and weight instance
variables.
- Define three mutator methods (three overloaded versions
of the methodadd):
- add. This method has one parameter: a reference
to anotherWacketobject. The algorithm for this method is: add
the Wacket parameter object's weight and count to this object's male
gremlin weight and count.
- add. This method has one parameter: a reference
to anotherWocketobject. The algorithm for this method is: add
the Wocket parameter object's weight and count to this object's female
gremlin weight and count.
- add. This method has one parameter: a reference
to anotherWidgetobject. The algorithm for this method is: add
the Widget parameter object's male weight and count to this object's
male gremlin weight and count and add the Widget parameter object's
female weight and count to this object's female gremlin weight and count.
- Next, create a new class named WascallyWabbit.
(Do you know the reference? I am kidding. The Widget, Wacket, andWocket
classes are all you need write for this lab.)
- Required documentation: Adequate comments
for every class (there will be 3), every field (several in each class) and
every method (also several in each class). You must show your preliminary
design documentation to the Instructor or Lab Assistant before you code.
Draw some simple UML design diagrams like the ones drawn in your textbook
(they need not be perfect UML, but give some nice, clean "boxes and arrows"
with good labels to indicate how this whole thing will be coded up. Draw the
classes as boxes, show the methods as rectangles that protrude from the boxes,
and have the data fields labled clearly. Indicate the required parameters
for the method calls and the return types.) The documentation is a very
important part of this exercise.
- Learn to design
an application class main method that uses reference variables as local variables.
- Create a new class named WidgetApp
with a main method. You may find it convenient to declare other
static methods in this class, but do not declare instance variables or
instance methods.
- Declare a main method and within its
block declare the following local variables.
- TWO Wacket objects
- TWO Wocket objects
- TWO Widget objects
- Construct the six objects. Use
Math.random() to generate the count and weight values for each Wacket,
Wocket, and Widget constructor call. For each
object generate an integer random count value that is in the range {-5, +25}
and a real (type double) random weight value that is in the range
{-1.0, +10.0}. This will represent the average weight of the gremlins.
To generate the weight, first generate a random integer in the range {-1000,
+10000} and divide that by 1000.0. Then to call the constructor, use
the 1st random number for the count parameter and use the count mulutiplied
by the average weight for the second parameter (total weight).
- Call System.out.println() and print
out theString description of the six objects. Do not use the
toString()method. Just use the object name and let Java
automatically invoketoString() for you. (Have you seen
this before? Isn't it a good thing?)
- Next add a statement that calls the add
method for the 1st Wacket object. Use the version that has a
Wacket as a parameter and use the other Wacket
as the argument. Then print out the String
version of the changed
Wacket with System.out.println().
- Next do the same for the Wocket object.
- Finally, call the three add methods
for theWidget object and print out the result after each call.
- When you run your main method, copy and paste
the output and insert it inside a comments block at the end of your
WidgetApp.java file.
Then, your ouput results will be turned in along with your source file.
You are required to demo this part to the instructor;
print up your source code and put a cover sheet on it to
hand in after your demo and the instructor checks you off.
- Required documentation: Javadoc
comments for the class, its "main" method, and any other methods you declare.
There should be no class or instance fields, so no documentation for them either.
This lab is to be done individually
Due: in lab on Thursday.