package staticexamples; // test harness for Evil RObot class // with demos of methods in main class public class EvilRobotHarness { // print out the given robots, and the leader // this is static, so can call from main public static void printRobots(EvilRobot r1, EvilRobot r2, EvilRobot r3) { System.out.println(r1); System.out.println(r2); System.out.println(r3); System.out.println(EvilRobot.getFearlessLeader()); } // print out the given robots, and the leader // this is static, so can call from main // overloads above method -- add string param public static void printRobots(EvilRobot r1, EvilRobot r2, EvilRobot r3, String msg) { System.out.println(msg); System.out.println(r1); System.out.println(r2); System.out.println(r3); System.out.println(EvilRobot.getFearlessLeader()); } // print a greeting // this is not static, // need instance to call from main public void printGreeting() { System.out.println("I for one welcome our Evil Robot overlords (version " + EvilRobot.ROBOT_VERSION + ")."); } // test out Evil RObot class // also show calling above methods public static void main(String[] args) { // to call printGreeting, we need an instance // printGreeting(); // error, no instance, non-static method from static context EvilRobotHarness harn = new EvilRobotHarness(); harn.printGreeting(); // create some robots to test EvilRobot robot1 = new EvilRobot(); robot1.setIdNumber(1); robot1.setJob("sit around beeping"); EvilRobot robot2 = new EvilRobot(2, "be shiny"); EvilRobot robot3 = new EvilRobot(3, "spin in circles"); System.out.println("---initial robots---"); // static method, so can call by itself printRobots(robot1, robot2, robot3); // kill off fearless leader EvilRobot.setFearlessLeader(null); // robot behavior robot2.getAJob(); robot1.getAJob(); robot3.getAJob(); // call other version of printRobots printRobots(robot1, robot2, robot3, "---updated robots---"); } }