// Robot uses the built-in Color class in java.awt.Color. (see Java API) import java.awt.Color; /** A Robot is a self-animating object that * moves (or tries to move) when you switch it on. */ public class Robot implements Runnable { // fields // parts private Thread spirit; // label for thread that will run run() protected Stepper myStepper; // used for moving forward or backward // properties/characterisitcs/attributes private Color c; // state protected boolean on; // constructor public Robot( Stepper s, Color c ) { this.myStepper = s; this.c = c; this.spirit = new Thread( this ); this.spirit.start(); } // methods /** switchOn() turns the robot on */ public void switchOn() { this.on = true; } /** switchOff() turns the robot off */ public void switchOff() { this.on = false; } /** run() has a while(true) whose body calls act() if the robot * is on, and does nothing otherwise. */ public void run() { while( true ) { if (on) { act(); } } } /** This method is called periodically by run() if the robot is on. */ public void act() { try { // try to step forward myStepper.stepForward(); } catch (CannotStepException e) { // if it's not possible, shout for help System.out.println( "I'm stuck! Help me!" ); } } }