/* BunnyHarness.java example code AC Chapin */ package publicPrivate; /** * example of using objects with constructors, accessors, mutators */ class BunnyTestHarness { // main method tests out Bunny class public static void main(String[] args) { /* code written using old version of Bunny */ // created Bunny using default constructor Bunny bugs = new Bunny(); // values from default constructor System.out.println("first : [" + bugs.getName() + "] has [" + bugs.getCarrotCount() + "] carrots."); // use accessors and mutators bugs.setName("Bugs Bunny"); bugs.setCarrotCount(5); System.out.println("later : [" + bugs.getName() + "] has [" + bugs.getCarrotCount() + "] carrots."); // class methods bugs.sayHi(); bugs.feed(2); // automatic call to tostring System.out.println("so that was " + bugs); // Bunny created using parameterized constructor Bunny babs = new Bunny("Babs Bunny", 6); // values from parameterized constructor System.out.println("first : [" + babs.getName() + "] has [" + babs.getCarrotCount() + "] carrots."); // accessors and mutators babs.setName("Barbara J. Bunstein"); babs.setCarrotCount(16); System.out.println("later : [" + babs.getName() + "] has [" + babs.getCarrotCount() + "] carrots."); // class methods babs.sayHi(); babs.feed(9); // automatic call to tostring System.out.println(babs + " is finished"); /* code that knows about new version */ // get some stolen carrots for (int i = 0; i < 10; i++) { bugs.stealCarrot(); } System.out.println("bugs after stealing " + bugs); bugs.eatTheEvidence(); System.out.println("bugs, who ate the evidence " + bugs); // get some stolen carrots for (int i = 0; i < 10; i++) { babs.stealCarrot(); } System.out.println("babs after stealing " + babs); babs.setCarrotCount(7); System.out.println("babs, starting over " + babs); babs.eatTheEvidence(); } }