package Files; //a class representing a Bunny rabbit import java.io.Serializable; public class Bunny implements Serializable { private String name; private int carrotCount; // how many carrots // transient variable will not be serialized // so deserialized Bunnys start with default for boolean // deserialized Bunnys are sad private transient boolean happy; // default constructor public Bunny() { name = "Unnamed Bunny"; carrotCount = 0; happy = true; } // parameterized constructor (all instance vars) public Bunny(String nameVal, int carrotVal) { this(); setName(nameVal); setCarrotCount(carrotVal); } // name accessor public String getName() { return name; } // name mutator public void setName(String val) { name = val; } // carrotCount accessor public int getCarrotCount() { return carrotCount; } // carrotCount mutator -- 0 or positive vals public void setCarrotCount(int val) { if (val >= 0) { carrotCount = val; } } public boolean isHappy() { return happy; } public void setHappy(boolean happy) { this.happy = happy; } // print a greeting public void sayHi() { // inline if for format System.out.println("My name is " + getName() + " and I am " + (happy ? "" : "not") + " happy"); } // increase the number of carrots public void feed(int numcarrots) { setCarrotCount(getCarrotCount() + numcarrots); System.out.println("Thanks! I now have " + getCarrotCount() + " nummy carrots!"); } // the height bunnies hop // is determined by how many carrots they ate public void hop() { int height = getCarrotCount() / 2; System.out.println(getName() + " just hopped " + height + " feet in the air!"); } // toString determines how Bunny is printed public String toString() { return getName() + ", a " + (happy ? "happy" : "sad") + " bunny who has eaten " + getCarrotCount() + " carrots"; } }