CS 139 Algorithm Development
Lab09B: Stars

Objectives

Students use a for loop to solve a counted loop problem.

Background

This program will display various patterns of stars ('*') that will require the creative use of iteration and decisions.

New Terms

loop
A program structure that supports iteration
loop control variable
A variable used to control the continuation of a loop
sentinel value
A special value that signals to the program that the loop should not continue
off by one error
A kind of loop error in which the loop executes one too many or one too few times
infinite loop
A kind of loop error in which the loop never finishes executing

Materials

Download Stars.java.
Submission
  • Complete the CodeBat exercises indicated below at http://codingbat.com. Be sure to include your instructor e-mail in your profile.

  • Submit your completed Stars.java to Blackboard or demonstrate for the lab instructor.

Acknowledgment

Adapted from an original lab by Arch Harris & Nancy Harris



Part 1 - Set Up
  1. Create a new folder named Stars for this lab, on your Desktop or removable drive.

  2. Download Stars.java to the folder.

Part 2 - Warmup

  1. In the CodingBat Java / String-2 group, do repeatFront - http://codingbat.com/prob/p128796. You should do this part individually, but may receive help from a partner. Hint, a decrementing loop may make this problem easier.


Part 3 - Understand the problem
  1. Edit and compile Stars.java. Run the program. What shape does this pattern produce?

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

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

  4. When you think you are ready to implement different patterns move to Part 4.


Part 4 - Pattern A

  1. Add code to your program to carry out pattern A as follows:

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

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: Again think about the relationship between the number of rows, what row you are on and the number of starts to display.

 


Part 5 -Pattern B

  1. Add code to your program to carry out pattern B as follows:
         *
**
***
****
*****
******
*******
********
*********
**********

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

HINT:  You used starCnt in the example pattern and pattern A.  You should think about using a spaceCnt variable as well to "display" the right number of spaces. How many for loops will you have for this part of the problem?

Also, while developing the code, replace space which is "hidden", with another character which is visible.  This will help you to see what is happening in the code. 


Part 6 - Pattern C

  1. Add code to your program to carry out pattern C as follows:
**********
*********
********
*******
******
*****
****
***
**
*
The top row, leftmost star is in the first position of output.

Also, while developing the code, replace space which is "hidden", with another character which is visible.  This will help you to see what is happening in the code. 


Optional Part 7 - Pattern D - Bragging Rights

  1. Add code to your program to carry out pattern D as follows:
    *
***
*****
*******
*********
*********
*******
*****
***
*
Note, the pattern is defining the number of rows. For an odd number of rows, you would only have one middle line. The middle row leftmost star is in the first position of the output.


updated 10/20/2013 - NLH