Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)

Similar presentations


Presentation on theme: "Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)"— Presentation transcript:

1 Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc) –move() –turnLeft() –putBeeper() –pickBeeper()

2 Ch. 2 2 move() forward only, one block “Error Shutoff” if trying to move into a wall (a duh! Look first!) –Think divide by zero???

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

4 Ch. 2 4 pickBeeper() picks up beeper on current corner and places in beeper bag attempted on beeper-less corners causes an error shutoff

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

6 Ch. 2 6 Description Robot class public class Robot { void move() {…} void turnLeft() {…} void pickBeeper() {…} void putBeeper() {…} } “primitive” You can’t/don’t need to define this class – it is in a library for you to use

7 Ch. 2 7 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?

8 Ch. 2 8 More on Error Classification See additional PowerPoint presentation: –FOP_compilerErrors

9 Ch. 2 9 Valid Symbol Names Must not be a reserve word (e.g. main, private,..) Must begin with a letter –Not totally true, but we’ll go with this definition to make our lives easier! The rest of the name can be either –An (upper case) letter: A, B, C, … Y or Z –A (lower case) letter: a, b, c, …, y or z –A number 0, 1, 2, 3, …, 9 –An underscore: _ NO SPACES ALLOWED NO other characters: +, *, -, /, “, #, %, etc.

10 Ch. 2 10 Examples: Symbol Names Valid iLove_FOP value1 w_a_19_05 HELP h_e_l_p Robot karel move NOT valid know why! 1win main void you win I-love-FOP HELP! win+win TroyHighSchool#1

11 Ch. 2 11 Symbol Names Conventions Identifiers, Method name –Begins with a lower case letter. Each new word in the name is capitalized. For example: turnleft becomes turnLeft() Class name/Type –Begins with a Upper case (Capital) letter. Each new word in the name is capitalized. For example: left spinning robot becomes LeftSpinningRobot CONSTANTS –All capital letters For example: pi becomes PI

12 Ch. 2 12 Now You Try a Short Program on Paper – 10 minutes Assume a robot is at position (1, 1) (a.k.a, the “origin”) facing North and having plenty of beepers Have the robot “print” (using beepers) the number E (5-beepers tall and a 3 beepers wide) when done, the robot should be back at the origin, facing North.

13 Ch. 2 13 Your Task You will copy the Karel02 Folder to your work area There are two MainDriver files in this project. We will focus on FirstMainDriver first. In this class, there are four different Robot classes. –HurdlerRobot –WallClimberRobot –UpAndUnderRobot –AnchorLegRobot You will implement the task() method for all four Robot classes

14 Ch. 2 14 MainDriver When you execute this class, you should see each Robot practice its leg of the Obstacle Course in MainDriver1. Once each Robot has successfully completed its leg, the entire Obstacle course can be tested by executing the MainDriver2.

15 Ch. 2 15 Stepwise Refinement Factoring out common code –If you find yourself writing the same lines of code, you should make a new method Give new methods appropriate names! –Method names should describe what task the method completing

16 Ch. 2 16 Factoring out common code Example: public void sampleMethod() { turnRight(); move(); turnRight(); // other lines of code turnRight(); move(); turnRight(); } Replace common lines with a method call

17 Ch. 2 17 Factoring out common code Example: public void sampleMethod() { rightU_Turn(); // other lines of code rightU_Turn(); } // helper method public void rightU_Turn() { turnRight(); move(); turnRight(); }


Download ppt "Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)"

Similar presentations


Ads by Google