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
☑ Create a class modeling some type of creature,
(do not use previous example types such as Bunny, Cat, etc). Remember to
name your class for the type of creature, in the singular.
(Your creature does not have to be real or zoologically accurate, but
your design in the choices you make below should make some sense)
☑ For your new creature class
- include a name
- include an instance variable of type double or boolean for some
characteristic appropriate to your creature
- add a method for some behavior appropriate to your creature. This
method only need print a description of the action, using the name. (remember
you can copy and make changes to example code)
- add an appropriate toString method
☑ In a separate harness class,
in a main,
- create two instances of your class,
- set their names and other instance var to different values
- test the behavior,
- print them
(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 creature that keeps the first type as a pet.
☑ For your new creature class
- include a name
- include a variable pet of the other creature type (NOT just its name,
the whole other creature; think about dogs who know about cats )
- include a method, play, for this creature to play with its pet,
which prints a message using the other creature's name. This
might print something like "I'm playing with Harvey, my pet Godzilla."
(you may want to look at how Dogs chase Cats)
- Include another method, eat, that prints a message using the
pet's other instance variable, and then changes the pet's name to "Dinner."
For example it might print "Since it is false that my pet Godzilla can fly,
I've decided to eat it" or "Despite all my love and care my pet
butterfly only weighs 3.7 tons, so I'm going to have to eat it." (no
matter what the value, the pet is getting eaten)
- include the name and the pet in the toString
☑ In your harness, test out this
creature, including its interactions with the creature from part A.
Your methods in the second class will blow up if you don't give your second
creature a pet before calling play or eat, so make sure you give
this creature a pet!!
[EC] Give your second class a myBestFriend, who is of
the same type, and a method swapPets, which prints that they are swapping pets
with their bestFriend, after which they should have their myBestFriend's old
pet, and their myBestFriend should have their old pet. This DOES NOT MEAN
just to swap the names and other vars for the pets, swap the ACTUAL pets!!!
(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 should have a pet too).