Karel Primitive Instructions Basic tools with which all

Karel – Primitive Instructions • Basic tools with which all problems are solved (analogies: carpentry, geometry) – move() – turn. Left() – put. Beeper() – pick. Beeper() – turn. Off() 1 Ch. 2

OOP -ness • object. method 1(); • object. method 2(); -where a method acts on the object -objects are typically nouns (e. g. , karel) -methods are typically verbs (e. g. , move) and represent behavior karel. move() 2 Ch. 2

move() • forward only, one block • “Error Shutoff” if trying to move into a wall (a duh! Look first!) Remember C++ and div by zero? ? ? 3 Ch. 2

turn. Left() • stay at same corner • turn 90 degrees to the left • cannot cause an error shutoff 4 Ch. 2

pick. Beeper() • picks up beeper on current corner and places in beeper bag • attempted on beeper-less corners causes an error shutoff 5 Ch. 2

put. Beeper() • take beeper from beeper bag and put down on current corner • attempted with an empty beeper bag causes an error shutoff 6 Ch. 2

turn. Off() • robot turns off and is incapable of executing another instruction until restarted • the last instruction executed on a robot object 7 Ch. 2

Description class Ur. Robot • public Ur. Robot class (the “Model-T” Robot) { void move() {…} “primitive” void turn. Off() {…} void turn. Left() {…} void pick. Beeper() {…} void put. Beeper() {…} You can’t/don’t need to define this class – it is in a library for you to use } 8 Ch. 2

Sample Program import kareltherobot. *; public class Sample. Test implements Directions { public static void main(String args[]) { ur_Robot karel = new Ur. Robot(2, 1, East, 0); karel. move(); karel. turn. Left(); karel. turn. Off(); } static { … // code here that you need not worry much about } } 9 Ch. 2

Error Classification • 4 -types • lexical error (compiler catches) – word not in its dictionary • syntax error (compiler catches) – incorrect grammar, punctuation, incorrect location of a statement • execution error (run-time environment catches) – can’t perform what you ask (at run-time) • intent error (logic - guess who catches this one!) – program terminates successfully – junk output, however Which is the hardest type of error to correct? Why? 10 Ch. 2

Now You Try a Short Program on Paper – 10 minutes • start a robot (HBot) at position (1, 1) (a. k. a, the “origin”) facing North and having an infinite number of beepers • Have the robot “print” (using beepers) the letter H (with 3 -beeper sides and a 1 -beeper bar) • when done, the robot should be facing North back at the origin (leaving the world in its original state of being) 11 Ch. 2

Programming the HBot together • Let’s go into Blue. J and write the HBot. We’ll cover these items: – Your network drive and staying organized – Creating a workspace – Using/reading the API – Compile vs. Execute – www. apcomputerscience. com and setting up things at home 12 Ch. 2
- Slides: 12