// Helene Martin, CSE 142 // Prints a scalable diamond shape made of ASCII characters. // NOTE: goals, pseudocode and loop tables should NOT appear in your final program's // comments. They appear here so you can recall what we did in lecture. // goal: demonstrate use of pseudocode and using loop tables to express complex // patterns as loops. Demonstrate use of a class constant. /* #================# | <><> |1 | <>....<> |2 | <>........<> |3 |<>............<>|4 |<>............<>|4 | <>........<> |3 | <>....<> |2 | <><> |1 #================# line: #, 16 =, # topHalf: repeat for 4 lines (|, some spaces, <>, some dots, <>, some spaces, |) bottomHalf: repeat for 4 lines (|, some spaces, <>, some dots, <>, some spaces, |) Scaling: Note that only the spaces are different. The number of dots is the SAME for size 3 or 4. #============# | <><> | | <>....<> | |<>........<>| |<>........<>| | <>....<> | | <><> | #============# SIZE 4 line * -2 + 8 3 line * -2 + 6 SIZE line * -2 + SIZE * 2 */ public class Diamond { public static final int SIZE = 3; public static void main(String[] args) { drawLine(); drawTopHalf(); drawBottomHalf(); drawLine(); } // line: #, 16 =, # -- we need to use a loop for scaling // prints a line of equal signs public static void drawLine() { System.out.print("#"); for (int i = 1; i <= SIZE * 4; i++) { System.out.print("="); } System.out.println("#"); } /* topHalf: repeat for 4 lines (|, some spaces, <>, some dots, <>, some spaces, |, new line) | <><> | | <>....<> | | <>........<> | |<>............<>| line spaces line * -2 line * -2 + 8 dots line * 4 line * 4 -4 1 6 -2 6 0 4 0 2 4 -4 4 4 8 3 2 -6 8 12 4 0 */ // draws an isosceles triangle that opens downwards public static void drawTopHalf() { for (int line = 1; line <= SIZE; line++) { System.out.print("|"); for (int spaces = 1; spaces <= line * -2 + SIZE * 2; spaces++) { System.out.print(" "); } System.out.print("<>"); for (int dots = 1; dots <= line * 4 - 4; dots++) { System.out.print("."); } System.out.print("<>"); for (int spaces = 1; spaces <= line * -2 + SIZE * 2; spaces++) { System.out.print(" "); } System.out.println("|"); } } // we noticed that the top and bottom are symmetrical and just ran // the loop the other way // draws an isosceles triangle that opens upwards public static void drawBottomHalf() { for (int line = SIZE; line >= 1; line--) { System.out.print("|"); for (int spaces = 1; spaces <= line * -2 + SIZE * 2; spaces++) { System.out.print(" "); } System.out.print("<>"); for (int dots = 1; dots <= line * 4 -4; dots++) { System.out.print("."); } System.out.print("<>"); for (int spaces = 1; spaces <= line * -2 + SIZE * 2; spaces++) { System.out.print(" "); } System.out.println("|"); } } }