/* Bunny.java example code AC Chapin */ package cis119.pkg11.methods; // a Bunny -- example of a class with this. public class Bunny { public String name; // name of the Bunny public int carrotCount; // how many carrots eaten // print a greeting to another Bunny public void sayHi(Bunny otherBunny) { // uses the name instance variable // of the Bunny the method is called for // and this is that bunny // so if we called bugs.sayHi(babs) // then this is bugs // and name (same as this.name) is bugs' name while // otherBunny.name is babs' name // if we called babs.sayHi(bugs) // then this is babs System.out.println("Hi, " + otherBunny.name + ", my name is " + this.name + "!"); } // increase the number of carrots public void feed(int numcarrots) { this.carrotCount += numcarrots; System.out.println("Thanks! I now have " + this.carrotCount + " nummy carrots!"); } // the height Bunnys hop // is determined by how many carrots they ate public void hopWith(Bunny competition) { int height = this.carrotCount / 2; int otherheight = competition.carrotCount/2; if (height > otherheight) { System.out.println(this.name + " the Bunny hopped higher than " + competition.name); } else { System.out.println(competition.name + " the Bunny hopped higher than " + this.name); } } // run around like crazy public void marchMadness() { System.out.println(this.name + " runs around crazily until knocking itself out against a tree. Poor " + this.name); } // toString method creates String representing the Bunny // whenever a Bunny is implicitly cast to a String // println calls this automagically @Override // netbeans likes to do this public String toString() { return "a bunny named " + this.name + " with " + this.carrotCount + " carrots"; } }