Classes
Creating Our Own Classes
- declaring instance variables
- methods in classes
- local variables
- toString method
Dog.java
DogHarness.java
Bunny.java
BunnyHarness.java
Cat.java
Deitel chapter 3
Classwork
Part A
For the following, do not use
previous example types such as Bunny, Cat, etc nor the types you used in the
previous classwork. Remember to
name your classes for the type of creature, in the singular.
Your creatures do not have to be real or zoologically accurate, but
your design in the choices you make below should make some sense.
☑ Create a class representing some sort of creature.
☑ For your new creature class
- include a name
- include an instance variable for the age of the creature
- add a method for some behavior appropriate to your creature. This
method only need print a description of the action, using the name.
- add a method birthday that prints a happy birthday message using the name and also update's the creature's age
- add an appropriate toString method
☑ In a separate harness class,
in a main,
- create two instances of your class,
- print them (you should see the default values for name and age)
- set their names and ages to different values
- test all their behaviors (all methods other than toString)
- print them (this tests toString)
(This may look a lot like BunnyHarness.)
Part B
☑ Create a second new creature class. You will test it in the same test harness.
This class represents a kind of creature that likes to hunt the first type.
☑ For your new creature class
- include a name
- include a variable energy, a number representing how much energy it has
- include a variable prey using the other creature type (NOT just its name,
the whole other creature; think about dogs who know about cats )
- include a method, hunt, for this creature to hunt its prey,
which prints a message using both this creature's name and the other creature's name. This
might print something like "Vlad the goat is hunting Fluffy the cow."
(you may want to look at how Dogs chase Cats)
- Include another method, devour that first prints a message saying that it is eating the prey,
then adds the prey's age to its energy, prints how much energy it now has, and finally changes the prey's name to "Food."
- include the name, energy, and prey in the toString
☑ In your harness, create an instance
of this class and give it the first of your original creatures as prey, and have it hunt
and devour, then repeat the process with the second of your original creatures
Part C
Give your second class a myBestFriend, who is of
the same type, and a method swapPrey, which prints that they are swapping prey
with their bestFriend, after which they should have their myBestFriend's old
prey, and their myBestFriend should have their old prey. This DOES NOT MEAN
just to swap the names and other vars for the prey, swap the ACTUAL prey!!!
(hint: if you had a glass of milk and a glass of wine, could you swap their
contents using just those two glasses...?) Test this in main (it will only
work if you give them a myBestFriend in main before you call the method, and the
myBestFriend has a prey too).