Question: HeroHarness.java example code /* HeroHarness.java example code AC Chapin */ package objectsExample; // a harness class to test out the Hero class public class HeroHarness

HeroHarness.java example code

/* HeroHarness.java example code AC Chapin */ package objectsExample; // a harness class to test out the Hero class public class HeroHarness { // create several Hero and Monster variables and show what they can do public static void main(String[] args) { // create and initalize Hero variables Hero thondar = new Hero(); Hero mitziWarriorPrincess = new Hero(); // setting instance variables thondar.name = "Fred the Mighty"; // note: name is a String variable in Hero // Java doesn't care that the variable name is name // and the String inside name doesn't have to match the variable name thondar mitziWarriorPrincess.name = "Mitzi"; // printing the instance variables System.out.println("thondar's name is " + thondar.name); System.out.println("mitziWarriorPrincess' name is " + mitziWarriorPrincess.name); // if we just print the object, toString is called System.out.println("just printing thondar: " + thondar); System.out.println("just printing mitziWarriorPrincess: " + mitziWarriorPrincess); // another instance var // number instance variables are initialized to 0 System.out.println("thondar's hit points initially: " + thondar.hit_points); thondar.hit_points = 15; System.out.println("thondar's hit points set: " + thondar.hit_points); // this object instance var initialized to null System.out.println("thondar's enemy is " + thondar.enemy); // next line causes null pointer exception because enemy is null //System.out.println("thondar's enemy is " + thondar.enemy.myName); // calling methods of an object // run decreases hit points thondar.run(); System.out.println("thondar's hit points after running: " + thondar.hit_points); // rest increases hit points thondar.rest(); System.out.println("thondar's hit points after resting: " + thondar.hit_points); // make mitziWarriorPrincess' ally and thondar the same hero // we are passing thondar to the method joinForces mitziWarriorPrincess.joinForces(thondar); System.out.println("mitziWarriorPrincess' ally's name is " + mitziWarriorPrincess.ally.name); } } 

Hero.java example code

/* Hero.java example code AC Chapin */ package objectsExample; // represents a hero who fights monsters public class Hero { // instance variables -- each Hero all of these, and will have values for them // instance variables are initialized to 0, false, or null public String name; // my name public int hit_points; // how much life I have public Monster enemy; // my mortal foe public Hero ally; // my buddy // the hero runs, which decreases health public void run() { // the name variable is our instance variable from above // it will have different values for different heroes System.out.println(name + " is running, and getting tired"); hit_points--; } // resting replenishes health public void rest() { System.out.println(name + " is resting"); hit_points++; } // fighting the hero's sworn enemy public void fight() { // if enemy is null, this will explode! System.out.println(name + " is fighting the monster " + enemy.myName); } // join forces with another hero -- so they become the ally public void joinForces(Hero otherHero) { // if this is called as hero1.joinForces(hero2) then name is hero1's name, and otherHero is hero2 // if this is called as hero2.joinForces(hero1) then name is hero2's name, and otherHero is hero1 System.out.println(name + " is joining forces with " + otherHero.name); ally = otherHero; } // toString is called whenever a Hero is cast to a String (e.g. by println) // it does not print, but returns so the String can be used elsewhere public String toString() { return "The Hero " + name; } }

Monster.java example code

/* Hero.java example code AC Chapin */ package objectsExample; // represents a hero who fights monsters public class Hero { // instance variables -- each Hero all of these, and will have values for them // instance variables are initialized to 0, false, or null public String name; // my name public int hit_points; // how much life I have public Monster enemy; // my mortal foe public Hero ally; // my buddy // the hero runs, which decreases health public void run() { // the name variable is our instance variable from above // it will have different values for different heroes System.out.println(name + " is running, and getting tired"); hit_points--; } // resting replenishes health public void rest() { System.out.println(name + " is resting"); hit_points++; } // fighting the hero's sworn enemy public void fight() { // if enemy is null, this will explode! System.out.println(name + " is fighting the monster " + enemy.myName); } // join forces with another hero -- so they become the ally public void joinForces(Hero otherHero) { // if this is called as hero1.joinForces(hero2) then name is hero1's name, and otherHero is hero2 // if this is called as hero2.joinForces(hero1) then name is hero2's name, and otherHero is hero1 System.out.println(name + " is joining forces with " + otherHero.name); ally = otherHero; } // toString is called whenever a Hero is cast to a String (e.g. by println) // it does not print, but returns so the String can be used elsewhere public String toString() { return "The Hero " + name; } }

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 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 (), 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!