Karel J Robot - Chapter 2: Simple Robot Programs               BACK TO KAREL MAIN



Simple Robot Programs
The basic robot class is called UrRobot (see definition below). An UrRobot will only respond top the five commands listed. In order to perfrom tasks, the robot must be given combinations of these commands which, when put together in the appropriate order, complete the given task.

The UrRobot Class of Robots

class UrRobot
{
    void move() // move forward one space
    void turnOff() // turn the robot off
    void turnLeft() // rotate 90 degrees counterclockwise
    void pickBeeper() // pick a beeper up at current position
    void putBeeper() // put a beeper down at current position
}



An Example
Below is a program that uses the UrRobot class of robot. Note that the important part is shown in blue. We begin the program by defining and placing a robot into the world. The statement

UrRobot Karel = new UrRobot(1, 2, East, 0);

defines a UrRobot named Karel at location (1,2), facing East with 0 beepers. After that, the rpbpt is instructed to move twice, pick up a bepper, move again, etc.... When the robot has completed its task, it is shut down using the command turnOff.


A Sample Program

public class Tester implements Directions
{
    public static void main(String [] args)
    {
        UrRobot Karel = new UrRobot(1, 2, East, 0);
        Karel.move();
        Karel.move();
        Karel.pickBeeper();
        Karel.move();
        Karel.turnLeft();
        Karel.move();
        Karel.move();
        Karel.putBeeper();
        Karel.move();
        Karel.turnOff();

    }
}

Program 2-1: The StairClimber Class
Karel must learn to climb stairs, picking up a beeper from each step. See the initial and final situations below.

World File: fig3-1.kwld                                                                BACK TO KAREL MAIN

Program 3-2: The ShelfStocker Class
Karel just got a new job stocking grocery store shelves. He has to take the items off of the floor and place them on the shelf. He should begin and end the task at (1,1). See the initial and final situations below.

         

Initial Situation                                Final Situation

World File: Shelf.kwld                                                                      BACK TO KAREL MAIN

Program 3-3: The Harvester Class
Karel must “harvest” a field of beepers. Each row of will consist of 5 beepers, and the number of rows will always be an even number. See the initial and final situations below.

               

World File: fig3-2.kwld                                                                     BACK TO KAREL MAIN

Program 3-4: The Pinsetter Class
Karel just got a job as a pinsetter at the local bowling alley. His job requires him to set up 10 pins in the normal configuration. See the initial and final situations below.

World File: blank.kwld                                                                     BACK TO KAREL MAIN

Program 3-5: The Mover Class
Karel is rearranging office furniture. he needs to move employees desks from one cubicle to the one adjacent to it. He will then move on and do the next desk, etc... . Beginning and ending situations are as shown below. Karel begins the task at (1, 3) facing East.

          

World File: mover.kwld                                                                         BACK TO KAREL MAIN

Program 3-6: The Planter Class
Karel will be planting flowers in planter boxes. He will pick up 3 plants at the bottom of each planter box and then place them in a column in the boxes as shown in the example task below. Karel begins the task at (1, 1) facing East.

           

World File: planter.kwld                                                                       BACK TO KAREL MAIN