// Robot uses the built-in Color class in java.awt.Color. (see Java API) import java.awt.Color; /** FeelingRobot is a BouncingRobot except that it uses sensors * to detect the wall before actually hitting it. (Hopefully, * this will protect the stepper from stress.) */ public class FeelingRobot extends BouncingRobot { // fields // new parts protected Sensor front, // front sensor back; // back sensor // constructor /** FeelingRobot now also accepts Sensor objects for the * whiskers and tail, which are the front and back * sensors, respectively. */ public FeelingRobot( Stepper s, Color c, Sensor whiskers, Sensor tail ) { // first call the superclass's constructor // (note: Java requires that the super constructor, if called, // *must* be the first line in the constructor!) super( s, c ); // then do the new stuff this.front = whiskers; this.back = tail; } /** This overrides BouncingRobots act(). Here, right before * we try to move, we check the sensors and adjust the direction * accordingly if the sensors sense anything. */ public void act() { // check the sensors if ( front.sensesSomething() ) { // if front sensor hits a wall or something, // then we decide to move backward movingForward = false; } else if ( back.sensesSomething() ) { // if back sensor hits something, // then we decide to move forward movingForward = true; } // Note: if sensors don't sense anything, then movingForward // is not changed. // Now call BouncingRobot's act() method super.act(); } }