230 likes | 244 Views
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.
E N D
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)
Streets Corner (many robots may occupy) Avenues Karel J (the Robot) • Robot World • A flat plane of streets (east-west) and avenues (north-south)
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
Robot Capabilities • Move • Turn • Sense surroundings • hear beepers (on same corner) • Determine direction it is facing • Pick up, carry, and put down beepers
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
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.
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
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(); }
Stepwise Refinement • You are right, moveForwardTurnLeft is two separate task, so this method could/should be refined further.
Stepwise Refinement • Alternate implementation for: moveForwardTurnLeft public void moveForwardTurnLeft() { moveForward(); turnLeft(); } public void moveForward() { move(); move(); move(); move(); }
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.
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.
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.
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: }
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
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
LeftSpinningRobot • The task method will have the Robot make three complete revolutions invoking only the turnLeft();
RightSpinningRobot • The task method will have the Robot make one complete revolution invoking only the turnRight() method.
GuardRobot • This Robot will march (move()) twice in a rectangular pattern around the SpnningRobots.
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
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?
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() { } }