/* 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); } }