Nested Loops & Non-robotic Programs

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

           

            Download the NestedWhile.java file.  Trace the entire program, making sure that you're tracing through the nested loops properly.  When you finish tracing the first nested loop, raise your hand so that the instructor can check it.

 

Part 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:

 

            *

 

or

 

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

 

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

            Remember that 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

 

Part 3: Printing A Line Of Stars

 

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 bold)

            If you want to print some text, and move to the next line (like you normally want to), then you use

System.out.println(""); // print nothing, but move to the next line!

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

 

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

5

Ok, here are your stars:

*****

 

Part 4: Printing A Rectangle

 

Download a new copy of the file named PrintingShapes.java from the website.  Using this as a basis, write some code that will print out a rectangle of asterisks, using loops (yes, they have to be nested).  The output might look like so:

 

*****

*****

*****

 

            Feel free to put all of your code into public static void main, to start.  Next, modify the code that your code so that you declare two local variables, like so:

int width = 5;

int height = 3;

            Modify your code so that your code will use the above variables to determine how large to make the rectangle.  Obviously, if you change the variables to, say:

int width = 3;

int height = 5;

Your code should print out:

 

***

***

***

***

***

 

Part 5: Printing A Rectangle, With A Subcommand

 

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 public static void 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.  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.

 

Part 6: Printing A Hollow Rectangle, Via A Subcommand

 

For this part, you should add a method to your PrintHelper class, named PrintRectangleHollow.  This should have the exact same parameters as PrintRectangle, and should function similarly, except that any 'interior' spaces should be filled with blank spaces, not asterisks.  Thus, the following code snippets should generate the following output:

 

// This:

helper.PrintRectangleHollow(3, 5);

// Outputs this:

//    ***

//    * *

//    * *

//    * *

//    ***

 

// This:

helper.PrintRectangleHollow(4, 5);

// Outputs this:

//    ****

//    *  *

//    *  *

//    *  *

//    ****

 

// This:

helper.PrintRectangleHollow(2, 2);

// Outputs this:

//    **

//    **

// This:

helper.PrintRectangleHollow(5, 1);

// Outputs this:

//    *****

 

Part 7: Printing A Left Triangle, Via A Subcommand

 

For this part, you should add a method to your PrintHelper class, named PrintLeftTriangle.  This should have only a single parameter, height, which is an integer.  This method will print out a 'triangle', with a straight vertical edge long the left side of the screen

 

// This:

helper.PrintLeftTriangle(5);

// Outputs

// *****

// ****

// ***

// **

// *

 

// This:

helper.PrintLeftTriangle(3);

// Outputs

// ***

// **

// *

 

// This:

helper.PrintLeftTriangle(2);

// Outputs

// **

// *