1 / 17

Karel – Primitive Instructions

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

Download Presentation

Karel – Primitive Instructions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

  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. More on Error Classification • See additional PowerPoint presentation: • FOP_compilerErrors

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

  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. 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. 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. 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. 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. 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. 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(); }

More Related