The Karel the Robot software has been used to help teach programming concepts for several years. The current version is called Karel J Robot and is based on the Java programming language. Karel J Robot has been developed by Joseph Bergin of Pace University and builds on other work by Mark Stehlik, Jim Roberts and Richard Pattis (all of Carnegie Mellon University). Professor Bergin's Karel web site is at http://csis.pace.edu/~bergin/KarelJava2ed/Karel++JavaEdition.html The simulator and other parts of Karel will no doubt change and improve with time. Professor Bergin's site is the definitive source. I will attempt to keep up with changes but cannot guarantee this will happen.

This document explains how to use the Java class files with Visual Studio .NET and the .NET languages.

The Karel J Robot .Class files, from the .jar file, must be converted to a .dll file. This dll file holds the MSIL (Microsoft Intermediate Language) code for these classes. The code was converted using the JbImp.exe utility that comes with the Microsoft Visual J# compiler and libraries. The command I used is below (it was entered all on one line) and creates a DLL file named KarelTheRobot.dll.

"c:\program files\Microsoft visual J# .net\framework\bin\JbImp.exe"  /t:library karellib.jar     /out:KarelTheRobot.dll

My version of the library may be copied from here.

For ease of starting, complete sample Karel projects may be copied from here.

To use the Karel classes first open a new Windows project (this has been tested with both Visual Basic .NET and Visual C# Windows applications) and add a reference to the KarelTheRobot.dll and the vsjlib. Both are added using the Add Reference dialogue box from the Project menu or Visual Studio .NET.

To add the vjslib reference (which is required to support the J# libraries used by Karel) open the Project menu and select Add Reference. Use this window to find vjslib as in the picture below.

Select the library and press the Select button. The library will show up in the Select Components box.

After the vjslib has been added press the Project tab and use the browse option to find the KarelTheRobot.dll. This file will be in the directory that you copied it into (if you took a copy from my web site) or created it if you created your own.

 

Add a reference to the kareltherobot namespace (package in Java terms) to the project.

J#                     package kareltherobot;

C#                   using kareltherobot;

VB .NET    Imports kareltherobot

 

These lines should be included at the top of any program file that will use the Karel classes so that they do not have to be fully qualified. The Karel classes may now be used like any other .NET classes. They may be used as they are or as base classes that can be inherited by new classes written in other .NET languages.

I generally call the World class right after InitializeComponent in a Visual Basic .NET or C# project. For J# projects I have been using the World commands in the main function. The following table shows sample code.

 

J#

World.setDelay(20);

World.readWorld("garden.txt");

task();

C#

World.setDelay(20);

World.readWorld("garden.txt");

task();

VB .NET

World.setDelay(20)

World.readWorld("garden.txt")

task()

 

The following code shows sample inheritance code for C# and VB .NET.

 

C#

     public class myRobot : ur_Robot

     {

           public myRobot(int street, int avenue, int direction,

             int beepers) : base(street, avenue, direction, beepers)

           {

 

           }

 

           public void reverse()

           {

                turnLeft();

                turnLeft();

           }

     }

VB .NET

Public Class myRobot

    Inherits ur_Robot

    Sub New( ByVal street As Integer, ByVal avenue As Integer, _

           ByVal direction As Integer, ByVal beepers As Integer)

        MyBase.New(street, avenue, direction, beepers)

    End Sub

    Public Sub Reverse()

        turnLeft()

        turnLeft()

    End Sub

 

End Class

 Karel J Robot uses Direction constants that are defined inside an interface. Different languages handle this differently. In J#, the constants (North, East, South, and West) may be referenced by their name without qualification. In Visual Basic .NET the constants must be at least partially qualified by using the name of the interface. For example, Directions. North. Visual C# does not support constants inside interfaces and so the constants must be defined inside the program that uses them.  For example:

private const int East = 0;

private const int South = 1;

private const int West = 2;

private const int North = 3;

 

 

 

Copyright Alfred C Thompson II 2006