Method Overloading polygon() Pattern Program in Java (ICSE)

⚑ Direct Answer & Key Takeaway

The ICSE polygon() overloading task pairs three void methods with distinct parameter lists to render console patterns via nested for loops: polygon(int n, char ch) prints an n Γ— n square of ch, polygon(int x, int y) prints an x-row by y-column rectangle of @, and zero-argument polygon() prints a growing * triangle. Outer loops advance rows with println(), while inner loops repeat symbols on the same line via print().

1. Zero-Argument vs. Parameterized Overload Design

The polygon() problem shows how a single class can combine a zero-argument overload with two-argument overloads cleanly. Calling obj.polygon() triggers the parameterless triangle builder, obj.polygon(n, ch) draws a custom-symbol square, and obj.polygon(x, y) draws an @-filled rectangle. Notice that both two-argument methods accept two values yet remain unambiguous because their second parameter types differ (char versus int). Keep all three return types as void since each method renders multi-line console shapes directly rather than returning a single scalar value.

  • Zero-argument polygon() demonstrates overloading with arity 0 alongside arity 2.
  • polygon(int, char) and polygon(int, int) share 2 parameters but differ in the second type.
  • All three methods use void because console drawing has no numerical return result.

2. Controlling Rows and Columns with Nested Loops

Every two-dimensional pattern relies on two nested for loops: an outer loop i counting rows from 1 to the height, and an inner loop j printing characters across each row. For the square and rectangle, the inner limit stays constant (n or y), producing uniform rows. For the right-angled triangle inside polygon(), the inner loop stops at i (j <= i), adding one extra * on every new line. Always place System.out.print() inside the inner loop and System.out.println() outside it so characters stay on the same row.

  • Outer loop variable i controls the total number of horizontal lines printed.
  • Inner loop variable j controls how many characters appear on the current line.
  • Empty System.out.println() advances the cursor only after an entire row completes.

3. Dry-Running Loop Counters for ICSE Section B

Examiners award full Section B marks when your dry run proves you understand how loop bounds behave on boundary values. For instance, calling obj.polygon(3, '#') runs outer i for 1, 2, 3 and inner j three times per row, printing a 3 Γ— 3 grid of #. Calling obj.polygon(2, 4) limits rows to 2 and columns to 4, catching swapped x and y variables immediately. Documenting n, ch, x, y, i, j clearly in the Variable Description Table makes your logic effortless to verify.

  • Test non-square dimensions such as x = 2, y = 4 so row/column swaps show up immediately.
  • Show row-by-row counter progression (i = 1 -> *, i = 2 -> **, i = 3 -> ***) during exam dry runs.
  • List loop indices i and j explicitly in your Variable Description Table.

4. PolygonPattern.java β€” Overloaded polygon() Nested Loop Patterns

import java.util.*;

public class PolygonPattern {
    public void polygon(int n, char ch) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.print(ch);
            }
            System.out.println();
        }
    }

    public void polygon(int x, int y) {
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= y; j++) {
                System.out.print("@");
            }
            System.out.println();
        }
    }

    public void polygon() {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PolygonPattern pp = new PolygonPattern();

        System.out.print("Enter side and character for square: ");
        int n = sc.nextInt();
        char ch = sc.next().charAt(0);
        pp.polygon(n, ch);

        System.out.print("Enter rows and columns for rectangle: ");
        int x = sc.nextInt();
        int y = sc.nextInt();
        pp.polygon(x, y);

        System.out.println("Right-angled triangle pattern:");
        pp.polygon();

        sc.close();
    }
}

Verified Console / Dry-Run Output:

Enter side and character for square: 3 #
###
###
###
Enter rows and columns for rectangle: 2 4
@@@@
@@@@
Right-angled triangle pattern:
*
**
***

5. CISCE Variable Description Table (VDT) & Dry Run

VariableData TypeExam Purpose & Sample Value
nintSide length of the filled square (3)
chcharFill character passed to polygon(int, char) ('#')
x, yintRow count and column count of the rectangle (2, 4)
iintOuter loop counter controlling row number (1..3)
jintInner loop counter controlling symbols per row (1..4)
ppPolygonPatternObject reference used to invoke all polygon() methods
🎯 1-on-1 Live Mentorship & Free Study Kit

Conquer Java Pattern Programs Live

Build confidence with nested loops, pattern dry runs, and ICSE overloaded polygon() questions in a 1-on-1 live BlueJ class.

πŸ’¬ WhatsApp Now
πŸ”’ Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Calling System.out.println(ch) inside the inner loop instead of System.out.print(ch), printing one vertical column.
  • Avoid Mistake: Swapping row count x and column count y in polygon(int x, int y), flipping width and height.
  • Avoid Mistake: Using j <= 3 instead of j <= i inside zero-argument polygon(), printing a 3x3 square instead of a triangle.
  • Examiner / Mentor Tip: Draw a miniature 3-row output sketch next to your rough work before coding to confirm i and j limits.
  • Examiner / Mentor Tip: Match exact symbols required by the question ('@' for rectangle, '*' for triangle, user char for square).
  • Examiner / Mentor Tip: Never omit the empty System.out.println() after the inner loopβ€”it is the #1 bug in ICSE pattern code.

7. Interactive Concept Check Quiz

Question: What pattern does pp.polygon(2, 4) print in the PolygonPattern class?

8. Frequently Asked Questions

Can Java confuse polygon(int, char) and polygon(int, int) at compile time?
Not when you pass an int and a char literal such as obj.polygon(3, '#'), because (int, char) is an exact parameter-type match. When you pass two integers like obj.polygon(2, 4), Java immediately selects (int, int) as an exact match, keeping both two-argument signatures completely distinct and unambiguous at compile time.
Why did my polygon() pattern print a single vertical column instead of a grid?
That happens when you accidentally write System.out.println(ch) inside the inner loop instead of System.out.print(ch). Using println() moves the cursor to a new line after every single character. Keep print() inside the inner j loop and place an empty System.out.println() at the end of the outer i loop.
How many rows should zero-argument polygon() print in ICSE exams?
When a board question gives a visual sample such as 3 rows (*, **, ***), match that exact row count in your loop bound (i <= 3). If no count is specified, 3 to 5 rows is standard. State the chosen row count clearly in your comments or VDT.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class