Class Methods
Homework
You will write two classes, Gorilla and Cat, which interact (also a test
harness).
Part A
☑ Write a class Gorilla. Gorillas should have a name and a weight, so add instance variables to represent these.
☑ Add an accessor and a mutator for each of the instance variables. In the mutator for weight, don't allow the weight to be set to a negative number.
☑ Add a default constructor which sets the name and weight to reasonable default values.
☑ Add a parameterized constructor that takes values for name and weight as parameters and sets the instance variables accordingly
☑ Add a toString that includes all the information about the Gorilla.
☑ Add a method eat, which takes an int howMany and prints
"Eating a banana!" howMany times
☑ Create a harness class with a main and test that you can create Gorillas (using both constructors) and set their weights and names. Don't forget to print the original values for these variables, to show your constructors work.
Part B
☑ Create a class Cat. Cats should have a name and a color (both Strings).
☑ Add accessors, mutators, and a default constructor for the Cat class. Also add a parameterized constructor that takes a value for the name.
☑ Add a toString that includes all the information about the Cat.
In the same harness as in part A, test that you can create Cats and set their variables.
Part C
☑ Add to the Gorilla class an instance variable of type Cat, called pet (Not
a String. We want a Cat, not just its name!!!).
(Do not use new to create a new cat in the default Gorilla constructor,
unless you are prepared to argue that every time a baby gorilla is born, a
kitten magically appears with it.)
☑ Add an accessor and mutator for the pet variable.
☑ update toString so that it includes something
like " and a pet kitten, Fluffy" if the Gorilla has a pet but doesn't
mention pets if there is no pet yet (what value would pet have if it
hasn't been set to any Cat yet?)
☑ Also add a method play, which takes no
parameters. If there is no pet, print "Playing all alone." otherwise
print something like "Playing with Fluffy" using the name of the cat.
☑ In your harness, check that your new accessor and mutator work. For instance, if you have a Gorilla koko and a Cat fluffy, you should be able to say
koko.setPet(fluffy);
And if you then said
System.out.println(koko.getPet().getName());
you should get the same result as
System.out.println(fluffy.getName());
(Think through it and make sure you understand why those should be the same thing. You may want to draw what is happening in memory, if you're having trouble.)
EC +10:
Give the Cat class an instance variable ape, of type Gorilla, and add mutator and accessor for it. In the harness, test that these work.
EC +10:
To the Gorilla class, add a parameterized constructor that takes a String and a Cat, and sets the Gorilla's name and pet accordingly.
EC +15:
To the Gorilla class, add a parameterized constructor that takes two Strings. The first is the name of the Gorilla. The second will be the name of the pet Cat. In this constructor, use the parameterized constructor for the Cat class to set up the pet variable so that it points to a
newly created Cat, using the given second string for the name. So
in this case calling the Gorilla constructor will also cause a Cat to be created on the heap.