Download presentation
Presentation is loading. Please wait.
Published byEstella Caldwell Modified over 9 years ago
2
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)
3
2
4
3 Karel J (the Robot) Robot World –A flat plane of streets (east-west) and avenues (north-south) Corner (many robots may occupy) Avenues Streets
5
4 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
6
5 Robot Capabilities Move Turn Sense surroundings –hear beepers (on same corner) –Determine direction it is facing Pick up, carry, and put down beepers
7
6 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
8
7 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.
9
8 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
10
9 Stepwise Refinement Alternate implementation for Karel00 public void task() { moveForwardTurnLeft(); } Now we must define moveForwardTurnLeft(); public void moveForwardTurnLeft() { move(); turnLeft(); }
11
10 Stepwise Refinement You are right, moveForwardTurnLeft is two separate tasks, so this method could/should be refined further.
12
11 Stepwise Refinement Alternate implementation for: moveForwardTurnLeft public void moveForwardTurnLeft() { moveForward(); turnLeft(); } public void moveForward() { move(); }
13
12 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.
14
13 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.
15
14 Implement turnRight() In your class you would need to include the following method public void turnRight() { 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.
16
15 Adding methods Consider the implementation of turnRight() below public void turnRight() { turnLeft(); } –Note the different parts of the method –Heading : public void turnRight() –Beginning : { –Body : turnLeft(); turnLeft(); turnLeft(); –End : }
17
16 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
18
17 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
19
18 LeftSpinningRobot The task method will have the Robot make three complete revolutions invoking only the turnLeft();
20
19 RightSpinningRobot The task method will have the Robot make one complete revolution invoking only the turnRight() method.
21
20 GuardRobot This Robot will march (move()) twice in a rectangular pattern around the SpnningRobots.
22
21 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
23
22 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?
24
23 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() { }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.