Differences Between Karel++ and Karel J. Robot

CIS 6:  Introduction to Computer Programming
Spring 2001

 
This is intended to help students who have been unable to buy the required textbook Karel++:  A Gentle Introduction to the Art of Object-Oriented Programming.

Chapters 1 and 2 of Karel++ can be read online at the Karel++ home page:

http://csis.pace.edu/~bergin/karel.html#toc

For Chapter 3 and beyond, there is an online version of the book which is based on Java, instead of C++.  It is titled Karel J. Robot:  A Gentle Introduction to the Art of Object-Oriented Programming in Java.  It can also be read online at the Karel++ home page:

http://csis.pace.edu/~bergin/KarelJava/Karel++JavaEdition.html

The online book Karel J. Robot is a useful, temporary alternative to the required textbook Karel++.  There are differences, however, between the C++ and Java versions of the robot programming language.  Some of the differences are summarized below.

The C++ version of the robot programming language must be used with the Karel++ simulator.
 

Class Declaration and Instruction Definition
 
In the C++ version of the robot programming language, new instructions for a class are defined outside of the class declaration: In the Java version of the robot programming language, new instructions for a class are defined inside of the class declaration:
 
// C++ version of Stair_Sweeper class
// defined in Section 3.5

class Stair_Sweeper : ur_Robot
{
    void climbStair();
    void turnRight();

}; // semicolon needed here

void Stair_Sweeper::climbStair()
{
    turnLeft();
    move();
    turnRight();
    move();
}

void Stair_Sweeper::turnRight()
{
    turnLeft();
    turnLeft();
    turnLeft();
}
 
// Java version of Stair_Sweeper class
// defined in Section 3.5

class Stair_Sweeper extends ur_Robot
{
    void climbStair()
    {
        turnLeft();
        move();
        turnRight();
        move();
    }

    void turnRight()
    {
        turnLeft();
        turnLeft();
        turnLeft();
    }

} // no semicolon needed here
Note that above there are also other slight differences in the two versions of the robot programming language.
 

Task Definition
 
There are differences in the robot delivery statements at the beginning of the task definition:
 
// C++ version of Stair_Sweeper task
// defined in Section 3.5

task
{
    Stair_Sweeper Alex(1, 1, East, 0);

    Alex.climbStair();
    Alex.pickBeeper();
    Alex.climbStair();
    Alex.pickBeeper();
    Alex.climbStair();
    Alex.pickBeeper();
    Alex.turnOff();
}
 
// Java version of Stair_Sweeper task
// defined in Section 3.5

task
{
    Stair_Sweeper Alex = new Stair_Sweeper(1, 1, East, 0);

    Alex.climbStair();
    Alex.pickBeeper();
    Alex.climbStair();
    Alex.pickBeeper();
    Alex.climbStair();
    Alex.pickBeeper();
    Alex.turnOff();
}
 

Modifying Inherited Instructions
 
When using an overridden instruction in the C++ version of the robot programming language, the name of the class defining the overridden instruction is specified, followed by two colons and the name of the overridden instruction. When using an overridden instruction in the Java version of the robot programming language, the name of the parent class defining the overridden instruction is not specified.  Instead, the word super is used, followed by a period and the name of the overridden instruction.  The word super is used because the parent class is also known as the "superclass."  The derived class is also known as the "subclass."
 
// C++ version shown in Section 3.6

class Mile_Mover : ur_Robot
{
    void move();
};

void Mile_Mover::move
{
    ur_Robot::move();
    ur_Robot::move();
    ur_Robot::move();
    ur_Robot::move();
    ur_Robot::move();
    ur_Robot::move();
    ur_Robot::move();
    ur_Robot::move();
}
 
// Java version shown in Section 3.6

class Mile_Mover extends ur_Robot
{
    void move()
    {
        super.move();
        super.move();
        super.move();
        super.move();
        super.move();
        super.move();
        super.move();
        super.move();
    }
}
 

File Inclusion
 
 
// C++ version shown in Section 3.9

#include "Harvest.r"
#include "FHarvest.r"
 
// Java version shown in Section 3.9

import Harvester
import Field_Harvester
 

Type of Value Returned by Predicate Functions
 
In the C++ version of the robot programming language, the capitalized word Boolean is used (see Section 4.2). In the Java version of the robot programming language, the uncapitalized word boolean is used (see Section 4.2).