Classes & Objects
Using and Creating Classes
- new
- dot operator
- instance variables
- methods
Hero.java
HeroHarness.java
Monster.java
Deitel ch 3
Classwork
When example code is given, you may use it as a model when doing assignments, unless I say otherwise. You should use nothing beyond what we have covered in class or is given in example code (e.g. even if you know about accessors and mutators, don't use them yet).
Part A
Create a project and add to it the given example classes
Hero and Monster (easiest way to do this is to add classes with those names, then copy-paste
my code) You will probably need
to fix the package statement to match your package name.Call your
class with a main method HeroAndMonster.
☑ In the main method:
- create a Hero (remember to use "new").
- give the Hero a name of your choosing (set the name instance variable)
- set the Hero's hit points
- make the Hero rest, then run
(You can base this code on HeroHarness but use different variable names
and values.
)
☑ Continuing in main:
- create a Monster
- give the Monster a name of your choosing
- make the Monster attack
- set your hero's enemy to be your Monster
- print out your Monster's name without using your local Monster variable
(must work even if we went back and changed the monster's name value) (hint: who
else in the main knows about the monster, other than the local
monster variable?)
- let your Monster eat your Hero by passing your Hero variable into the
parentheses of the Monster's eat method
- print out the Hero's new name and number of hit points
Part B
You will create a class modeling some specific type of creature. You can choose any type of creature you want --
capybara, grue, naked mole rat, creeper, zombie dinosaur, whatever except
the examples we've already done such as
generic monster, cat, dog, or bunny. You
can use Hero as a model. Name the class based on the specific type of
creature it is
modeling (i.e. public class ZomebieDino, not just public class Creature)
☑ Create the new class in the same package as part A.
If it shows up in a different package (<default package>), you didn't do
new on the package, drag it to the right package.
☑ For your new creature class
- give it a String instance variable to hold its name
- give it an int instance variable to keep track of how much of
something it has (do not use hit points) e.g. how many balls of yarn a cat
has collected, how many bales of hay a horse has eaten, how many cities
godzilla has stomped.
- add a method that, when called, increases (or decreases) this count by
1, and prints what has happened e.g. stompCity() could print
"Monster-kun has stomped another city! That's 8 total! Tokyo is next!"
(nothing beyond printing and changing the value of the variable required,
you can copy the run method from the example code and just modify it.)
- add an appropriate toString method
- add a method for some other behavior appropriate for your creature. This method
should just print a description of the action, using the name of the creature,
but it may also change an instance variable if you wish (again, you can just
copy from the example code and make changes)
☑ Further down in the main of your HeroAndMonster, create an instance of your
new class, and test setting all variables and calling all methods.