Second RobotWorld Lab

Follow these instructions to permanently fix your CLASSPATH.

Note:

  1. You should do these problems with a partner using paired programming. Here is some information on pair programming.
  2. When you have finished the lab, you may start the assignment.

Learning Exercises

  1. What does the following program do? When the program has finished, which cell will the Robot be in? Which cell will the Beeper be in?

    Mystery.java
    public class Mystery
    {
       public static void main(String [] args)
       {
          World eerie = new World();
          Robot x = new Robot();
    
          eerie.addWall(5, 3, 3, "north");
          eerie.addWall(7, 3, 2, "north");
          eerie.addBlock(6, 3);
          eerie.addBeeper(6, 4);
    
          eerie.add(x);
    
          x.move();
          x.move();
          x.move();
          x.move();
          x.turnLeft();
          x.move();
          x.move();
          x.move();
          x.move();
          x.move();
          x.move();
          x.turnLeft();
          x.turnLeft();
          x.turnLeft();
          x.move();
          x.move();
          x.turnLeft();
          x.turnLeft();
          x.turnLeft();
          x.move();
          x.move();
          x.pickBeeper();
          x.turnLeft();
          x.turnLeft();
          x.move();
          x.move();
          x.turnLeft();
          x.move();
          x.move();
          x.move();
          x.move();
          x.turnLeft();
          x.move();
          x.move();
          x.move();
          x.move();
          x.move();
          x.putBeeper();
       }
    }

    Could the robot move the beeper to its final position using fewer instructions? Explain.

  2. Consider the following Robot class
    Dancer.java
    public class Dancer extends Robot
    {
       void toTheLeft()
       {
          move();
          move();
          turnLeft();
          move();
       }
    
       void toTheRight()
       {
          move();
          move();
          turnLeft();
          turnLeft();
          turnLeft();
          move();
       }
    }

    This Robot is then placed in a world controlled by the following program

    Choreographer.java
    public class Choreographer
    {
       public static void main(String [] args)
       {
          World danceFloor = new World();
          Dancer suzi = new Dancer();
    
          danceFloor.add(suzi);
    
          suzi.toTheLeft();
          suzi.toTheRight();
          suzi.toTheLeft();
          suzi.toTheRight();
          suzi.toTheLeft();
    
          // Finish with two rights
          suzi.toTheRight();
          suzi.toTheRight();
       }
    }

    When the program is executed, where does suzi end up?

    If the Dancer's toTheRight method definition is changed as shown below (the first move() instruction is deleted)

       void toTheRight()
       {
          move();
          turnLeft();
          turnLeft();
          turnLeft();
          move();
       }
    

    And the Choregrapher program is run again, where does suzi end up this time? Explain the difference.

  3. Create a class called CleverRobot that extends Robot. Add two methods, turnAround() which turns the Robot through 180 degrees, and move5() which moves the Robot forward 5 squares.

  4. Create a world, add a CleverRobot instance (defined in the previous exercise) to the World and, using the CleverRobot's instructions turnAround() and move5(), make the Robot move to cell(10, 5), turn around and retrace its steps to return to cell (0, 0).

    What are the advantages of using methods in this case? Are there any disadvantages?

    What are the fewest number of Robot instructions required to do task?

    What are the fewest move() instructions required to do the job?

  5. Extra Credit
    Consider the following class definition
    Mover.java
    public class Mover extends Robot
    {
       void move2()
       {
          move();
          move();
       }
    
       void move6()
       {
          move2();
          move2();
          move2();
       }
    }

    This robot is created and used in the following program:

    GoingPlaces.java
    public class GoingPlaces
    {
       public static void main(String [] args)
       {
          World stage = new World();
          Mover jim = new Mover();
    
          stage.add(jim);
    
          jim.move6();
          jim.turnLeft();
          jim.move6();
    
          // three turnLeft instructions to turnRight
          jim.turnLeft();
          jim.turnLeft();
          jim.turnLeft();
    
          jim.move6();
          jim.turnLeft();
          jim.move6();
       }
    }



  6. Extra Credit
    Write the program that gets the robot to pick up a rectangle of beepers. Use methods to help reduce the amount of typing you do. To save typing in code to add all the beepers, you can use the following supplied code.

Assignment

To see the assignment follow these instructions.