Karel J Robot OOP approach to learning computer
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) 1
2
Karel J (the Robot) • Robot World – A flat plane of streets (east-west) and avenues (north-south) Streets Avenues Corner (many robots may occupy) 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 (3 rd St. & 5 th Ave. ) – Run a race – Escape from a maze – Find a beeper and deliver it to the origin 6
Previous task (Karel 00) • Karel ran a lap around the Block – How many lines of code did you write? • 20 (16 move() and 4 turn. Left() ) • Was there a pattern? 4 x (4 moves, turn. Left) – 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 Karel 00 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 Karel 00 public void task() { move. Forward. Turn. Left(); } • Now we must define move. Forward. Turn. Left(); public void move. Forward. Turn. Left() { move(); turn. Left(); } 9
Stepwise Refinement • You are right, move. Forward. Turn. Left is two separate task, so this method could/should be refined further. 10
Stepwise Refinement • Alternate implementation for: move. Forward. Turn. Left public void move. Forward. Turn. Left() { move. Forward(); turn. Left(); } public void move. Forward() { move(); } 11
We’re a little wiser • Lets apply our new design methodology to the next task: • Consider the turn. Right() command • Our Robot doesn’t understand turn. Right(), it is not capable of turning right • But turning right make sense. 12
turn. Right() • Karel doesn’t know how to turn right, so how do we get the robot to turn clockwise 90 degrees? • turn. Left 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 turn. Right and invoke this method. 13
Implement turn. Right() • In your class you would need to include the following method public void turn. Right() { turn. Left(); } • 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 turn. Right() below public void turn. Right() { turn. Left(); } – Note the different parts of the method – Heading: public void turn. Right() – Beginning: { – Body: turn. Left(); – End: } 15
Adding methods – Heading: public void turn. Right() – Three parts • For now, the first two words are always the same: public void • The third word is the name of the method turn. Right() – Beginning: { • Lets the compiler know that here comes the implementation – Body: turn. Left(); • 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 Karel 01 folder to your work area. • The Main. Driver constructs: – Two Left. Spinning. Robots – Two Right. Spinning. Robots – Two Guard. Robot • You will implement the task() for all three Robot classes continue 17
Left. Spinning. Robot • The task method will have the Robot make three complete revolutions invoking only the turn. Left(); 18
Right. Spinning. Robot • The task method will have the Robot make one complete revolution invoking only the turn. Right() method. 19
Guard. Robot • This Robot will march (move()) twice in a rectangular pattern around the Spnning. Robots. 20
How to get Started • From the K drive – Copy the Karel 01 Folder to your workspace. – Open Blue. J • Open Project and select Karel 01 – Implement the task method for: • Left. Spinning. Robot • Right. Spinning. Robot • Guard. Robot 21
Special Notes • Why is the Right. Spinning 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 Class. Name() { serval. Methods() { } } 23
- Slides: 23