CS 149: Introduction to Programming
James Madison University, Spring 2018 Semester

Lab12: Practice with nested loops

Background

In the good old days, we didn't have fancy computer graphics. So we had to rely on ASCII art and the user's imagination. Today's lab will take you back in time! Your task is to draw various pictures of stars ('*') that require the creative use of iteration and decisions.

Collaboration: You are encouraged to work with another student to complete this lab. Each of you should submit your own copy of the programs. It's okay if your files are similar or identical, as long as both of your names are present at the top.

Objectives

Instructions

  1. Download Stars.java and open your favorite editor.

  2. Write your name and today's date in the class comment.

  3. Compile and run the program. What shape does this pattern produce?

  4. Notice the relationship between the row (outer loop) and the number of stars that are printed.

  5. Notice the relationship between the col (inner loop) and the place where we move to a new line.

  6. Add code to your program to carry out Patterns A, B, and C as shown below.

Pattern A

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

The leftmost stars are in the leftmost output column. Test your code with an odd number of stars and an even number of stars.

HINT: While developing the code, replace each space with another character that is visible.

Pattern B

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

The leftmost star of the last row is in the first position of the output column.

HINT: You used starCnt in the original example and Pattern A. You should think about using the blnkCnt variable as well.

Pattern C

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

The top row, leftmost star is in the first position of the output.

HINT: It should be the same as Pattern B, except for the way you calculate blnkCnt and starCnt.

Generalization

Create a new void method called print that encapsulates the code that Patterns A, B, and C have in common. Add parameters that generalize the method so that it works for Patterns A, B, C, and potentially other patterns.

Be sure to write a meaningful documentation comment including @param tags. Replace any duplicated code in main with method calls to your print method. Submit your Stars.java via Canvas by the end of the day.