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

Simple Robot Programs
The basic robot class is called Robot (see definition below). A Robot 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 Robot Class of Robots

class Robot
    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

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

defines a UrRobot named Karel at location (1,2), facing East with 0 beepers. After that, the robot is instructed to move twice, pick up a beeper, 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)
        Robot Karel = new Robot(1, 2, East, 0);


Program 2-1: The Newspaper Retrieval Task
Every morning Karel is awakened in bed when the newspaper, represented by a beeper, is thrown on the front porch of his house. Program Karel to retrieve the paper  and bring it back to bed. See the initial situation below.

Newspaper Task

World File: fig2-8.kwld                                                                BACK TO KAREL MAIN

Program 2-2: Plant the Flag!
Karel has to scale a mountain, plant a flag, then descend the mountain on the other side. Karel must begin the task with a flag-beeper in his beeper bag. Karel must also follow the path shown in the diagram below - he is climbing, not flying!


Initial Situation                                Final Situation

World File: fig2-9.kwld                                                                      BACK TO KAREL MAIN

Program 2-3: The Leaky Shopping Bag
On the way home from grocery shopping, Karel's bag ripped open, leaking a few items. The items are represented by - you guessed it - beepers. Karel needs to go back and pick these items up and return to his starting position. See the initial situation below.

shopping bag


World File: fig2-10.kwld                                                                     BACK TO KAREL MAIN

Program 2-4: The Rearranger
Karel must rearrange a column of beepers into a row of beepers. This requires picking the beepers up, then placing back down as seen in the example Note that the middle beeper does not change position. See the initial and final situations below.


World File:fig2-11.kwld                                                                     BACK TO KAREL MAIN

Program 2-5: The Initializer
Karel must spell out your inititals using beepers. Make the initials at least 5 beepers high. See below for an example using my initials, DRS. Start with an infinite number of beepers (recall that is done with a -1).


World File: blank2.kwld                                                                         BACK TO KAREL MAIN