170 likes | 284 Views
Karel – Primitive Instructions. Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc) move() turnLeft() putBeeper() pickBeeper(). move(). forward only, one block “Error Shutoff” if trying to move into a wall (a duh! Look first!)
E N D
Karel – Primitive Instructions • Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc) • move() • turnLeft() • putBeeper() • pickBeeper()
move() • forward only, one block • “Error Shutoff” if trying to move into a wall (a duh! Look first!) • Think divide by zero???
turnLeft() • stay at same corner • turn 90 degrees to the left • cannot cause an error shutoff
pickBeeper() • picks up beeper on current corner and places in beeper bag • attempted on beeper-less corners causes an error shutoff
putBeeper() • take beeper from beeper bag and put down on current corner • attempted with an empty beeper bag causes an error shutoff
Description public class Robot { void move() {…} void turnLeft() {…} void pickBeeper() {…} void putBeeper() {…} } • Robot class “primitive” You can’t/don’t need to define this class – it is in a library for you to use
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?
More on Error Classification • See additional PowerPoint presentation: • FOP_compilerErrors
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.
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 Examples: Symbol Names
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
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.
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
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.
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
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
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(); }