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 15.1: The for Loop
Download the ICE_15_For_1.java file, and change all of the counting while loops to be for loops, instead.
Make sure that the program still compiles, and continues to do the same thing it did before your modifications.
Here is a quick table to remind you how the two loops (while loops and for loops) are equivalent :-)
int i = 0;
i++; // same as i = i + 1;
for(int i = 0;
i < 4 ;
When you get done with this exercise, go on to the next section.
ICE 15.2: The for Loop and Robots
Locate a Java file that contains a Robot within it. This can be something you download from the website right now, or any file that you’ve previously downloaded or created or used.
Have the Robot start out carrying 10 Things in its backpack.
Use a for loop to have the Robot move forward 6 spaces, putting down a Thing each time it moves.
At the end, have the console show two (2) messages:
- How many Things are remaining in the Robot's backpack
- How many Things the Robot has put down
Once you’ve gotten that to compile and run, using a commented section compare and contrast the while loop with the for loop. In particular, explain what sort of situation(s) a for loop is really good for, and what sort of situation(s) a while loop is really good for.
The Alternative Universe of Excellence! OPTIONAL
Instead of using comments for your while loop versus for loop explanation, print it out to the console using several lines of System.out.println services.
Hint: Need help figuring out the differences between the while and for loops? Google it! For example, Google "java for loop vs. while loop" and start poking through some of the offered pages.
Hint: When someone [usually a teacher :-) ] asks you to ‘compare and contrast’ something, you basically end up examining: how two things are similar (this is the comparison) and how two things are different (this is the contrast).
When you get done with this exercise, show your Instructor.