// CSE 142, Autumn 2009 // Marty Stepp // // This program produces a text figure that looks like a mirror. // It uses nested loops to capture the repetition of characters on each line. // This version also uses a class constant so that the figure can be resized. public class MirrorConstant { public static final int SIZE = 7; public static void main(String[] args) { line(); topHalf(); bottomHalf(); line(); } // Draws the figure's top/bottom line of #===...===# . public static void line() { System.out.print("#"); for (int equal = 1; equal <= (4 * SIZE); equal++) { System.out.print("="); } System.out.println("#"); } // Draws the top half of the mirror's center, // with patterns of lines represented by nested loops. public static void topHalf() { for (int line = 1; line <= SIZE; line++) { // print the contents of each line // | System.out.print("|"); // print a space, (-2 * line + (2 * SIZE)) times for (int space = 1; space <= (-2 * line + (2 * SIZE)); space++) { System.out.print(" "); } // <> System.out.print("<>"); // print a dot, (4 * line - 4) times for (int dot = 1; dot <= (4 * line - 4); dot++) { System.out.print("."); } // <> System.out.print("<>"); // print a space, (-2 * line + (2 * SIZE)) times for (int space = 1; space <= (-2 * line + (2 * SIZE)); space++) { System.out.print(" "); } // | System.out.println("|"); } } // Draws the bottom half of the mirror's center, // with patterns of lines represented by nested loops. // This is the same code as the top half, but the order of // the lines has been reversed. public static void bottomHalf() { for (int line = SIZE; line >= 1; line--) { // print the contents of each line // | System.out.print("|"); // print a space, (-2 * line + (2 * SIZE)) times for (int space = 1; space <= (-2 * line + (2 * SIZE)); space++) { System.out.print(" "); } // <> System.out.print("<>"); // print a dot, (4 * line - 4) times for (int dot = 1; dot <= (4 * line - 4); dot++) { System.out.print("."); } // <> System.out.print("<>"); // print a space, (-2 * line + (2 * SIZE)) times for (int space = 1; space <= (-2 * line + (2 * SIZE)); space++) { System.out.print(" "); } // | System.out.println("|"); } } }