JKarel Notes


JKarel Built-inMethods for UrRobot Class

Command Effect
move(); Move Robot one unit in current direction.
turnLeft(); Robot turns 90 degrees left.
pickBeeper(); Robot picks up a beeper.
putBeeper(); Robot puts down a beeper.
turnOff(); Turns off robot.

Simple Robot Program: main task

// file: Mover.jkl

class Mover {
main {
      UrRobot karel = new UrRobot (1,1,East,0);
      // move one block
      karel.move();
      karel.turnOff();
  }
}

The class name and the file name must be identical (in the above example the class / file name is Mover).

Instantiating a Robot

syntax:
className robotName = new className (street, avenue, direction, numberOfBeepers);
example:
UrRobot karel = new UrRobot (1,1,East,0);

Defining a New Class of Robots by Inheritance

/*  File: SuperRobot.jkl
    Author: Dr. Cook
    Date: May 23, 2004
    Description: Inheritance
*/

class SuperRobot extends UrRobot
{
  void turnRight()
  // new method - make a right turn

  {
    turnLeft();
    turnLeft();
    turnLeft();
  }
  main
  {
   SuperRobot alex = new SuperRobot (1, 1, North, 0);
   alex.turnRight();
   alex.turnOff();
  }
}

Extends means that SuperRobot inherits all methods from UrRobot.

Programming Errors
  • Syntax or compilation error - grammar and punctuation, upper or lower case, spelling
  • Execution or run-time error - because of the situation, the robot can't perform a method, e.g. hits a wall
  • Intent or logical or human error - the robot does not perform as expected

updated: 5/24/2004
Dr. Leah Cook