Lab 11: Pattern Printer
Nested loops, print vs println, and ASCII art
Learning Objectives
After completing this lab, you will be able to:
- Use nested
forloops where the inner loop bound depends on the outer loop variable - Distinguish between
System.out.print()(stays on the line) andSystem.out.println()(moves to the next line) - Apply the fencepost pattern to handle leading/trailing spaces
- Translate a visual pattern into loop bounds systematically
What You’re Building
Three static methods that each print an ASCII pattern of a given size n. You will build from simple (rectangle) to complex (centered diamond). Each method takes an int n parameter and prints directly to the console.
Concepts and Common Misconceptions
| Concept | What students get wrong |
|---|---|
print vs println |
Using println("*") for every star, which puts each on its own line. Use print("*") inside the inner loop and println() after the inner loop to move to the next row. |
| Nested loop bounds | Making the inner loop always run n times. For a triangle, the inner loop count must change with the outer loop variable. |
| Fencepost / spaces | Forgetting leading spaces in centered patterns. The diamond requires n - 1 - row spaces before the first star on each row. |
| Off-by-one | A size-5 diamond has 9 rows (5 top half including middle, 4 bottom half), not 10. |
Checkpoint 1: Rectangle (1 pt)
Write printRectangle(int n) that prints an n x n grid of * characters.
For n = 4:
****
****
****
****
Outer loop: rows 0 to n - 1. Inner loop: columns 0 to n - 1. Print * with print, then println after each row.
Debugging tip: If all stars appear on one line, you forgot the println() after the inner loop. If each star is on its own line, you used println("*") instead of print("*").
Checkpoint 2: Right Triangle (1 pt)
Write printTriangle(int n) that prints a right triangle with n rows.
For n = 5:
*
**
***
****
*****
The inner loop runs from 0 to row (inclusive) on each row, printing row + 1 stars.
Debugging tip: If your triangle is upside down, your inner loop bound is n - row instead of row + 1. If the first row has no stars, check whether your outer loop starts at 0 or 1.
Checkpoint 3: Centered Diamond (1 pt)
Write printDiamond(int n) that prints a centered diamond. n is the half-height (the number of rows in the top half, including the middle row). The total height is 2 * n - 1.
For n = 4:
*
***
*****
*******
*****
***
*
Break this into two halves. Top half (rows 0 to n - 1): print n - 1 - row spaces, then 2 * row + 1 stars. Bottom half (rows 0 to n - 2): mirror the top half.
Debugging tip: If your diamond is lopsided, print the space count and star count for each row before printing the actual characters. Verify the numbers match the pattern above.
How to Debug
- Start with n = 3. Small inputs make it easy to count characters by hand.
- Print row and column indices. Replace
*withrow + "," + coltemporarily to see exactly which iteration produces each character. - Build a table. For each row, write down: number of spaces, number of stars. Then translate that table into loop bounds.
- Test the top half alone. Get the top half of the diamond working before adding the bottom half.
Scoring Breakdown
| Component | Points |
|---|---|
| Checkpoint 1: Rectangle | 1 |
| Checkpoint 2: Right triangle | 1 |
| Checkpoint 3: Centered diamond | 1 |
| Autograder correctness | 5 |
| Timeliness (on-time submission) | 2 |
| Total | 10 |