package staticexamples; // 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 double ROBOT_VERSION; private static int numRobots; // static block -- happens once at beginning of program // use to initialize static variables // and do other needed setup static { ROBOT_VERSION = 3.7; 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 --------------- */ // constructor block gets included in all constructors as first thing // (but not called twice when chaining constructors with this() ) // put code here to emphasize that it runs in all constructors // and isn't just about setting instance variable values { setNumRobots(getNumRobots() + 1); } /** default constructor **/ public EvilRobot() { idNumber = 0; job = "jobless"; } /** 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; } public static double getROBOT_VERSION() { return ROBOT_VERSION; } // example of shadowing static variable with local // and getting at it with dot operator public static void setROBOT_VERSION(double ROBOT_VERSION) { // ROBOT_VERSION here is local // EvilRobot.ROBOT_VERSION is static variable of class EvilRobot.ROBOT_VERSION = ROBOT_VERSION; } /* 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"; } } }