CS 231 Intro to Programming, Spring 2003
Lab 2: Conditional and Repeated Execution


Part 1: Practice Programming Problems

  1. Using Methods: Start with the code you wrote from the previous lab (chp 1, prob 18). Think about how you would group the robot movements into sequences of code that are meaningfully related, e.g. the lines of code for picking up first pile, or for picking up second pile, or exiting room, etc. Place these code segments into separate methods which will get called from the init method. When you are done, your init method should be very short and easy to read. When you run the program, it should do exactly what it did before, however, now the code is easier to read and easier to modify.

    Recall: To use methods, you must shift the declaration of the robot and room objects as shown in the example below:

    public class RoomCleaningSimulation extends Applet
    {
      Room   edsRoom;
      Butler james;
        
      public void init ()
      {
        edsRoom = new Room("onepile.room", this);
        james   = new Butler (11,2, edsRoom);
        edsRoom.waitForStart();
    
        // here is a sample method call:
        enterRoom();
      }
    
      // And here is a sample method:
      public void enterRoom() 
      {
         james.forward(3);
         james.turnLeft();
      }
    }

The problems below are intended to be quick and easy problems to give you practice using simple conditional and repetition statements. Doing these problems will build skills you will need for doing the harder problems.

  1. Implement the program in listing 2.3 p. 45. Note that this program makes use of two patterns: sentinel loop pattern (the while loop) and the guarded command pattern (the if statement).

    Modify this program so that if the robot runs into the basket, he twirls completely around once and then leaves the room and if he runs into the laundry he picks it up and leaves the room. Use an Alternative Action Pattern, that is, use an if-else statement.

    Modify the program in listing 2.3 so that the robot moves the basket out of the way so that he can always get to the laundry.

  2. Using the onepile.room, write a program that has the robot walk around the laundry 3 times and then exit the room. Place the code for walking once around the laundry into a method that is called from the init method. Then use a Counter Loop Pattern (see listing 2.4 p. 48 for an example of a Counter Loop Pattern) to make the robot circle the laundry 3 times before exiting the room.

  3. Using the chap2c.room with starting location (5,5), write a program that has the robot pace back and forth (without stopping) either horizontally. Your program should work regardless of the size of the room.

    Now, add a second robot and place her somewhere else in the room. Have this second robot pace back and forth vertically while the first robot is also pacing horizontally. (Wheee!!)

  4. Maze: On pp 50-52 your text describes a program for having the robot go through a maze. Replace "maze.room" with "maze1.room" in listing 2.5 on p. 52. Then modify the code so that the robot can go through this room and exit out of the top.

Part 2: Harder Programming Problems

NOTE: It turns out that problem 6 is quite tricky. If you prefer a somewhat easier problem, you may do problem 8 in place of problem 6.
  1. Maze: Replace "maze1.room" with "maze2.room" in the previous problem. There is now a basket and piles of laundry. Modify the program so that the robot carries the backet through the maze, picking up the laundry as he goes.

  2. Cleaning up a room. Do problem 24, p. 88. This is a tough problem. To work up to doing problem 24 we recommend that you work up to it by first doing problems 22 and 23. It helps to place the main commands you used in prob 22 into a method that can then be used repeatedly in 23 and 24.

  3. This is an alternative to problem 6: You've decided to make a lot of money by selling the clothes cleaning robot to a hotel chain. To strengthen your pitch, you decide to provide a demonstration of the Butler's suitability for the task.

    Program the Butler to clean three hotel rooms. The room file is in the BUTLER folder on the L drive and is called "hotel.room". Assume that all the rooms are the same shape, and that each has exactly one pile of clothes in it but you don't know where the pile is (except you know it is in the room proper, not in the hallway -- i.e. in one of the 6 locations at the top). Start the butler in position (11,7) and have him exit at the other end of the hall.

    Leave the clothes right outside the door (for the next Butler with the basket) of each room. Test your code on the new room "hotel1.room". You should not have to change anything in your code except for the room name.

What to turn in


[top]

[CS 231 Home Page]