Ice15-Part 1 Part 2: Nested Loops and Non-Robotic Programs

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 15.2.1: Nested Loops: Trace

Download the NestedWhile.java file.

As much as we all know how much you all love doing traces, I'm going to ask you to trace this one particular program using the Tracer_Starter trace. It will help you in wrapping your head around what the nested loops are doing.

As you trace, be attentive that you're tracing through the nested loops properly.

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

ICE ICE 15.2.2: Loops: Practice Without Robots

Download the file ICE_15_For_4.java.

You'll notice that it's pretty much a bare-bones Java file. Your job is to create code in main that will print out a row of asterisks, like so:

*****************

The tricky part is that I want you to be able to modify only one line of code, and by modifying that single line, have the code print out as many asterisks as you want, whether 5 or 15 or 50.

NoteREMEMBER: if you want to print some text, without moving to the next line, use System.out.print("*");
// Note that it's print, not println

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

ICEICE 15.2.3: Printing a Line of 'Stars' (Asterisks)

Download the file named PrintingShapes.java from the website.

Using this as a basis, write some code that will first ask the user how many stars to print out, and then print out that many asterisks, using loops.

An example transcript of the program might look like so (user input in red)


Hello! How many stars would you like me to print?

9

Okay, here are your stars:

*********


NoteNOTE: If you want to print some text, and move to the next line (like you normally want to), then you might add System.out.println("");
// print nothing, but move to the next line!

NoteNOTE: If you want to print some text, without moving to the next line, you use System.out.print("*");
// Note that it's print, not println

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

ICEICE 15.2.4: Printing a Rectangle of 'Stars' (Asterisks)

Download a new copy of the file named PrintingShapes.java from the website and rename it to PrintingShapesRec.java (and be sure to rename the class in main to match the file name).

For this part, add a new method PrintRectangleNested() that will look something like this:

public void PrintRectangleNested(int width, int height)

Invoking this method in main willl print out a rectangle of asterisks, using two for loops (and yes, they have to be nested).

The output might look like this:

*****
*****
*****

Modify the code so your code so that you declare two local variables, like so:

int width = 5;

int height = 3;

Obviously, if you change the variables to, say:

int width = 3;

int height = 5;

Your code should print out:

***
***
***
***
***

NoteHINT: The two for loops will use one System.out.print("*"); and one System.out.println(""); within the loops, and an additional System.out.println(""); at the end of the method outside the loops.

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

ICEICE 15.2.5: Printing a Rectangle with a Method (Sub-Command)

For this part, you should create a brand-new, non Robotic class, named PrintHelper.

This class will serve the purpose of holding a bunch of commands (methods) that print things out.

The first method you'll make will be named PrintRectangle() and should look something like this:

public void PrintRectangle(int width, int height)

When called from PrintingShape's main method, you'll first have to create an instance of the PrintHelper class, then tell it to print a rectangle.

So, in order to print a 3 (wide) by 5 (tall) rectangle, your code (in main) might look like:

public static void main(String[] args)
{
    PrintHelper helper = new PrintHelper();

    helper.PrintRectangle(3, 5);

    // More code might go here...

}

Your job is to write the code for PrintRectangle().

NoteNote: I'd highly recommend cannibalizing the code that you wrote in the previous part—the new PrintRectangle method should be (almost) a straight copy-and-paste from what you've already written.