// class representing a Bee // including a static swarm of bees // example of class implementing interfaces package interfaceexample; public class Bee implements Fighter, Worker, LivingBeing { // inches high private static int sizeofHive; // all the bees, with tracker number private static Bee[] swarm; private static int numBees; // 1 in swarmAttackChance chance of a swarmAttack private static int swarmAttackChance; static { sizeofHive = 0; numBees = 0; swarm = new Bee[100]; new Bee(); swarmAttackChance = 10; } // constructors // every new bee tries to join the Swarm public Bee() { joinSwarm(); } // accessors, mutators public static int getSizeofHive() { return sizeofHive; } public static void setSizeofHive(int sizeofHive) { Bee.sizeofHive = sizeofHive; } // behaviors // the bee joins the swarm public void joinSwarm() { if (numBees < swarm.length) { swarm[numBees++] = this; // swarm attacks get more annoying as there are more bees // so decrease the chances of a swarm attack with each bee swarmAttackChance++; } } // the whole swarm attacks // warning!! swarm attacks can trigger MORE swarm attacks! public void swarmAttack() { System.out.println("SWARM ATTACK!"); for (int i = 0; i < numBees; i++){ System.out.print(i + " " ); swarm[i].sting(); } } // the bee stings, possibly inspiring others public void sting() { System.out.println("The bee stings!"); // in rare cases, the rest of the swarm will join in if ((int)(Math.random() * swarmAttackChance) == 1) { // swarming gets less likely as we go. int temp = swarmAttackChance; swarmAttackChance *= 10; swarmAttack(); swarmAttackChance = temp; } } // defend // for interface Fighter public void defend() { System.out.println("The bee stings some more!"); } // attack // for interface Fighter public void attack() { sting(); } // work on building the hive public void buildTheHive() { if (getSizeofHive() == 0){ System.out.println("The bees have started a new hive"); } sizeofHive++; System.out.println(hiveReport()); } // so we can qualify as a Worker public void doWork() { buildTheHive(); } // the status of the hive public static String hiveReport() { return "The hive is now " + sizeofHive + " inches high"; } // the status of the swarm public static String swarmReport() { return "There are now " + numBees + " in the swarm"; } // utilities // bees have no personal information @Override public String toString() { return "one bee out of "+ numBees; } @Override // all bees are the same public boolean equals(Object obj) { if (obj != null && obj instanceof Bee) { return true; } return false; } @Override public int hashCode() { int hash = 31; return hash; } }