Follow these instructions to permanently fix your CLASSPATH.
Note:
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?
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.
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
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.
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?
public class Mover extends Robot
{
void move2()
{
move();
move();
}
void move6()
{
move2();
move2();
move2();
}
}
|
This robot is created and used in the following program:
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();
}
}
|
void move2()
{
move();
}
|
i.e. it contained only one move instruction, then where would the robot end up? Explain why this occurs. Can you think of a use for this effect?
To see the assignment follow these instructions.