15-100: Introductory/Intermediate Programming (Section F)
Fall 2006
Exercise   
08/28/2006
written Robot trace
Due:  Wednesday, August 30
beginning of class

Relevant Reading:

  • Chapters 1 and 2 of the Karel J. Robot text.
    • Note that in Chapter 2, there are several places where the street or avenue are off by one.  For instance, on page 11, the Initial Situation in Figuer 2.3 shows the robot on street 2, while the text below says it should be on street 1.  EXTRA CREDIT:  turn in a complete list of places in Chapter 2 where there is a typo.
  • On-line references for using the software.

Assignment:  (worth 10 exercise points)

1)  Start getting the software necessary for this course.  In particular, get the Java SDK software from http://java.sun.com/javase/downloads/index.jsp.

2)   Java programs look very much like English paragraphs though written in a very stilted and awkward style.  With some thought, study, and reasoning you should be able to read through a Java program and get an idea of what the program is doing.    For this exercise, you will read 4 Java programs and determine what they do.  Trace the execution of the program on paper very carefully on the scratch sheet. Each program runs to a successful termination – there are no errors or crashes.    For each program, you are given instructions for how to draw your answer.

We are asking a great deal of you on the first day of class and for the first assignment. We know that. In order to do well in this class, you will sometimes need to be able to work in situations where you know less than what could be considered ideal.  You will find yourself frequently looking at code trying to figure out what it is doing.  You may talk to classmates about this.   However, you should be writing up your own answers.


Handin:
Bring all of your papers to class on Wednesday.  You will turn in a copy of your “final situations” page at the start of class.   Be sure your name is on it.


Part A
Part B
Part C
Part D



Part A:  4 points

Specifications:
Draw the following on the sheet marked “final situations”:
  • Locate and mark the starting position of the Robot object with an “R”.
  • Locate and mark each beeper position placed by the Robot object with a “O”,
  • If more than one beeper is placed on the same intersection, color in the “O”.
  • Locate the final position of the robot when the program is finished with a “V” pointing in the direction the robot is facing; an east facing Robot would be marked with a “>”.
  • Mark up the scratch copy of the world as you trace the code.  Then make a clean copy of the final situation on the last page in the appropriate box.

package kareltherobot;

public class Application implements Directions
{    
    public static void main(String [ ] args)
    {    
    World.setVisible(true);
    World.readWorld("worldA.txt");
    World.setTrace(true);
    World.setDelay(10);
       
    Robot karel = new Robot(1, 1, East, 25);
    karel.putBeeper();
    karel.move();
    karel.putBeeper();
    karel.move();
    karel.putBeeper();
    karel.turnLeft();
    karel.putBeeper();
    karel.move();
    karel.putBeeper();
    karel.move();
    karel.putBeeper();
    karel.turnLeft();
    karel.putBeeper();
    karel.move();
    karel.putBeeper();
    karel.move();
    karel.putBeeper();
    karel.turnLeft();
    karel.putBeeper();
    karel.move();
    karel.putBeeper();
    karel.turnLeft();
    karel.move();
    karel.move();
    karel.move();
    karel.turnOff();
    }// end of method main
} // end of class Application


 
 
Part B:  4 points

Specifications:
Draw the following on the sheet marked “final situations”:
  • Locate and mark the starting position of the Robot object scott with an “S” and Jim with a “J”.
  • Locate and mark each beeper position placed by the Robot object with a “O”,
  • If more than one beeper is placed on the same intersection, color in the “O”.
  • Locate the final position of the robots when the program is finished.  Mark Scott  with an “S" and Jim with a "J".   For each, add an arrow pointing in the direction the robot is facing.  For example, if Scott finishes facing East, you would use S>.
  • Mark up the scratch copy of the world as you trace the code.  Then make a clean copy of the final situation on the last page in the appropriate box.

package kareltherobot;

public class Application implements Directions
{    
    public static void main(String [ ] args)
    {    

    World.setVisible(true);
    World.readWorld("worldB.txt");
    World.setTrace(true);
    World.setDelay(10);
       
    Robot scott = new Robot(2, 2, North, 10);
    scott.putBeeper();
    scott.move();
    scott.putBeeper();
    scott.move();
    scott.putBeeper();
    scott.move();
    scott.putBeeper();
    scott.move();
    scott.turnLeft();
    scott.turnLeft();
    scott.move();
    scott.move();
    scott.move();
    scott.move();
    scott.move();
    scott.turnOff();

    Robot jim = new Robot(3, 1, East, 0);
    jim.move();
    jim.pickBeeper();
    jim.move();
    jim.turnLeft();
    jim.putBeeper();
    jim.move();
    jim.move();
    jim.move();
    jim.move();
    jim.turnOff();

    }// end of method main
} // end of class Application


 
 Part C:   1 point

This has things not contained in the reading.   Read the program as English and try to figure out what it is doing.

Specifications:
Draw the following on the sheet marked “final situation”:
  • Locate and mark the starting position of the Robot object with a “V” to show the direction the robot is facing.
  • Locate and mark each beeper position placed by the Robot object with a “O”,
  • If more than one beeper is placed on the same intersection, color in the “O”.
  • Locate the final position of the robot when the program is finished using an arrow to indicated the direction the robot is facing.
  • Mark up the scratch copy of the world as you trace the code.  Then make a clean copy of the final situation on the last page in the appropriate box.


package kareltherobot;

public class Application implements Directions
{    
    public static void main(String [ ] args)
    {    
 
    World.setVisible(true);
    World.readWorld("worldC.txt");
    World.setTrace(true);
    World.setDelay(10);
       
    Robot boring = new Robot(3, 1, East, 5);
    boring.move();
    boring.move();
    while (boring.anyBeepersInBeeperBag() )
    {
        boring.putBeeper();
        boring.move();
    }
    boring.turnLeft();
    if (boring.frontIsClear() )
    {
        boring.move();
    }
    while (boring.anyBeepersInBeeperBag() )
    {
        boring.move();
    }

  }// end of method main
} // end of class Application







 
 Part D:  also only 1 point

More stuff not from the reading.  This one could be tough.    Read it, think about it, and give it a try.

Specifications:
For of the program draw the following on the sheet marked “final situation”:
  • Locate and mark the starting position of the Robot object with a “V” to show the direction the robot is facing.
  • Locate and mark each beeper position placed by the Robot object with a “O”,
  • If more than one beeper is placed on the same intersection, color in the “O”.
  • Locate the final position of the robot when the program is finished using an arrow to indicated the direction the robot is facing.
  • Mark up the scratch copy of the world as you trace the code.  Then make a clean copy of the final situation on the last page in the appropriate box.
package kareltherobot;

public class Application implements Directions
{
   Robot confusing = new Robot( 4, 1, East, 42); 
 
   public static void main(String [ ] args)
   {    
 
    World.setVisible(true);
    World.readWorld("worldD.txt");
    World.setTrace(true);
    World.setDelay(10);
       
    Application app = new Application( );    
    app.putBeeperAndMove(7);
    app.moveToNextRow();
    app.putBeeperAndMove(4);
    
  }// end of method main

  public void putBeeperAndMove(int numberOfBlocksToMove)
  {
    int i = 0;
     while ( i < numberOfBlocksToMove)
    {
        confusing.putBeeper();
        confusing.move();
        i = i + 1;
    }
  }// end of method putBeeperAndMove

  public void moveToNextRow()
  {
    confusing.turnLeft();
    confusing.move();
    confusing.turnLeft();

  }// end of method moveToNextRow
} // end of class Application