Repeating Commands A Specific Number Of Times: Counting 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

           

One of the most useful combinations of stuff in Java is a loop and variable combo.  With these two separate parts, we can then have our program keep count of the number of times it's done something.  The way it works is this: the variable holds the number of times that something has been done.  Each time through the loop, we do some piece of the work, then remember that the work has been done by increasing the number (stored in the variable) by one.

 

For this exercise, download the file the ICE_05_CL_Trace.java file and trace the code.

 

Remember to use a column to keep track of all variables (including the variables we’re using to track how many times the loop has been run)!

 

Part   2: Finding Errors: Counting using while loops

 

            You should make sure that you understand this concept by downloading the file ICE_05_Count.java. Start by fixing all the compile-time errors (and any logical errors that you find), and then go on fixing logical/runtime errors that you find.

 

Part   3: Write Code : Eliminate Repetitive Code

            Go back to the code that you wrote in part 4 of the exercises for the "If statements", (the exercise using If…else, canPickThing, and frontIsClear).  Examine main, and replace the repetitive 6 blocks (that each call Jo.move(); 4 times, then call Jo.chooseDirection();) with a single (counting) while loop.

Part   4: Write Code : Move The Robot Down A Hallway

             In a previous part 5 (another exercise using if...else), you created a program that helped the robot figure out which hallway to go down.  You should re-visit that problem.  However, you should now try to solve it by putting a while loops inside one (or more) of the if statements, so that the robot can 'run down' the hallway, rather than being told to move exactly 4 intersections.

 

            You can start with the file behind the "Hallways w/ While" link on the website.

 

 

Part   6:  Writing Code: Variations on the 'Counting' Loop Pattern (Optional)

 

            You should be able to use the 'counting' loop pattern to solve one of the following problem (if you get done with one, go on to the next)

 

Part.6.1: Counting up

 

            Copy the ICE_05_Pattern_1.java file, and modify it so that it starts by setting it's counter to 1.  Your robot should drop 4 things in the city, just like it does now.  This means you'll have to figure which other  part of the program you'll need to change, in order to get this correct result.
Outcomes: 1C, 2B, 2C, etc

 

Part 6.2: Counting down

 

            Copy the ICE_05_Pattern_1.java file - do this again, a second time, so that you have a copy of the work you did in part 3.1, AND the work you do here.  Modify this version so that it starts by setting it's counter at some number (which you’ll need to figure out), and counts down until the counter is less than or equal to zero.  Your robot should drop 5 things in the city.

Outcomes: 1C, 2B, 2C, etc

 

Part 6.3: Counting from one number to another number

 

            Copy the ICE_05_Pattern_1.java file, and modify it so that it starts by setting it's counter to the current avenue number (using the getAvenue() service).  It should then count up, moving once each time, until it reaches avenue 12. 

Outcomes: 1C, 2B, 2C, etc

 

Part 6.4: Compare and Contrast the different counting loops

 

Go back through the code that you (and other people) have written, and look for a common pattern.  What's the same in each of your programs?  What's different?  Write a short paragraph, or bulleted list, or some other notes for yourself on what the most important aspects of this loop pattern are.

Outcomes: 1A, 1B, maybe 1D, 2D, 4A, 4B, 4C, etc