Repeating Commands: While loops

Note:  Please keep the programs that you create today, in case you have a dispute about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save their own, individual copy

Part   1: Code Tracing

            To start, download the file the ICE_05_Trace.java file and trace the code.

Tracing loops is a little different than tracing the step-by-step (sequential) code that we have seen so far, but very, very similar to tracing the "if" or "if/else" code that you've seen previously. To trace the code given in the following code snippet from the class demo program, use the Program Trace Table with an additional column for the true/false condition, as demonstrated in class.

(For homework assignments, you can (and should!) use the same true/false column for both if statements, and while loops)

 Part   2: Finding & Fixing Errors

You should download the code from the website for ICE_05_Avenues.java, and find all the compile-time errors that prevent the program from running.  Once you've done that, you should find and fix all intent errors.

Part   3: Write Code : Relay Race (Extra Challenging)

             You should download the code from the website for ICE_05_Race.java.  The code that you're given will set up a relay race, much that the one pictured in Figure 1.1, below. 

The interesting part of this exercise is that each of the three robots start in a slightly different location than what's pictured above.  Each robot will be located on the street (east-west road) pictured above, but may start in a different avenue.   However, karel (the left most robot) will always start left of the other two robots.  bob will always start between karel and mary, and mary will always start to the right of the other two.  Thus, another valid way for the race to start is pictured in Figure 1.2, below.

Code has been provided in ICE_05_Race.java that will take care of placing all three robots for you.  You may wish to examine the getAvenue() service which all Robots provide – it will tell you which avenue the robot is in.  You'll probably solve this problem by combining the getAvenue() service with a loop, so that karel will continue moving east until it's reached the same avenue that bob is in, etc.  A flowchart explaining one possible way to solve the problem is given in Figure 1.3.

 

Figure 1.1                                                     Figure 1.2

 

Figure 1.3


Part   4: Write Code : Move The Robot To A Specific Intersection (Extra)

             You should continue working with the code you were using in the previous part.   

You should write code so that you move the Mary robot to avenue 18, and street 5.  However, you MUST use a while loop to do this – you're not allowed to simply put in a bunch of "move" commands.  You will have to write the new loop on your own, from scratch, but it will obviously bear some similarity to the loop that moves the robot to the 3rd  avenue.

Part   5: Write Code : Move The Robot To A Specific Avenue

            You should download the code from the website for ICE_05_Avenues.java.  The file contains some simple starter code to get you up and running.  You should write code so that you move the robot to avenue 8.  However, you MUST use a while loop to do this – you're not allowed to simply put in 8 "move" commands.  Using the while loop is substantially less typing, and once you get used to it, it'll be substantially easier, too.