pair programming assignment

while Statement

  1. Describe the difference between the following two program fragments:

       while(frontIsClear())  
       {
          move();
       }
       if(frontIsClear())  
       {
          move();
       }
  2. Write a method that will cause a robot to drop all of its beepers. Test the method in an actual world.

  3. Write a method called goToBeeper() that will cause a Robot to keep moving forward until it finds a beeper. You may assume that a beeper is somewhere in front of the robot. Use a while statement and the beeperPresent() method. Test the method in an actual world.
    Hint: you may also need the NOT operator: !.

  4. Write a method that will cause a robot to face south, no matter what direction it starts from. Use a while statement (no if statements).

  5. Using the method from the previous exercise (or some other technique), create a program which will be able to find the origin (cell 0,0) no matter where in the world it is placed. (You may assume there are no blocks or walls in the world.)

    Note: if your robot only finds the origin when placed on one particular cell, then it hasn't solved the problem. It must work no matter where it is placed.

    Here's a hint.

  6. Consider the following code

            while(frontIsClear())
            {
               // some unknown instructions   
    	      :
    	      :
            }

    You don't know which instructions are controlled by the while loop. However, Assume that the while loop has finished (i.e. there were no errors, and it was not an infinite loop), what can you say about the robot?

  7. Optional Problem

    Study both of the following program fragments separately. What does each do? For each, is there a simpler program fragment that is execution equivalent? If so, write it down; if not, explain why not.

       while(!beeperPresent())  
       {
          if(beeperPresent())
          {
             pickBeeper();
          }
          else
          {
             move();
          }
       }
       while(!beeperPresent())  
       {
          move();
       }
       if(beeperPresent())
       {
          pickBeeper();
       }	
       else
       {
          move();
       }