CMSC 280
Fall 2017
08/23/2017
Code Reading
Due:  Fri, August 25
beginning of class

Relevant Reading:

  • Chapter 1 of the Karel J Robot book.
  • Skim Chapter 2 of the Karel J Robot book for the ideas. Details can come later.
    •  Note that in some early versions of the book, 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 Figure 2.3 shows the robot on street 2, while the text below says it should be on street 1.
    • Also, we will be using Robot rather than UrRobot.

Assignment: (worth 5 exercise points)

For this exercise, you may work in pairs. If you do so, hand in only one copy.

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.

1) There are four programs below (labeled A, B, C, and D). 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.

The picture file (in case you want to download it)

2) OPTIONAL:  AFTER YOU HAVE COMPLETED PART 1: download a zip file for each of the programs and run the program using Eclipse. On the BACK of the sheet, briefly indicate (in words and/or pictures) how your predictions differed from the end answers. You should NOT modify your answer to part 1 based on this.
PartA.zip PartB.zip
PartC.zip
PartD.zip

Handin:

Handin your paper at the beginning of class on Friday.

The problems:

Part A: 2 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: 2 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 add a “V” pointing in the direction the robot is facing. Mark Jim with a J and a similar V. If Scott finishes facing West, 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

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 a “V” 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 (yes, that makes 6, even though this is out of 5).

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 a “V” to indicate 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