// class representing a member of the evil robot army public class EvilRobot { /* static class variables --------------- */ // the leader of the army private static EvilRobot fearlessLeader; // version number of evil robots public static final double ROBOT_VERSION = 3.7; private static int numRobots; // static block -- happens once at beginning of program // use to initialize static variables // and do other needed setup static { setNumRobots(0); // temp variable, only exists within static block EvilRobot fl = new EvilRobot(); fl.makeLeader(); } /* instance variables --------------- */ private int idNumber; // the id of this robot private String job; // this robot's job /* constructors --------------- */ /** default constructor **/ public EvilRobot() { idNumber = 0; job = "jobless"; setNumRobots(getNumRobots() + 1); } /** constructor with all variables as parameters **/ public EvilRobot(int idNumber_val, String job_val) { // call default constructor this(); setIdNumber(idNumber_val); setJob(job_val); } /* accessors and mutators --------------- */ /** * Accessor for idNumber **/ public int getIdNumber(){ return idNumber; } /** * Mutator for idNumber **/ public void setIdNumber(int newvalue){ if (newvalue >= 0) { idNumber = newvalue; } } /** * Accessor for job **/ public String getJob(){ return job; } /** * Mutator for job **/ public void setJob(String newvalue){ if (newvalue != null) { job = newvalue; } } /** * Accessor for fearlessLeader **/ public static EvilRobot getFearlessLeader(){ return fearlessLeader; } /** * Mutator for fearlessLeader **/ public static void setFearlessLeader(EvilRobot newvalue){ fearlessLeader = newvalue; } /** * Accessor for numRobots **/ public static int getNumRobots(){ return numRobots; } /** * Mutator for numRobots * private, cannot be used outside class **/ private static void setNumRobots(int newvalue){ numRobots = newvalue; } /* utility methods --------------- */ /** * represent as a string **/ public String toString() { return "[Robot #" + getIdNumber() + "(ver " + ROBOT_VERSION + "): " + getJob() + "]"; } /* methods --------------- */ /** * make this robot the leader **/ public void makeLeader() { setFearlessLeader(this); // same as just setJob setJob("lead my glorious troops to dominate the universe"); } /** * check if there is currently a leader * if not, volunteer **/ public boolean checkLeader() { // if the leader pointer is null // I become the leader if (getFearlessLeader() == null) { makeLeader(); return true; } return false; } /** * get a job from the leader * (or become leader) **/ public void getAJob() { // check if there is a leader // if there was (so I didn't volunteer) if (!checkLeader()) { // get a job from the leader getFearlessLeader().assignJob(this); } } /** * assign a job to a robot **/ public void assignJob(EvilRobot rob) { // if I'm not the leader, I can't assign jobs if (this == getFearlessLeader()) { rob.setJob(generateJob()); } } /** * randomly choose a job * this isn't specific to one robot * so make static **/ public static String generateJob() { // get a random number int choice = (int)(Math.random() * 3.0); // return string based on choice number switch (choice) { case 0: return "go bzzzt"; case 1: return "build rockets"; case 2: return "dig pit traps"; default: return "shoot lasers"; } } }