SuperKarel
Introducing SuperKarel
Using SuperKarel
// Regular Karel
public class HurdleKarel extends Karel
// SuperKarel
public class HurdleKarel extends SuperKarelA SuperKarel Example
/*
* This program has Karel run a race by jumping
* two hurdles. This is a SuperKarel program which
* means turnRight() is built in.
*/
public class HurdleKarel extends SuperKarel
{
/*
* This method is our main entry point for the program.
* Karel runs to the hurdle and jumps it twice, before
* finishing the race.
* Precondition: Karel should be in the bottom left
* corner, facing east.
* Postcondition: Karel should be at the end of the race.
*/
public void run()
{
runToHurdle();
jumpHurdle();
runToHurdle();
jumpHurdle();
runToFinish();
}
/*
* This method has Karel jump over a hurdle that
* is one row tall.
* Precondtion: Karel is right next to a hurdle, facing east.
* Postcondition: Karel has jumped over the hurdle and is on
* the other side, facing east.
*/
private void jumpHurdle()
{
turnLeft();
move();
turnRight();
move();
turnRight();
move();
turnLeft();
}
/* This method has Karel move four times to finish the race.
* Precondition: Karel has finished jumping the last hurdle.
* Postcondition: Karel is at the end of the race.
*/
private void runToFinish()
{
// This method has Karel move four times to finish the race.
move();
move();
move();
move();
}
/*
* This method has Karel run to a hurdle that is three spots
* away.
* Precondition: Karel is three spaces away from a hurdle,
* facing east.
* Postconding: Karel is right next to a hurdle, ready to jump
* over it.
*/
private void runToHurdle()
{
move();
move();
move();
}
}Last updated