/** * class representing a general monster with teeth **/ package monsters; public class Monster { protected int teeth; // protected means inheriting classes can use directly (and rest of package too) /** * default constructor for Monster **/ public Monster() { teeth = 0; } /** * accessor for teeth **/ public int getTeeth() { return teeth; } /** * mutator for teeth **/ public void setTeeth(int val) { teeth = val; } /** * monsters eat people **/ public void eat(){ System.out.println("Eat people!"); } /** * monsters make noise **/ public void roar() { System.out.println("GRRARGH!"); } /** * monsters fight!! **/ public void fight(Monster other) { System.out.println(this + " and " + other + " are fighting!"); roar(); other.roar(); } /** * overrides toString from Object * @return * string representing this monster **/ public String toString() { return "monster with " + getTeeth() + " teeth at " + super.toString(); // user Object's toString } /** * overrides equals from Object * @return * true if other is equal to this monster (same number of teeth) **/ public boolean equals(Object other) { if (other != null && other instanceof Monster) { Monster m = (Monster)other; return getTeeth() == m.getTeeth(); } return false; } }