Create a new Robot with a method which will pick up a beeper if one is present, otherwise it will move forward one square. Write a test program that will create a world and place your new robot in the world. Execute your test method for each case, i.e. when there is a beeper in the cell with a robot and when there is no beeper in the cell.
Create a Robot with a method which will always turn to face north and then move forward one square. Write a Test program which creates a World and place four of your Robots on the world, each facing a different direction. Verify that the robots all do as required.
Which robot executes the most instructions? Which executes the fewest instructions?
Consider the following Robot
public class IfRobot extends Robot
{
void go()
{
if(beeperPresent())
{
pickBeeper();
move();
}
else
{
turnLeft();
move();
}
}
}
|
Can the statements in the go() method be rearranged so that the same effect is achieved using fewer statements?
Hint: the move() is executed whether or not a beeper is present, could it be moved outside the if statement? If you do rearrange the code, ensure that it still does the same thing.
Consider the following three Robot definitions
public class BeeperSeeker1 extends Robot
{
void go()
{
if(!beeperPresent())
{
move();
}
else
{
pickBeeper();
}
}
}
|
public class BeeperSeeker2 extends Robot
{
void go()
{
if(!beeperPresent())
{
move();
}
if(beeperPresent())
{
pickBeeper();
}
}
}
|
public class BeeperSeeker3 extends Robot
{
void go()
{
if(beeperPresent())
{
pickBeeper();
}
if(!beeperPresent())
{
move();
}
}
}
|
Their go() methods are slightly different. Will they always do the same thing? Try and imagine a situation where they would act differently. If you can't imagine, then check this test.