// class with main method, which uses class Bunny // note: output is given in a comment at the bottom public class BunnyHarness { // main method shows how to use Bunny class public static void main(String[] args) { // create a variable named bugs, of type Bunny Bunny bugs; // we need to say new to set it up // if not, compiler will complain that it is not initialized bugs = new Bunny(); // all Bunnys have a name variable, a String // the dot operator allows us to access var inside object // so bugs.name is the name variable for the // Bunny named bugs // obj.var is often read "obj's var" // before we set them, the variables have default values System.out.println("bugs' name before it is set: " + bugs.name); System.out.println("bugs' carrotCount before it is set: " + bugs.carrotCount); System.out.println("bugs' pointyTeeth before it is set: " + bugs.pointyTeeth); System.out.println(); // sane classes would not let us change their // variables directly like this bugs.name = "Mr. Bugs Bunny, esq."; // all Bunnys have a carrotCount variable, an int bugs.carrotCount = 1; // all Bunnys have a pointyTeeth variable // (to tell if they are scary Monty Python bunnies) bugs.pointyTeeth = false; // create another Bunny, named evilBun, with its own variables // this is a different object with the same structure Bunny evilBun = new Bunny(); evilBun.name = "Bun Bun (prince of darkness)"; evilBun.carrotCount = 14; evilBun.pointyTeeth = true; // both are Bunnys, and have same instance vars // but the values are different System.out.println("bugs' name: " + bugs.name); System.out.println("bugs' carrotCount: " + bugs.carrotCount); System.out.println("bugs' pointyTeeth: " + bugs.pointyTeeth); System.out.println(); System.out.println("evilBun's name: " + evilBun.name); System.out.println("evilBun's carrotCount: " + evilBun.carrotCount); System.out.println("evilBun's pointyTeeth: " + evilBun.pointyTeeth); System.out.println("\n\nHere goes " + bugs.name); System.out.println("Printing the Bunny variable " + bugs + " is different from printing the name instance variable " + bugs.name); // all Bunnys have a method feed(), // allowing you to feed them some carrots // this method takes an int as argument to know how many // so bugs.feed(2) says, feed the Bunny named bugs 2 carrots bugs.feed(2); // all Bunnys have a method hop bugs.hop(); // all Bunnys can go crazy bugs.marchMadness(); System.out.println("\n\nHere goes " + evilBun.name); // call the same methods for evilBun and get different results // because the variables have different values // we can pass an integer variable to the method int howmanycarrots = 4; evilBun.feed(howmanycarrots); evilBun.hop(); evilBun.marchMadness(); } } /* --- OUTPUT -------------------------------------------- bugs' name before it is set: null bugs' carrotCount before it is set: 0 bugs' pointyTeeth before it is set: false bugs' name: Mr. Bugs Bunny, esq. bugs' carrotCount: 1 bugs' pointyTeeth: false evilBun's name: Bun Bun (prince of darkness) evilBun's carrotCount: 14 evilBun's pointyTeeth: true Here goes Mr. Bugs Bunny, esq. Printing the Bunny variable Bunny@1172e08 is different from printing the name instance variable Mr. Bugs Bunny, esq. My name is Mr. Bugs Bunny, esq. Thanks! I now have 3 nummy carrots! Mr. Bugs Bunny, esq. just hopped 1 feet in the air! Mr. Bugs Bunny, esq. just went CRAAAAAAZY!!!! Here goes Bun Bun (prince of darkness) My name is Bun Bun (prince of darkness) Graaaaargh! Thanks! I now have 18 nummy carrots! Bun Bun (prince of darkness) just hopped 9 feet in the air! Bun Bun (prince of darkness) just went CRAAAAAAZY!!!! --------------------------------------------------------- */