/* Bunny.java example code AC Chapin */ package ObjectsEx; // a Bunny -- example of a class // class code is written for one Bunny // when we create Bunny variables, each one // gets all the things in this code public class Bunny { // INSTANCE VARIABLES // aka FIELDS // characteristics every Bunny has public String name; // name of the Bunny public int carrotCount; // how many carrots eaten // METHODS // actions every Bunny can do // increase the number of carrots public void feed() { carrotCount++; System.out.println("Thanks! I now have " + carrotCount + " nummy carrots!"); } // the bunny hops public void hop() { System.out.println(name + " the Bunny just hopped!"); } // toString method creates String representing the Bunny // whenever a Bunny is implicitly cast to a String // println calls this automagically // String is return type (instead of void) // String instead of void means this method has a result, which is a String // should make sense in a sentence @Override // netbeans likes to do this public String toString() { // keyword return means the method gives the following value back as result // return ends the method // return the result of evaluating this expression (must be a String because that is return type) return "a bunny named " + name + " with " + carrotCount + " carrots"; } // run around like crazy public void marchMadness() { System.out.println(name + " runs around crazily until knocking itself out against a tree. Poor " + name); } // print a greeting public void sayHi() { // uses the name instance variable // of the Bunny the method is called for // so if we called bugs.sayHi() // then we use bugs.name System.out.println("My name is " + name + " and I'm a Bunny!"); } }