Java Academy · Lesson

Pattern Practice & Mini-Challenges

Practice nested loop patterns: hollow rectangles, centered pyramids, and diagonals. Tackle mini-challenges step by step.

Lesson 3 of 38 steps

Pattern Practice & Mini-Challenges is a free Java Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.

Pattern Tips

Tips for text patterns:

  • Outer loop controls rows
  • Inner loop builds each row
  • Append characters to a String then print once
  • Use conditions to choose star or space

Hollow Rectangle

Hollow rectangle 3x5

public class Main {
  public static void main(String[] args) {
    // Outer loop: goes through each row (1 to 3)
    for (int r = 1; r <= 3; r = r + 1) {
      String line = "";

      // Inner loop: goes through each column (1 to 5)
      for (int c = 1; c <= 5; c = c + 1) {
        // Print a star if we are on:
        // - the first row (r == 1)
        // - the last row (r == 3)
        // - the first column (c == 1)
        // - the last column (c == 5)
        if (r == 1 || r == 3 || c == 1 || c == 5) {
          line = line + "*";
        } else {
          // Otherwise, print a space (inside of the rectangle)
          line = line + " ";
        }
      }

      // Print the completed row
      System.out.println(line);
    }
  }
}

Centered Pyramid

Centered pyramid height 4

public class Main {
  public static void main(String[] args) {
    int h = 4; // height of the pyramid

    // Outer loop: goes through each row (1 to h)
    for (int r = 1; r <= h; r = r + 1) {
      String line = "";

      // First inner loop: add spaces before the stars
      // (h - r) spaces so the stars are centered
      for (int s = 1; s <= (h - r); s = s + 1) {
        line = line + " ";
      }

      // Second inner loop: add stars
      // Formula (2*r - 1) makes the row width odd (1, 3, 5, 7, ...)
      for (int c = 1; c <= (2 * r - 1); c = c + 1) {
        line = line + "*";
      }

      // Print the completed row
      System.out.println(line);
    }
  }
}

Diagonal

Diagonal in a 4x4 square

public class Main {
  public static void main(String[] args) {
    int n = 4; // size of the square (4 rows and 4 columns)

    // Outer loop: controls rows (1 to n)
    for (int r = 1; r <= n; r = r + 1) {
      String line = "";

      // Inner loop: controls columns (1 to n)
      for (int c = 1; c <= n; c = c + 1) {
        // If row index == column index, print a star
        if (r == c) {
          line = line + "*";
        } else {
          // Otherwise, print a space
          line = line + " ";
        }
      }

      // Print the row
      System.out.println(line);
    }
  }
}

Mini Challenges

Mini-challenges

  1. Hollow right triangle height 4
  2. Checkerboard 4x4 using two characters
  3. Centered diamond with odd height 5

Write small steps, build the line string, then print.

Patterns Demo

Run it: Three patterns printed without any console input.

public class Main {
  public static void main(String[] args) {
    // 1) Hollow rectangle 3x5
    for (int r = 1; r <= 3; r = r + 1) {
      String line = "";
      for (int c = 1; c <= 5; c = c + 1) {
        if (r == 1 || r == 3 || c == 1 || c == 5) line = line + "*";
        else line = line + " ";
      }
      System.out.println(line);
    }

    // 2) Centered pyramid height 4
    int h = 4;
    for (int r = 1; r <= h; r = r + 1) {
      String line = "";
      for (int s = 1; s <= (h - r); s = s + 1) line = line + " ";
      for (int c = 1; c <= (2 * r - 1); c = c + 1) line = line + "*";
      System.out.println(line);
    }

    // 3) Diagonal in a 4x4 square
    int n = 4;
    for (int r = 1; r <= n; r = r + 1) {
      String line = "";
      for (int c = 1; c <= n; c = c + 1) {
        if (r == c) line = line + "*";
        else line = line + " ";
      }
      System.out.println(line);
    }
  }
}

Hollow Rectangle Check

Quick check: Which snippets print a 3x5 hollow rectangle?

Recap & Next

Recap: You practiced hollow shapes, centered pyramids, and diagonals. Keep pattern building with nested loops.

Next: Move on to arrays to store and process collections.

Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “Pattern Practice & Mini-Challenges” lesson free?

Yes — the full text of “Pattern Practice & Mini-Challenges” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.

What will I learn in “Pattern Practice & Mini-Challenges”?

Practice nested loop patterns: hollow rectangles, centered pyramids, and diagonals. Tackle mini-challenges step by step. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Pattern Practice & Mini-Challenges” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. For Loop Basics
  2. Nested For & Text Patterns
  3. Pattern Practice & Mini-Challenges
← Back to Java Academy