1 karel j robot oop approach to learning computer science “its study involves development of the...
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)
3
Karel J (the Robot)
• Robot World– A flat plane of streets (east-west) and avenues
(north-south)
Corner (many robots may occupy)
AvenuesStre
ets
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
5
Robot Capabilities
• Move
• Turn
• Sense surroundings– hear beepers (on same corner)– Determine direction it is facing
• Pick up, carry, and put down beepers
6
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
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.
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
9
Stepwise Refinement• Alternate implementation for Karel00
public void task(){
moveForwardTurnLeft(); moveForwardTurnLeft(); moveForwardTurnLeft(); moveForwardTurnLeft();
}
• Now we must define moveForwardTurnLeft();public void moveForwardTurnLeft(){
move(); move(); move(); move(); turnLeft();
}
10
Stepwise Refinement
• You are right, moveForwardTurnLeft is two separate tasks, so this method could/should be refined further.
11
Stepwise Refinement• Alternate implementation for: moveForwardTurnLeft public void moveForwardTurnLeft()
{
moveForward();
turnLeft();
}
public void moveForward(){
move();
move();
move();
move();
}
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.
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.
14
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.
15
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: }
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 methodturnRight()
– 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
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
18
LeftSpinningRobot
• The task method will have the Robot make three complete revolutions invoking only the turnLeft();
19
RightSpinningRobot
• The task method will have the Robot make one complete revolution invoking only the turnRight() method.
20
GuardRobot
• This Robot will march (move()) twice in a rectangular pattern around the SpnningRobots.
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
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?