Java Patterns: Rectangle & Rhombus
Java Patterns: Rectangle & Rhombus
Both Solid Rhombus (Pattern 10) and Diamond (Pattern 14) employ spaces to achieve their distinct geometric shapes. In the Solid Rhombus, spaces are printed in a decreasing count before each row of asterisks, resulting in the asterisks forming a rhombus shape. For the Diamond, the upper half increases stars between decreasing spaces, whereas the lower half is a mirror image, creating a symmetric diamond shape across a central axis. This careful placement of initial spaces effectively centres the stars, defining the geometry visualized in each pattern .
The implementation of a solid rhombus (Pattern 10) involves two nested loops: the outer loop manages the number of rows, ensuring each row begins with a certain number of spaces followed by n asterisks. The inner loop for spaces decreases as the row number increases. Conversely, the solid rectangle (Pattern 1) uses two straightforward nested loops without space padding; both loops iterate completely over 'n' to print n asterisks per row. This makes the rhombus require a manipulation of spaces to achieve its shape, whereas the rectangle does not .
In the 0-1 triangle pattern (Pattern 9), the logic '(i + j) % 2 == 0' ensures alternating numbers in the output. The use of this condition alternates the print of '1' and '0'. For each row 'i', it checks if the sum 'i+j' is even or odd, printing '1' for even sums and '0' otherwise. This results in an alternating binary pattern within the triangle structure, creating visual diversity based on row and column indices .
The differentiation between a hollow rectangle and a solid rectangle lies in the use of conditional checks within the inner loop. In a hollow rectangle (Pattern 2), the statement 'if(i==1 || i==n || j==1 || j==n)' determines whether to print an asterisk, leading to filled borders only, whereas a solid rectangle (Pattern 1) prints an asterisk for every position without conditions. This condition results in the edges being outlined with asterisks while leaving the center empty for hollow rectangles .
The Solid Butterfly (Pattern 13) is realized through manipulation of row and column indices to fill stars symmetrically across a center axis. For each 'i', stars print up to 'i' and also from 'i' after a central gap of '2*(n-i)'. The pattern symmetry relies on duplicating the row structure both upwards and downwards from the center, leading to a butterfly effect. Thus, the star printing sequence uniformly transitions from rows 1 through n and repeats the formially to maintain overall symmetry .
The Hollow Butterfly pattern (Pattern 15) achieves symmetry by mirroring its design vertically and maintaining a consistent horizontal span across its upper and lower halves. Hollowness is introduced through selective asterisk printing only at border lines, controlled by checking 'j == 1 || j == i' within the loops. The central gap of spaces at '2*(n-i)' further emphasizes its symmetrical appearance and hollow nature. These elements ensure a balance between symmetrical form and internal emptiness, essential for visualizing a butterfly structure .
The Number Pyramid (Pattern 11) uses the outer loop index 'i' to define both the position and value being printed in each row. For each row 'i', initial spaces align the numbers centrally, and the numbers themselves are printed in sequence matching the row index, ensuring that the number 'i' appears 'i' times. This means the value and count per row both increase in tandem with 'i', creating a hierarchical structure numerically by row, leading to a straightforward but powerful visual pyramid of numbers .
Floyd's Triangle (Pattern 8) uses a single variable 'k' initialized to zero to track the number count incrementally across rows. It prints numbers in increasing order for each dot position within each row. The outer loop manages the rows while the inner loop assigns values to each position, incrementing 'k' and printing it immediately. This ensures that the numbering continues seamlessly from the previous row's last number, systematically creating an incrementally filled triangle. The complexity arises from managing the sequence and maintaining flow across varying row lengths .
Both the Half Pyramid (Pattern 3) and Inverted Half Pyramid (Pattern 4) structures start with a single star, but they differ in their construction direction. The Half Pyramid pattern prints lines where each subsequent row increases in printed asterisks linearly, controlled by nested loops where the inner loop runs up to the current row index 'i'. The Inverted Half Pyramid, however, starts from 'n' stars and decreases the number of printed stars with each proceeding line, as it iterates backwards from 'n' to 1. These patterns exhibit symmetry in their logic but opposite in order .
In the Palindromic Number Pyramid (Pattern 12), numeric palindromes arise by first printing numbers decreasing from 'i' to 1, then extending the sequence by increasing from 2 to 'i'. The loop structure iterates twice: initially, it decrements from 'i' to 1, and then, the latter part of the loop increments from 2 back to 'i'. This setup inherently mirrors numbers around a midpoint, ensuring that each row forms a symmetric palindrome centered on '1', achieving the intended pattern .