// Helene Martin, CSE 142 // Prints a scalable diamond shape made of characters. // NOTE: 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. /* #================# drawLine: #, 16 =, # | <><> | 1 | <>....<> | 2 | <>........<> | 3 |<>............<>| 4 drawTop: repeat 4 (|, some spaces (decreasing), <>, some (increasing) dots, <>, some spaces (decreasing), |) |<>............<>| 4 | <>........<> | 3 | <>....<> | 2 | <><> | 1 drawBottom: repeat 4 (|, some spaces (increasing), <>, some (decreasing) dots, <>, some spaces (increasing), |) #================# 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 Mirror { public static final int SIZE = 4; public static void main(String[] args) { drawLine(); drawTop(); drawBottom(); drawLine(); } // draws a line of = public static void drawLine() { System.out.print("#"); for (int i = 1; i <= SIZE * 4; i++) { System.out.print("="); } System.out.println("#"); } /* | <><> | | <>....<> | | <>........<> | |<>............<>| drawTop: repeat 4 (|, -2 * line + 8 spaces, <>, some (increasing) dots, <>, some spaces (decreasing), |) line spaces -2 * line -2 * line + 8 1 6 -2 6 2 4 -4 4 3 2 -6 2 4 0 -8 line dots 4 * line 4 * line - 4 1 0 4 0 2 4 8 4 3 8 12 4 12 */ // draws a triangle opening towards the bottom public static void drawTop() { for (int line = 1; line <= SIZE; line++) { System.out.print("|"); for (int spaces = 1; spaces <= -2 * line + SIZE * 2; spaces++) { System.out.print(" "); } System.out.print("<>"); for (int dots = 1; dots <= 4 * line - 4; dots++) { System.out.print("."); } System.out.print("<>"); // NOTE: this is the same as the spacing above but we can't // yet reduce this redundancy with what we know. for (int spaces = 1; spaces <= -2 * line + SIZE * 2; spaces++) { System.out.print(" "); } System.out.println("|"); } } // draws a triangle opening toward the top public static void drawBottom() { // we noticed that the top and bottom were related // and just ran the loop backwards for (int line = SIZE; line >= 1; line--) { System.out.print("|"); for (int spaces = 1; spaces <= -2 * line + SIZE * 2; spaces++) { System.out.print(" "); } System.out.print("<>"); for (int dots = 1; dots <= 4 * line - 4; dots++) { System.out.print("."); } System.out.print("<>"); for (int spaces = 1; spaces <= -2 * line + SIZE * 2; spaces++) { System.out.print(" "); } System.out.println("|"); } } }