Karel J Robot Chapters 1 and 2 PowerPoint

advertisement
Karel J. Robot
A Gentle
Introduction to the
Art of Object
Oriented
Programming
Chapter 1
The
Robot
World
Modeling a Robot

Karel J Robot
– A Java class, with a graphical interface, used
to illustrate concepts we will study in this
course.
–
We will learn how to write instructions
(programs) so that our programmable robots
can perform the tasks we give them.
The World







Great flat plane with north, south, east, west
compass directions.
Bounded on the west side by an infinitely long
vertical wall extending northward.
Bounded on the south by an infinitely long
horizontal wall extending eastward.
Streets run horizontally (east-west)
Avenues run vertically (north-south)
Streets and avenues have numbers.
Origin is the corner of 1st street and 1st avenue.
What’s In The World

Robots
–
–
–

Wall Sections fabricated from the impenetrable metal
neutronium.
–
–
–

Occupy a corner.
One or more robots can occupy any corner at the same time.
Can face any one of the four compass directions.
Positioned between adjacent street corners.
Block a robots path from one corner to the next.
Used to represent obstacles, such as hurdles and mountains.
Beepers
–
–
–
2
Small plastic cones that emit a quiet beeping noise.
Located on street corners.
Robots can pick them up, carry them, or put them down.
A Robot in its World
N
Beeper
Robot
facing east
Wall
S
t
r
e
e
t
Corner
Origin
Avenue
1st St.,
1st Ave.
Fundamental Robot Behaviors

Robots are mobile.
–
–
–

Can move forward in the direction it is facing, from
corner to corner.
Can turn in place.
Can turn itself off.
Robots can detect.
–
–
–
Walls ½ block in front of them.
Can hear a beeper only if it is on the same corner as
one.
Robots on the same corner with it.
Fundamental Robot Behaviors

Robots can navigate by detecting the direction
it is facing (north, south, east, west).

Robots can manipulate beepers.
–
–
–
By carrying them.
By picking them up, and putting them down.
By knowing if it is carrying any.
Tasks

A task is something that we want a robot
to do. Some examples are:
–
–
–
–
–
–
Move to the corner of 15th Street and 10th
Avenue.
Run a hurdle race (jump over wall sections)
Escape from an enclosed room that has a
door.
Find a beeper and place it at the origin.
Escape from a maze.
Harvest rows of beepers.
Situation


A situation is an exact description of what the
world looks like.
Situations are specified by a small map or brief
written description.
–
–
–
What is each robot’s current position?
What is the location and length of each wall section in
the world?
What is the location of each beeper in the world?
See p. 5 for sample situations.
Problems p. 5
1.
2.
3.
4.
5.
Which of the following directions can a robot
face? Northeast, East, South-Southwest,
North, 164 degrees, Vertical, Down
What objects other than robots can be found
in the robot world?
Which of the objects listed in #2 can a robot
manipulate or change?
What reference points can be used in the robot
world to describe a robot’s exact location?
How many robots can we have in a given robot
world?
Chapter 2
Primitive
Instructions and
Simple Programs
How Do We Tell the Robot
What to Do?



We give the robot instructions.
A robot executes an instruction by
performing the instruction’s associated
action or actions.
The robot executes a program by
executing a sequence of instructions that
are given to it by the helicopter pilot.
Fundamental Robot Methods

Changing position
move()



Moves forward one block (faces the same
direction).
Will not move forward if it sees a wall section or
boundary wall between its current location and the
corner to which it would move.
Error-shutoff if the path is blocked by a wall.
turnLeft()


Pivots 90 degrees to the left (counter-clockwise).
Stays on the same corner.
Fundamental Robot Methods

Finishing a Task
turnOff()


Robot turns off and is incapable of executing any
new instructions until restarted on another task.
Must be the last instruction executed in the
program.
Fundamental Robot Methods

Handling beepers
pickBeeper()



Attempts to pick up beeper on the corner it is standing
and puts it in the beeper bag.
An error shutoff if no beeper is there.
If more than one beeper on the corner the robot picks up
only one.
putBeeper()



Attempts to take out a beeper from the beeper bag and
place it on the corner it is standing.
An error shutoff if no beeper in the beeper bag.
If more than one beeper in the beeper bag the robot puts
down only one.
Constructing New Robots



Use the command new
Give the robot a name.
Tell the helicopter pilot where to place
the robot, what direction it should face,
and how many beepers are in the beeper
bag.
UrRobot karel = new UrRobot(1, 2, East, 0);
The First Task

Have a robot named Karel transport the beeper from 1st
Street and 4th Avenue to 3rd Street and 5th Avenue. After
Karel has put down the beeper, it must move one block
farther north before turning off.
Initial Situation
Final Situation
The Instructions
task
{ UrRobot karel = new UrRobot(1, 2, East, 0);
karel.move();
karel.move();
karel.pickBeeper();
karel.move();
karel.turnLeft();
karel.move();
karel.move();
karel.putBeeper();
karel.move();
karel.turnOff();
}
// trace to see that it works
The Complete Program
/**
* main.java
*
* Title: Chapter 2 p. 5
* Description:
Move a beeper from position 1,4 to position 3,5
* @author
Mrs. Houston
* @version
August 26, 2013
*/
package kareltherobot;
public class Main implements Directions {
public static void task() {
UrRobot karel = new UrRobot(1, 2, East, 0);
karel.move(); karel.move();
karel.pickBeeper();
karel.move(); karel.turnLeft();
karel.move();
karel.move(); karel.putBeeper(); karel.move();
karel.turnOff();
}
// Main entry point
static public void main(String[] args) {
World.setDelay(100);
World.readWorld("first.txt");
task();
}
}
Programming Tip:

