Ice01 Repeating Commands: While Loops

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

 

ICE ICE 5.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.

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 (this will track whether the conditions of the while loop are determined to be true or false)

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

When you get done with this exercise, go on to the next section. Go Next


ICE ICE 5.2: Finding and 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 (logical) errors.

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.

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next


ICE Part 3: Writing 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 like the one pictured below.

The interesting part of this exercise is that two of the three robots (bob and mary) will on a slightly different avenue than what's pictured below (by utilizing Java's Random() class in the code). Their placement on the street will stay the same (only their placement on the avenues will be randomized).

SO: Each robot will be located on the Street (east-west west-east) pictured below, but bob and mary may start in a different Avenue (north-south North-South), however:

Thus, another valid way for the race to start is pictured in the second image below.

Code has been provided in ICE_05_Race.java that will take care of placement of the three robots automatically for you (no need to worry about that)

NOTENOTE: You may wish to examine the getAvenue() service which all Robots provide – it will tell you which avenue the robot is in.

NOTENOTE: 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 the third image below.

Race

Race 2

Flowchart

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next


ICE Part 4: Move the Robot to a Specific Intersection (TO DO If Time Remaining)

This exercise an be used for at-home practice-practice-practice if unable to work on in class Smile

You should continue working with the ICE_05_Race.java 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 (the last robot with the "baton", see Figure 1.4 below). 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.

NOTENOTE: it will obviously bear some similarity to the while loops that move the robots forward in Part 3

NOTEHINT: it would work well if the while loop contained an if statement that would move the robot to Street 5 once it reached Avenue 18

Optional

REFERENCE INFO

To build out the City and the randomized street behavior of bob and mary I used two new concepts that we will discuss in later lectures: Random and the nextInt() method.

If interested, you can read about why/how this was done here: http://www.tutorialspoint.com/java/util/random_nextint_inc_exc.htm