36
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Embed Size (px)

Citation preview

Page 1: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Karel J. RobotA Gentle Introduction to the Art of Object Oriented Programming

Page 2: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Chapter 1

The Robot

World

Page 3: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 4: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 5: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

What’s In The World

Robots– Occupy a corner.– One or more robots can occupy any corner at the same time.– Can face any one of the four compass directions.

Wall Sections fabricated from the impenetrable metal neutronium.

– 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– Small plastic cones that emit a quiet beeping noise.– Located on street corners.– Robots can pick them up, carry them, or put them down.

2

Page 6: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

A Robot in its World

Beeper

Robot facing east

Wall

Avenue

Street

Corner

Origin1st St.,1st Ave.

N

Page 7: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 8: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 9: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 10: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 11: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Problems p. 5

1. Which of the following directions can a robot face? Northeast, East, South-Southwest, North, 164 degrees, Vertical, Down

2. What objects other than robots can be found in the robot world?

3. Which of the objects listed in #2 can a robot manipulate or change?

4. What reference points can be used in the robot world to describe a robot’s exact location?

5. How many robots can we have in a given robot world?

Page 12: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Problems p. 5

1. Which of the following directions can a robot face? East, North

2. What objects other than robots can be found in the robot world? Walls, Beepers

3. Which of the objects listed in #2 can a robot manipulate or change? Beepers

4. What reference points can be used in the robot world to describe a robot’s exact location? Streets, Avenues

5. How many robots can we have in a given robot world? As many as our computer can hold in memory.

Page 13: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Chapter 2

Primitive Instructions and Simple Programs

Page 14: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 15: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Fundamental Robot Methods

Changing positionmove()

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.

Page 16: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Fundamental Robot Methods

Finishing a TaskturnOff()

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.

Page 17: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Fundamental Robot Methods

Handling beeperspickBeeper()

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.

Page 18: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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);

Page 19: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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

Page 20: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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

Page 21: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

The Complete Program/** * main.java * * Title: Chapter 2 p. 5 * Description: Move a beeper from position 1,4 to position 3,5 * @author Mrs. Klipp * @version August 8, 2002 */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 pointstatic public void main(String[] args) {

World.setDelay(100);World.readWorld("first.txt"); task();

}}

Page 22: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 23: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Objects, behavior, and classes

In Java programming, model elements are called objects.– Objects that share common behavior are grouped into

classes. Defining a class in Java is writing code that

specifies how objects of the class behave or act.– 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.

Page 24: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 25: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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

Page 26: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Java Syntax Issues

Special symbols– Semi-colon ; Separates one instruction from the next– Braces { } Group blocks of instructions– Period . 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.

Page 27: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 28: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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 ;karel.move();karel.turnLeft(); // notice the indentionkarel.move(); // notice the commentskarel.move();karel.putBeeper();karel.move();karel.turnOff(); // always the last statement

}

Braces must match

Page 29: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Style Issues

Programs should be indented nicely. Comments should be included. Programs should be well organized. Programs should be easy to read.

Page 30: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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(); }

Page 31: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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)

Page 32: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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. karel.move() Instruction missing () at the end. karel.move; Instruction given without a reference. move(); Instruction given without period. karel move();

Page 33: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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

Page 34: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

Bugs and Debugging

In programming jargon, all types of errors are known as “bugs”.

Removing the errors from a program is known as “debugging”.

Page 35: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

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.

Page 36: Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming

A Task For Two Robots

task{ UrRobot karel = new UrRobot(3, 1, East, 1);UrRobot jane = new UrRobot(1, 1, East, 0);karel.turnLeft();karel.turnLeft();karel.turnLeft();karel.turnLeft();karel.move();karel.move();karel.turnLeft();karel.setBeeper();jane.pickBeeper();jane.move();jane.move();jane.setBeeper();karel.shutOff();jane.shutOff();

}