Always “trace” the program to determine what
the program does.
–
–

Simulate the program exactly as the pilot and robot
would, recording every action that takes place.
Follow the sequences of messages in the order the
pilot reads them to the robot.
When:
–
–
Trace the program before you type it in.
Trace the program when it does not perform as
expected.
Objects, behavior, and classes

In Java programming, model elements are
called objects.
–

Defining a class in Java is writing code that
specifies how objects of the class behave or act.
–
–

Objects that share common behavior are grouped into
classes.
Once a class has been defined, objects of that class
can be created.
Every object belongs to exactly one class and is an
instance of that class.
Predefined objects and classes.
–
–
Java comes with some classes already defined.
We will also use classes created by other
programmers.
Some terminology

Java uses a reference to identify an object.

Messages are sent to references, specifying
behavior with supporting details.

Reference: any phrase that is used to refer to
an object.

Messages: a request for some desired behavior
from an object.
Sending a message

To send a message, we must specify the object
and the behavior for that object.
–
–
–

A reference to the receiver object
A period
The message to be sent
To send a message to a Robot:
karel.move();
reference
message
Java Syntax Issues

Special symbols
–
Semi-colon
–
Braces
Period
–

;
{ }
.
Separates one instruction from
the next
Group blocks of instructions
Show which instructions belong to
which robot
Identifiers
–
–
–
Used for robot and class names.
Made up of characters (A..Z, a..z), digits (0..9), and
underscore (_)
Must start with a character.
Java Syntax Issues

Reserved words
–
–

Used to structure and organize primitive instructions.
Their use is reserved for their built-in purpose, nothing
else.
Comments
//
/*
–
single line comments
multi line
comments
*/
Provide explanation of what is going on to other
people who read our programs.
Look Again At The Program
task must have one main task block
{
UrRobot karel = new UrRobot(1, 2, East, 0);
/* delivery instruction (start street, avenue, direction, # beepers in bag
all instructions must be given to Karel, the only robot in the world */
karel.move();
karel.move();
karel.pickBeeper();
//
all statements must end with a ;
Braces
must
karel.move();
match
karel.turnLeft();
//
notice the indention
karel.move();
//
notice the comments
karel.move();
karel.putBeeper();
karel.move();
karel.turnOff();
//
always the last statement
}
Style Issues




Programs should be indented nicely.
Comments should be included.
Programs should be well organized.
Programs should be easy to read.
Example of Poor Indentation
task {UrRobot karel = new UrRobot(1, 2, East, 0);
karel.move(); karel.move(); karel.pickBeeper();
karel.move(); karel.turnLeft(); karel.move(); karel.move();
karel.putBeeper(); karel.move(); karel.turnOff(); }
Errors Shutoffs

Robot turns itself off when it is prevented
from successfully completing the action
associated with a message.

Examples:
–
–
–
Illegal move (path is not clear)
putBeeper (no beepers in the bag)
pickBeeper (no beepers on the corner)
Programming Errors

Lexical errors
–
When a robot encounters an instruction that is not part of its
vocabulary



mvoe() instead of move(); turnleft() instead of turnLeft()
Yes, capitalization counts; Java is case sensitive
Syntax errors
–
Use of incorrect grammar or punctuation




An instruction missing a semi-colon at the end.
Instruction missing () at the end.
Instruction given without a reference.
Instruction given without period.
Karel.move()
Karel.move;
move();
Karel move();
Programming Errors

Execution errors
–
Occur whenever a robot in the world is unable to execute an
instruction successfully and is forced to perform an error
shutoff.





Instructing the robot to move when the front is blocked.
Trying to pickBeeper when the corner has no beepers.
Trying to putBeeper when the bag is empty.
All result in an error shutoff.
Intent error
–
Program seems to run successfully, but does not accomplish
the task.


Task: pickup the beeper, face north, move one block
Instead, the robot picks up the beeper, and moves ahead two
blocks
Bugs and Debugging


In programming jargon, all types of
errors are known as “bugs”.
Removing the errors from a program is
known as “debugging”.
A Task For Two Robots
Task: Karel is at 3rd street and 1st avenue on a corner with
a beeper facing east. Jane is at the origin facing east.
Karel should carry the beeper to Jane and put it down.
Jane should then pick it up and carry it to 1st street and
3rd avenue. The beeper should be placed on this corner.
Problems p. 13
1.
2.
3.
4.
5.
6.
7.
8.
9.
Are there any errors? Does the program
complete the task? If not, correct the
program.
Find the errors.
What is the smallest program?
Walk around the block.
Get the newspaper
Climb the mountain.
Ripped bag.
Rearrange beepers.
Shuttle race.
Download