Java Patterns: Stars, Numbers & Shapes
Java Patterns: Stars, Numbers & Shapes
In Floyd's Triangle Right-Aligned, the `width` variable ensures numeric alignment across rows by calculating it as the number of digits in the maximum expected number (`maxNum`). This value includes extra space allowances to assure text-based alignment remains consistent till the last number. Each number printed is adjusted rightwards by padding it with spaces to attain this computed width. By using this consistent overriding width, numbers align neatly on the right, irrespective of their digit count .
The Numeric Diamond pattern uses a counter to continuously increment numbers till the middle row reaches its maximum, resetting sequentially in the lower half based on a calculated decrement. The lower half substracts from the counter by computing the reduction value required to set back the sequence appropriately for each row's start, maintaining distinct continuity. This incremental and corrective alternating approach allows for seamless numeric flow across top and bottom sections of the diamond, ensuring precise continuation of the sequence .
The zigzag pattern is generated using a two-dimensional array to map stars and spaces over three rows for `n` columns. The logic involves iterating over columns and placing a star in a current row, which changes based on direction. Initially, direction is downward (increased row number), and upon reaching the last row, the direction reverses. This results in a visible zigzag motion as the stars are placed accordingly on their path across the columns .
The hollow diamond pattern maintains balance for both even and odd `n` by structuring iterations that separately handle the upper (including middle) and lower halves of the diamond. The pattern starts with leading spaces, adjusting by increment or decrement for each level, and adds stars with spaces in between for lower levels of each half, creating uniform sections irrespective of the total height. The specific condition checks for even/odd values ensure single star placements correctly appear at the topmost and bottom-most sections, maintaining the overall symmetry and structure .
The algorithm for generating the Concentric Rectangular Number Pattern calculates the pattern size as `2*n - 1` and iterates over each coordinate in this size. For any coordinate `(i, j)`, it determines the nearest boundary by calculating the minimum distance to any of the four edges of the matrix: top, bottom, left, and right. This minimum value (`val`) represents the 'layer' of numbers, and it is subtracted from `n` to determine the number to be printed. This approach ensures each layer around the matrix has decrementing numbers as layers progress inwards .
The Mirror Number Pyramid achieves alignment using spaces for leading adjustment. In each row `i`, spaces are printed decreasingly (`i` to `n`), allowing numbers to have a shifted placement. This aligns the edges each time, creating the pyramid shape. Additionally, numbers are first decreased to 1 from the current `i`, then counter-increased back to `i`, filling a pyramid row. The use of consistent space decreases and exact number increments ensures both left and right sides of numbers appear equidistant from each edge, visually structuring the pyramid .
The program evaluates a set of conditions for each position `(i, j)` in an `n x n` grid: if a position lies on any border `(i == 0 || j == 0 || i == n-1 || j == n-1)` or on the primary diagonal `(i == j)` or secondary diagonal `(i + j == n - 1)`, it prints a star. Otherwise, it prints a space. This approach effectively outlines the square and prints stars along its diagonals inside the square, creating a symmetric and visually distinct pattern .
The hourglass number pattern exhibits symmetry through its vertically mirrored structure. In both halves, numbers increment sequentially from 1 up to a peak determined by `n`, then decrement back to 1. Multiple loops manage spaces and numbers: in the upper half, numbers decrease, spaces increase with each row; in the lower half, the process reverses—spaces decrease, and numbers mirror the structure upwards again. This design ensures the top and bottom halves mirror each other perfectly around the central maximal point .
The order of these conditions is crucial as they serve as guards to exit the loop when the spiral formation reaches the center or when boundaries overlap, indicating that all elements in the matrix have been filled. The checks `(left > right)` and `(top > bottom)` ensure the loop exits before attempting to access array indices that would have otherwise exceeded bounds, thereby preventing logic errors and ensuring the spiral completes only once perfectly around the matrix grid .
The butterfly pattern's upper half involves increasing the number of stars from 1 up to a peak at `n`, interspersed with spaces which decrease as the rows progress to maintain symmetry. This pattern is mirrored in the lower half, where the stars start decreasing, maintaining the same symmetry of spaces. The transition from upper to lower half is smooth, as the bottom of the upper half and the top of the lower half maintain a similar number of stars and spaces, ensuring a continuous symmetrical flow .