Вы находитесь на странице: 1из 20

Karel J.

Robot
A Gentle Introduction to the Art of Object-Oriented
Programming in Java
http://csis.pace.edu/~bergin/KarelJava2ed/Karel++JavaEdition.
html
Copyright, Joseph Bergin
Karel's World
A flat World
Horizontal streets running East-West
Vertical avenues running North-South

Intersection 2, 3
Walls
Karel's World
walls
impenetrable structures that robots can't get
through
beepers
objects which robots can put down, pick up and
carry

Walls
Beepers
Robot Abilities
Move
move only in the direction it is facing
moves one block at a time, from one intersection to the
next
robot at 2, 3 facing East moves to 2,4 (still facing East)
Robot Abilities
Turn left
rotate 90 degrees anti-clockwise
robot at 2, 4 facing East turns to face North (still at 2,4)
Robot Abilities
pick up a beeper
put down a beeper
Beepers can
be picked up
or put down at
2, 3
Karel J. Robot Instructions in Java

Robot karel;




karel = new Robot(2, 3, East, 1);



karel.turnOff();

// indicate that there is a robot called karel
// create the robot and describe its initial state as:
// karel at corner of 2nd St & 3rd Ave, facing East, 1 beeper in its bag
// when finished turn the robot off
Karel J. Robot Instructions in Java

karel.move();


karel.turnLeft();


karel.pickBeeper();


karel.putBeeper();
// move one block in the facing direction
// turn left on the spot
// pick up a beeper from current intersection
// put down a beeper at current intersection
Simple Karel J. Robot Program
// Move robot in a 1 x 1 block square (anti-clockwise)
Robot karel; // robot named karel

// starting on corner of 2nd St & 3rd Ave, facing
// East, with 1 beeper in it's beeper bag
karel = new Robot(2, 3, East, 1);
karel.move();
karel.turnLeft();
karel.move();
karel.turnLeft();
karel.move();
karel.turnLeft();
karel.move();
karel.turnLeft();
Karel.turnOff();
Activity 1 Check/Run Karel Program
Go to the Karel JJ website:
http://www.publicstaticvoidmain.com/cgi-
bin/sfjj.cgi?freeschool=KarelJRobot&file=intr
oToKarel.java
Top left pane contains the karel program of
the previous slide.
click Checkit to ascertain that the program
has been written correctly
click Runit to run the program
click Start
Activity 2 Square Dance with Beeper
Change the code so that Karel performs the
same square dance and puts a beeper at the
3, 4 intersection (i.e. diagonally opposite his
starting point).
Activity 3 Square Dance with 4
Beepers
How could this code be changed so that
Karel performs his square dance and puts
beepers at all points of the square?
More Robot Abilities
Is the Robot next to a Beeper?
// determine if there is a beeper on the
// corner karel is on
if (karel.nextToABeeper())
{
// if next-to-a-beeper karel instructions
}

Example
// if karel is next to a beeper, pick it up
if (karel.nextToABeeper())
{
karel.pickBeeper();
}
Activity 4 Square Dance and Pick up
any Beepers
Write a program in which Karel performs the
same square dance and picks up any
beepers he finds on corners.
To test the program:
on Runit screen: click show World Builder
(make sure the world screen is visible)
Click Beeper
add some beepers on corners of the square
Click set new world defn
Java if/else Statement
// determine if there is a beeper on the
// corner karel is on
if (karel.nextToABeeper())
{
// next-to-a-beeper karel instructions
}
else
{
// NOT next-to-a-beeper karel instructions
}

Activity 5 Square Dance and Move
Beepers
Write a program in which Karel performs the
same square dance and
picks up a beeper from any corner on which
there is one
puts a beeper on any corner on which there is
no beeper
Hint: How many beepers should Karel initially
have in his beeper bag?
Note: He does not know how many beepers
he will find on corners.
Java - repeat instruction
Activity 1
When we know how many times that we are
repeating, we can use the for loop

// repeat activities 4 times
for (int i = 0; i < 4; i++)
{
//insert activity
}
Java repeat activity 2
We can also repeat until a certain condition is false

// condition here is how many beepers left
// in the beeper bag given at the start
while (karel.anyBeepersInBeeperBag())
{
//activity to be repeated
// ensure that you put the beepers
}

Note new logical function: anyBeepersInBeeperBag()
Activity 6
To test the repeat action, write two separate
programs that
(1) gets Karel to drop a 4 beeper along a
straight line
(2) gets Karel to drop a beeper along a
straight line, until there are no more beepers
left
Activity 7 Escape from Maze
A program which has Karel escape from a
maze.
Example maze world given in file: maze.txt
copy contents into pane below use text world
defn
click use text world defn
Simple version of program in file: maze.jj.txt
copy contents into code area
Check and run the program
What is the program algorithm (design)?

Вам также может понравиться