Karel J Robot - Chapter 5 Programs: Repeating Instructions BACK TO KAREL MAIN
Instructions that Repeat
We will now learn how to make Karel perform repetitive tasks. In other words, we will learn how to make a command that can cause Karel to perfrom an action over and over. You will see that this saves many lines of code. In computer science, the act of repeating a block of code is called a loop. There are two types loops we will use: the for loop and the while loop. See the examples below. |
The FOR loop – a loop used when you want a robot to perform the same task a specific number of times. Example 1: a for loop used to make exactly 5 moves: public void move() Example 2: a for loop used to make n moves: public void move(int n) |
The WHILE loop – a loop used when you want a robot to perform a task repeatedly until a condition becomes false (an unknown number of times). Example 1: a while loop used to make a robot move to a wall an unknown distance away: public void moveToWall() Example 2: a while loop used to pick up an unknown number of beepers at its current location: public void pickAllBeepers() |
Program 5-1: The Steeplechaser2 Class (focus on FOR loops) Karel must run a steeplechase race, jumping over steeples and picking up beepers as he goes. There will be exactly 8 steeples and beepers, so the race is 8 blocks long (the race always ends at 1st Street and 9th Avenue. See the example initial and final situations below.
World File: steeple2.kwld BACK TO KAREL MAIN |
Program 5-2:
The DiagonalMaker Class (focus on FOR loops)
World File: blank.kwld BACK TO KAREL MAIN |
Program 5-3: The SquareMaker Class (focus on FOR loops) Karel has been hired to build square rooms out of beepers. He is given instructions by you, the programmer, to go to a given location and build a room of a given size. In the example below, he was told to go to (2, 3) and build a room of size 4. It is to be assumed that Karel begins each task at the Origin, facing East, with an infinite number of beepers in his bag.
World File: blank.kwld BACK TO KAREL MAIN |
Program 5-4: The Fetcher Class (focus on WHILE loops) It’s Karel the robot dog! He can fetch a ball (beeper, of course) but he can’t run directly to it. He has to stick to East-West, North-South movement. He begins his little game at the origin, facing East. To help him out, we place a wall at the point where he needs to turn. He then turns and runs to the place where the ball is. Finally, he must return to the origin and drop the ball off to his master. Below is a sample situation. Your task should be able to handle any such situation.
World File: fetch.kwld BACK TO KAREL MAIN |
Program 5-5: The Tiler Class (focus on WHILE loops) Now our robot hero will be charged with laying ceramic tile in some rooms in a building. He will begin at the opening to a hallway, then go to each room and tile each available square in the room with a beeper. When he reaches the end of the hallway, he is to return to the entrance to the hallway where he started out. The robot should begin the task with an infinite number of beepers. Below is a sample building. Your task should be able to tile any such building.
World File: tiler.kwld BACK TO KAREL MAIN |
Program 5-6: The TrashCollector Class (focus on WHILE loops)
World File: trash.kwld BACK TO KAREL MAIN |
Program 5-7: The SuperSteepleChaser Class (focus on WHILE loops) - AP ONLY
World File: steeple3.kwld BACK TO KAREL MAIN |
Program 5-8: The Trailer Class (focus on WHILE loops) - AP ONLY
World File: trail.kwld BACK TO KAREL MAIN |
Program 5-9: The SuperDuperSteepleChaser Class (focus on WHILE loops) - AP ONLY
World File: fig6-22.kwld BACK TO KAREL MAIN |
Program 5-10: The Treasurehunter Class (focus on WHILE loops) - AP ONLY
World File: fig6-27.kwld BACK TO KAREL MAIN |