/* BunnyHarness.java example code AC Chapin */ package ObjectsEx; // class with main method, which uses class Bunny public class BunnyHarness { // main method tests out Bunny class public static void main(String[] args) { // creation of Bunny variables // each gets a copy of all instance vars // each can do all methods Bunny bugs = new Bunny(); Bunny babs = new Bunny(); // printing out the default values System.out.println("\nNewly Created Bunny vars\n"); // here we print the instance vars individually System.out.println(bugs.name + " has " + bugs.carrotCount + " carrots"); System.out.println(babs.name + " has " + babs.carrotCount + " carrots"); // setting Bunny instance variables to different values bugs.name = "Bugs Bunny"; babs.name = "Babs Bunny"; bugs.carrotCount = 6; babs.carrotCount = 2; // printing the new values System.out.println("\nInstance vars set\n"); // here we print the variables directly, which uses toString() System.out.println(bugs); // same as System.out.println(bugs.toString()); System.out.println(babs); // using Bunny methods System.out.println("\nCalling Methods\n"); bugs.feed(); /* We set bugs.carrotCount to 6. Since we are calling feed( for bugs the value of carrotCount used in the feed( method is bugs' value, so we get 6 + 1 = 7 */ bugs.hop(); babs.feed(); /* We set babs.carrotCount to 2. Since we are calling feed( for babs the value of carrotCount used in the feed( method is babs' value, so we get 2 + 1 = 3 */ babs.hop(); // printing new values System.out.println("\nAfter feeding and hopping\n"); System.out.println(bugs.name + " now has " + bugs.carrotCount + " carrots"); System.out.println(babs.name + " now has " + babs.carrotCount + " carrots"); // This method is in the Bunny class. // If we do not call it from main, it does not run when we run the program // if you uncomment these lines, then it will run. //bugs.marchMadness(); //babs.marchMadness(); } }