1 / 23

Karel J Robot

Explore the world of Karel J. Robot and learn computer science through a hands-on object-oriented programming approach. Task-oriented situations allow for effective abstraction and problem-solving skills development. Implement stepwise refinement to optimize code efficiency.

mwallace
Download Presentation

Karel J Robot

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 J Robot • OOP approach to learning computer science • “Its study involves development of the ability to abstract the essential features of a problem and its solution, to reason effectively in the abstract plane, without confusion by a mass of highly relevant detail.” (C. Hoare)

  2. Streets Corner (many robots may occupy) Avenues Karel J (the Robot) • Robot World • A flat plane of streets (east-west) and avenues (north-south)

  3. Karel’s World (cont’d) • Contains Beepers and Walls • Beepers • May be picked up, carried, and placed again • May place several on a corner and they don’t interfere with Robot movement

  4. Robot Capabilities • Move • Turn • Sense surroundings • hear beepers (on same corner) • Determine direction it is facing • Pick up, carry, and put down beepers

  5. Tasks & Situations • Examples • Move to a corner (3rd St. & 5th Ave.) • Run a race • Escape from a maze • Find a beeper and deliver it to the origin

  6. Previous task (Karel00) • Karel ran a lap around the Block • How many lines of code did you write? • 20 (16 move() and 4 turnLeft() ) • Was there a pattern? 4 x (4 moves, turnLeft) • Could the code have been written in fewer lines? • The answer is of course, the process is stepwise refinement.

  7. Stepwise Refinement • A different design/solution for Karel00 would have been to notice that the Robot perform the same task four times. • That is, the Robot was to move four times and turn left. After performing this maneuver four times, the task would have been complete

  8. Stepwise Refinement • Alternate implementation for Karel00 public void task() { moveForwardTurnLeft(); moveForwardTurnLeft(); moveForwardTurnLeft(); moveForwardTurnLeft(); } • Now we must definemoveForwardTurnLeft(); public void moveForwardTurnLeft() { move(); move(); move(); move(); turnLeft(); }

  9. Stepwise Refinement • You are right, moveForwardTurnLeft is two separate task, so this method could/should be refined further.

  10. Stepwise Refinement • Alternate implementation for: moveForwardTurnLeft public void moveForwardTurnLeft() { moveForward(); turnLeft(); } public void moveForward() { move(); move(); move(); move(); }

  11. We’re a little wiser • Lets apply our new design methodology to the next task: • Consider the turnRight() command • Our Robot doesn’t understand turnRight(), it is not capable of turning right • But turning right make sense.

  12. turnRight() • Karel doesn’t know how to turn right, so how do we get the robot to turn clockwise 90 degrees? • turnLeft three times? • Do you want to write three lines of code every time you want to turn right, • Or do we write one method called turnRight and invoke this method.

  13. Implement turnRight() • In your class you would need to include the following method public void turnRight() { turnLeft(); turnLeft(); turnLeft(); } • Wouldn’t it be nice if I did not have to write this code every time we create a new Robot class. • The answer is of course there is, but that is a topic for another day.

  14. Adding methods • Consider the implementation of turnRight() below public void turnRight() { turnLeft(); turnLeft(); turnLeft(); } • Note the different parts of the method • Heading: public void turnRight() • Beginning: { • Body: turnLeft(); turnLeft(); turnLeft(); • End: }

  15. Adding methods • Heading: public void turnRight() • Three parts • For now, the first two words are always the same: public void • The third word is the name of the method turnRight() • Beginning: { • Lets the compiler know that here comes the implementation • Body: turnLeft(); turnLeft(); turnLeft(); • The actual commands that in fact replace the method call • Every time the Robot (or any Object) sees the new command, it will automatically perform the Body of the new method • End: } • Lets the compiler know the implementation is complete

  16. Your Task • You will copy the Karel01 folder to your work area. • The MainDriver constructs: • Two LeftSpinningRobots • Two RightSpinningRobots • Two GuardRobot • You will implement the task() for all three Robot classes continue

  17. LeftSpinningRobot • The task method will have the Robot make three complete revolutions invoking only the turnLeft();

  18. RightSpinningRobot • The task method will have the Robot make one complete revolution invoking only the turnRight() method.

  19. GuardRobot • This Robot will march (move()) twice in a rectangular pattern around the SpnningRobots.

  20. How to get Started • From the K drive • Copy the Karel01 Folder to your workspace. • Open BlueJ • Open Project and select Karel01 • Implement the task method for: • LeftSpinningRobot • RightSpinningRobot • GuardRobot

  21. Special Notes • Why is the RightSpinning Robot turning Left and making more that one revolution. • Notice that not all the Robots move at the same time. They move one at a time. That is, they take turns. • Do you know how to determine the order that the Robots move?

  22. Special Notes • What do the last two lines of you file look like? • Both ending lines should consist of single } • The general class outline should be as follows: public class ClassName() { servalMethods() { } }

More Related