// CSE 142 Lecture 4 // Nested loops cont., class constants /* Note: long comments are to help you remember the process I followed in class. The comments in the code you turn in should not include pseudo-code or loop tables. #================# | <><> | 1 | <>....<> | 2 | <>........<> | 3 |<>............<>| 4 |<>............<>| 4 | <>........<> | 3 | <>....<> | 2 | <><> | 1 #================# #, 16 =, # for each of 4 lines |, some spaces (decreasing), <>, some dots (increasing), <>, same number of spaces, | same 4 lines, but upside-down #, 16 =, # */ // Draw an ascii art mirror that can scale based on SIZE constant. public class Mirror { public static final int SIZE = 4; public static void main(String[] args) { line(); top(); bottom(); line(); } // Prints the line at the top and bottom of the figure public static void line() { System.out.print("#"); // Determine the relationship between SIZE and number of equals signs // SIZE equals SIZE * 4 // 3 12 12 // 4 16 16 // 5 20 20 for (int i = 1; i <= SIZE * 4; i++) { System.out.print("="); } System.out.println("#"); } // Prints the top half of the mirror public static void top() { for (int line = 1; line <= SIZE; line++) { System.out.print("|"); // Determine the relationship between line number and spaces for SIZE = 4 // line spaces line * -2 line * -2 + 8 (here 8 is our offset) // 1 6 -2 6 // 2 4 -4 4 // 3 2 -6 2 // 4 0 -8 0 // Determine the relationship between line number and spaces for SIZE = 3 // line spaces line * -2 line * -2 + 6 (here 6 is our offset) // 1 4 -2 4 // 2 2 -4 2 // 3 0 -6 0 // Determine the relationship between SIZE and the offset calculated // in the previous two loop tables // SIZE offset SIZE * 2 // 3 6 6 // 4 8 8 for (int i = 1; i <= line * -2 + SIZE * 2; i++) { System.out.print(" "); } System.out.print("<>"); // line dots line * 4 line * 4 - 4 // 1 0 4 0 // 2 4 8 4 // 3 8 12 8 // 4 12 16 12 for (int i = 1; i <= line * 4 - 4; i++) { System.out.print("."); } System.out.print("<>"); for (int i = 1; i <= line * -2 + SIZE * 2; i++) { System.out.print(" "); } System.out.println("|"); } } // Prints the bottom half of the mirror public static void bottom() { // Just run the line numbers in reverse! for (int line = SIZE; line >= 1; line--) { System.out.print("|"); for (int i = 1; i <= line * -2 + SIZE * 2; i++) { System.out.print(" "); } System.out.print("<>"); for (int i = 1; i <= line * 4 - 4; i++) { System.out.print("."); } System.out.print("<>"); for (int i = 1; i <= line * -2 + SIZE * 2; i++) { System.out.print(" "); } System.out.println("|"); } } }