Appendix C


Differences between Karel++ and Java



 

The robot programming was purposely made very similar to Java since you might want to use Karel to help learn Java. There are some differences, however.

task versus main

In Karel++ we name the main task block task. In Java it is a function named main. This function may be a part of any class.

Visibility of Class features

In Karel++ local robots are assumed to be private to that robot and new instructions and predicates are assumed to be public. Java permits much more control over what is public and what is not.

Robot Initializations

In Java a robot initialization is the class name and robot name as here, but the rest different. Robots are initialized by calling a built-in function named new, so a robot initialization would look like


Mile_Walker Lisa = new Mile_Walker(3, 2, East, 0);

instead of our


Mile_Walker Lisa(3, 2, East, 0);

Polymorphism

In Java no special syntax is required to achieve polymorphism as discussed in Chapter 6. In fact, all robot names would be considered to be aliases in Java and many names could be used to refer to the same robot.

Structured statements: if, while, and loop

We required all such statements to include compound statements enclosed in { and }. Java has no such instruction. It is possible in Java to write


if (nextToABeeper())
	move();

where we require


if (nextToABeeper())
{	move();
}

This leads to a problem in Java called the "dangling else problem" which will be discussed in any good Java book.

Class Structure

In Java it is not possible to separate the instruction definitions from the class definition. In Java a class would look like the following. (See below for the word "extends".


class SomeRobot extends Robot
{	void doSomething()
	{	move();
		. . .
	}

	boolean testSomething()
	{	. . .
	}
}

Other

In Java, our Boolean is spelled boolean.

In Java, our loop instruction is replace by a much richer instruction called for.

In Java, our #include would be import, though import is only approximately the same as #include.

In Java it would be possible to have many robots moving simultaneously.

In Java, what we have called myself is called this.

In Java, inheritance is indicated by the word extends, where we have used a colon.

In Java, the semicolon after each class declaration is not required.