Title: Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith
1Karel Chapter 2 Primitive Instructions
and Simple Programs
Note Original slides provided by
www.apComputerScience.com and modified for Mr.
Smiths AP Computer Science A class
2Karel - Executing
- The robot executes an instruction by performing
the instructions associated action or actions - The robot executes a program by performing a
sequence of instructions given to it - Instructions are executed one at a time in the
order that they are listed
3Karel - Primitive Instructions
- Basic tools with which all problems are solved.
An analogy is when a carpenter performs primitive
tasks such as hammering, leveling, measuring,
sawing, etc. He can accomplish all his carpentry
tasks (some which are very complicated) using
these basic tasks. - In Java, each of these primitive instructions is
called a method. A standard robot has these five
methods - move()
- turnLeft()
- putBeeper()
- pickBeeper()
- turnOff()
4OOP-ness
- object.method1()
- object.method2()
- - A method acts on the object. It is the message
sent to the object. - - Objects are typically nouns (e.g., karel).
They either do things or remember things. - - Methods are typically verbs (e.g., move) and
represent behavior - Example karel.move()
method
object
5move()
- Moves forward one block in the direction that it
is facing (it will not move sideways) - Continues to face the same direction
- Error Shutoff if trying to move into a wall or
boundary (Ouch! Look first!)
6turnLeft()
- Stays at same corner (does not alter location)
- Turns 90 degrees to the left
- Cannot cause an error shutoff
- Note There is no turnRight() instruction
7pickBeeper()
- Picks up a single beeper on the current corner
and places it in beeper bag - It only picks up one beeper, even if there are
multiple beepers on the corner - If attempted on a beeper-less corner, it causes
an error shutoff
8putBeeper()
- Takes beeper from beeper bag and puts it down on
current corner - It only puts down one beeper, even if there are
multiple beepers in beeper bag - If attempted with an empty beeper bag, it causes
an error shutoff
9turnOff()
- Robot turns off and is incapable of executing
another instruction until restarted - This is the last instruction executed on a robot
object - After turnOff() is executed, all other
instructions are ignored
10UrRobot Description
- UrRobot class (the Model-T Robot)
public class UrRobot void move() void
turnOff() void turnLeft() void
pickBeeper() void putBeeper()
primitive You cant/dont need to define this
class it is in a library for you to use
11class
- A class is not a robot. It is a description of
robots of the same kind. - The simple model of the robot described above is
the UrRobot class - By convention classes begin with a capital letter
- A class is like a production line in a factory
that makes the same robots
12Sample Client Program
- import kareltherobot.
- public class KarelSample implements Directions
-
- public static void main(String args )
-
- UrRobot karel new UrRobot(2, 1, East, 1)
- karel.move()
- karel.move()
- karel.turnLeft()
- karel.putBeeper()
- karel.move()
- karel.turnOff()
-
- static
-
- World.setVisible(true)
- World.showSpeedControl(true)
-
Names for this type of program are Client
program, Driver program, Tester program
13Demo KarelSample
14Coding Conventions
- Classes begin with capital letters (i.e.
UrRobot). Methods, objects, and variable names
begin with lower case (camelCase) - Use indentation to line up statements
- Brackets should be on a line by themselves
- Use comments // to identify what the code is
supposed to do (very useful)
15Symbols in Java
- A Definition of Symbol A symbol is a written
word or sequence of characters used to represent
something else. - Special symbols . (they mean something
special in Java) - Names are symbols karel, UrRobot, putBeeper.
UrRobot is not a robot itself, but represents a
type of robot. - Reserved words are symbols class, void, new
(they have a built-in purpose) - Note Symbols must match in case when used in
Java programs (i.e. karel is not the same as
Karel and putBeeper is not the same as PutBeeper)
16(Errors are referred to as bugs)
Error Classification
- lexical error (compiler catches)
- word not in its dictionary (i.e. you type MOVE()
instead of move() ) - syntax error (compiler catches)
- incorrect grammar, punctuation, incorrect
location of a statement (i.e. leaving out a
parenthesis or using a colon instead of
semi-colon) - execution error (run-time environment catches)
- cant perform what you ask (i.e. error shutoff if
performing move() when a wall is in the way) - intent error (logic - guess who catches this
one!) - program terminates successfully junk output,
however (i.e. moving robot to the wrong corner)
Which is the hardest type of error to correct?
Why?
17IDENTIFY THE BUGS (Clue There are at least 9
errors)
- public Class SampleTest implements Directions
-
- public static void main(String args
-
- UrRobot karel New UrRobot(2, 1, East, 0).
- karel.move()
- karel.turnLeft()
- getBeeper()
- karel.Move()
- karel.turnOff
- karel.move()
-
18IDENTIFY THE BUGS SOLUTION
- public class SampleTest implements Directions
- 1) should be class instead of Class
-
- public static void main(String args) 2) needs
parentheses -
- UrRobot karel new UrRobot(2, 1, East, 0).
- 3) should be new instead of
New - 4) use semi-colon instead of period
- karel.move()
- karel.turnLeft() 5) needs a semi-colon on
end - karel.pickBeeper() 6) needs an object and is
named pickBeeper - karel.move() 7) Move is not a method
- karel.turnOff() 8) needs () (i.e.
turnOff() ) - karel.move() 9) cant perform move
after turnOff -
public Class SampleTest implements Directions
public static void main(String
args UrRobot karel New UrRobot(2,
1, East, 0).
karel.move() karel.turnLeft()
getBeeper() karel.Move()
karel.turnOff karel.move()
19Now You Try Writing a Short Program to Draw the
Letter H
- Robot starts at origin (1,1), facing north
- Robot draws the letter H
- The bottom of the left bar of the H starts at
(2,2) - The H is 5 beepers high
- The middle bar should be 2 beepers between the
bars - Robot should return to origin, face north, and
turn itself off when finished (leaving the world
in its original state of being
Take a little time to think about the best
approach before jumping into the code. It may
help to sketch the robots path on a sheet of
paper.
20DrawH program Starting Point
- import kareltherobot.
- public class DrawH implements Directions
-
- public static void main(String args)
-
- UrRobot karel new UrRobot(1, 1, North,
infinity) - // .insert your code here to draw the
letter H -
- static
-
- World.setVisible(true)
- World.showSpeedControl(true)
-
Save DrawH.java from my Daily Lesson Plan page
(or Homework page) into your kareltherobot
package in your Karel project. Rename it to
include your full name (i.e. DrawH_JohnDoe)
21DrawH Desired Output
Beginning Situation
Ending Situation
22Starting DrawH together
- Lets go into eclipse and begin writing DrawH.
Well cover these items - Your workspace/project/package structure
- Creating (Copying) the DrawH class. Copy the
starting code from my website. - Renaming the DrawH class to include your name
(DrawH_JohnDoe). Click on the DrawH class within
Package Explorer and select File gt Rename. - Compiling (save) vs. Executing (run)
- Copying programs between school and home
23Copying Classes between School and Home
- Since there are times when you will want to
continue your work at home, you will need to be
able to transfer your code back and forth. This
is where your flash drive becomes handy. - To reduce confusion, keep the folder structure on
your flash drive the same as the folder structure
on H\workspace. - Use file manager to locate the Java source file
(that has extension .java) within the package
folder. - Copy only the .java files (not the .class files)
to your flash drive folder. - You can then copy these files to your package
folder on your destination computer. - Note After you launch eclipse, it is important
to refresh the project and package. Otherwise,
the new code may not be seen within eclipse - Single click on the project and select File gt
Refresh (or press F5). - Single click on the package and select File gt
Refresh (or press F5).