/* Bunny.java example code AC Chapin */ package cis119.pkg11.methods; // 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 // 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!"); } // increase the number of carrots public void feed(int numcarrots) { carrotCount += numcarrots; System.out.println("Thanks! I now have " + carrotCount + " nummy carrots!"); } // the height Bunnys hop // is determined by how many carrots they ate public void hop() { int height = carrotCount / 2; System.out.println(name + " the Bunny just hopped " + height + " feet in the air!"); } // run around like crazy public void marchMadness() { System.out.println(name + " runs around crazily until knocking itself out against a tree. Poor " + 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 " + name + " with " + carrotCount + " carrots"; } }