Homework 3


Part A

You will write two classes, Gorilla and Cat, which interact. Your goal is to use the classes described to show that you know how to do all the standard class methods properly.

Gorillas have a name, a weight, which cannot be negative, and pet, which is a Cat. Cats have a name, a color (a String), a boolean for whether they are hungry, and an ape, which is a Gorilla.

In Gorilla, provide a parameterized constructor that takes two Strings, one of which is the name of the Gorilla, and the other the name of a new Cat that will be the Gorilla's pet. (We would 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, and vice versa.)

Consider how to construct the toString in each class so that it behaves reasonably both ways, for Gorillas that do and don't have pets, and Cats that do and don't have apes (watch out for infinite recursion). Make use of an inline conditional at least once.

Gorillas like to play; if possible, they will play with their pets, but if they don't have a pet they will play alone. If a Gorilla plays with their pet, the pet will become hungry.

In a main, test that all of this works.


Part B

In a separate class with main in the same project, do Exercise 5.29 from the Deitel & Deitel text, but you may choose a different song with a similar structure if you would prefer, as long as it has more than 7 verses (you may write your own if it amuses you).