Han

Han Ferik

Home | Projects | AP CSA

Project Diamond

Tools: VSCode | Source Code: GitHub

The fullDiamond program is designed to generate and display a symmetrical diamond pattern made of asterisks. This pattern is constructed by splitting the diamond into two parts: the upper triangular section and the lower inverted triangular section. The method uses nested for loops to achieve the desired structure and alignment of the asterisks in each row.

The upper triangular section of the diamond begins with a single asterisk in the center of the first row and progressively increases the number of asterisks in odd increments (1, 3, 5, etc.) in subsequent rows. This creates the widening effect of the top half of the diamond. For each row, a spacing loop is used to print the necessary number of spaces before the asterisks to maintain symmetry. The number of spaces decreases as the number of asterisks increases.

The lower inverted triangular section mirrors the upper part but in reverse. It starts with the maximum number of asterisks from the last row of the upper section and decreases the count in odd increments (7, 5, 3, etc.) for each subsequent row. A corresponding spacing loop ensures that the rows remain centered as the number of asterisks reduces. This forms the narrowing effect of the bottom half of the diamond.

The program uses two sets of nested for loops: one for the upper section and one for the lower section. Within each set, the outer loop controls the rows, the first inner loop handles spacing, and the second inner loop prints the asterisks. The careful use of these loops ensures that the diamond pattern remains symmetrical and properly aligned.

Through this program, I practiced using nested for loops to create visual patterns and manage alignment through precise spacing. This project helped reinforce my understanding of how to increment and decrement values in loops, as well as how to structure code to produce visually symmetric outputs. By breaking the diamond into two distinct sections, I was able to simplify the problem and focus on each part independently. This exercise also highlighted the importance of attention to detail when formatting outputs in Java.


Diamond Class Program

Diamond Class Program in Java


		public class Diamond {
		
		    public void half() {
		        for (int i = 0; i < 5; i++) {
		            for (int k = 0; k <= i; k++) {
		                System.out.print("*");
		            }
		            System.out.println();
		        }
		    }
		
		    public void invertedHalf() {
		        for (int a = 5; a >= 1; a--) {
		            for (int b = 1; b <= a; b++) {
		                System.out.print("*");
		            }
		            System.out.println();
		        }
		    }
		
		    public void fullDiamond() {
		        for (int c = 1; c <= 9; c += 2) {
		            for (int counterspace = 9; counterspace > c; counterspace -= 2) {
		                System.out.print(" ");
		            }
		            for (int d = 1; d <= c; d++) {
		                System.out.print("*");
		            }
		            System.out.println();
		        }
		        for (int e = 7; e >= 1; e -= 2) {
		            for (int counterspace = 9; counterspace > e; counterspace -= 2) {
		                System.out.print(" ");
		            }
		            for (int f = 1; f <= e; f++) {
		                System.out.print("*");
		            }
		            System.out.println();
		        }
		    }
		
		    public void fullandhalf() {
		        for (int g = 1; g <= 9; g += 2) {
		            for (int counterspace = 9; counterspace > g; counterspace -= 2) {
		                System.out.print(" ");
		            }
		            for (int h = 1; h <= g; h++) {
		                System.out.print("*");
		            }
		            System.out.print(" ");
		            for (int n = 1; n <= g / 2 + 1; n++) {
		                System.out.print("*");
		            }
		            System.out.println();
		        }
		    }
		
		    public void fullLetter() {
		        String[] Han = {"A", "B", "C", "D", "E", "F", "G", "H", "I"};
		        for (int m = 1; m <= 9; m += 2) {
		            for (int counterspace = 9; counterspace > m; counterspace -= 2) {
		                System.out.print(" ");
		            }
		            for (int o = 0; o < m; o++) {
		                System.out.print(Han[o % Han.length]);
		            }
		            System.out.println();
		        }
		        for (int p = 7; p >= 1; p -= 2) {
		            for (int counterspace = 9; counterspace > p; counterspace -= 2) {
		                System.out.print(" ");
		            }
		            for (int q = 0; q < p; q++) {
		                System.out.print(Han[q % Han.length]);
		            }
		            System.out.println();
		        }
		    }
		
		    public static void main(String[] args) {
		        Diamond D = new Diamond();
		        D.fullLetter();
		        D.fullDiamond();
		        D.invertedHalf();
		        D.half();
		    }
		}
		

Find me on the interwebs!

Github LinkedIn Instagram Facebook