Karel%20J.%20Robot - PowerPoint PPT Presentation

About This Presentation
Title:

Karel%20J.%20Robot

Description:

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 ... – PowerPoint PPT presentation

Number of Views:204
Avg rating:3.0/5.0
Slides: 36
Provided by: dsl84
Category:
Tags: 20j | 20robot | karel | multi | robot

less

Transcript and Presenter's Notes

Title: Karel%20J.%20Robot


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

2
Chapter 1
  • The
  • Robot
  • World

3
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.

4
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.

5
Whats 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
6
A Robot in its World
7
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.

8
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.

9
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.

10
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 robots 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.

11
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 robots exact location?
  5. How many robots can we have in a given robot
    world?

12
Chapter 2
  • Primitive Instructions and Simple Programs

13
How Do We Tell the Robot What to Do?
  • We give the robot instructions.
  • A robot executes an instruction by performing the
    instructions associated action or actions.
  • The robot executes a program by executing a
    sequence of instructions that are given to it by
    the helicopter pilot.

14
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.

15
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.

16
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.

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

18
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
19
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

20
The Complete Program
  • /
  • main.java
  • Title Chapter 2 p. 5
  • Description Move a beeper from position 1,4
    to position 3,5
  • _at_author Mrs. Houston
  • _at_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)

21
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.

22
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.

23
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.

24
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
25
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.

26
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.

27
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 indention
  • karel.move() // notice the comments
  • karel.move()
  • karel.putBeeper()
  • karel.move()
  • karel.turnOff() //
    always the last statement

Braces must match
28
Style Issues
  • Programs should be indented nicely.
  • Comments should be included.
  • Programs should be well organized.
  • Programs should be easy to read.

29
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()

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

31
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()

32
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

33
Bugs and Debugging
  • In programming jargon, all types of errors are
    known as bugs.
  • Removing the errors from a program is known as
    debugging.

34
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.

35
Problems p. 13
  1. Are there any errors? Does the program complete
    the task? If not, correct the program.
  2. Find the errors.
  3. What is the smallest program?
  4. Walk around the block.
  5. Get the newspaper
  6. Climb the mountain.
  7. Ripped bag.
  8. Rearrange beepers.
  9. Shuttle race.
Write a Comment
User Comments (0)
About PowerShow.com