// DEVELOPMENT NOTES: // (These notes would not be in your program's comments. They are here // to help you understand important topics or elements of this code.) // // This program produces part of the Tapestry that we did in class today // It contains a lot of scratch work to indicate how we figured out // expressions to use for number of each type of character dependent on line public class TapestryScratch { public static final int SIZE = 3; public static void main(String[] args) { // first we figured out how to make a figure // for the top half of the tapestry, using pseudocode and loop tables // we did this for a size 4 drawTopHalf(); // then we figured out how to make that figure flexible based // on the size constant and more loop tables // we did the same thing as before with a size 3 and then // compared the size 4 expressions to the size 3 expressions drawTopHalfWithSize(); // here is another example with loop tables based on size for the line drawFringeWithSize(); } /* First we used the output to figure out what our psuedocode was and turned it into real code. for (4 lines): print | print some number of spaces (dependent on which line) print <> print some number of dots (dependent on which line) print <> print some number of spaces (dependent on which line) print | println | <><> | | <>....<> | | <>........<> | |<>............<>| Then: We used loop tables to generate the expression: multiplier * line + offset for each characer dependent on which line it is on line num of dots 4 * line 4 * line - 4 1 0 4 0 2 4 8 4 3 8 12 8 4 12 16 12 general: 4 * line - 4 line num spaces -2 * line -2 * line + 8 1 6 -2 6 2 4 -4 4 3 2 -6 2 4 0 -8 0 general: -2 * line + 8 This expression got plugged into the inner for loops for each of those kinds of characters. */ public static void drawTopHalf() { for (int line = 1; line <= 4; line++) { System.out.print("|"); for (int spaces = 1; spaces <= (-2 * line + 8); 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 + 8); spaces++) { System.out.print(" "); } System.out.println("|"); } } /* Then we needed to figure out how to generalize that to a SIZE constant. We repeated the exact same process for the output for SIZE 3. Loop tables for SIZE 3: | <><> | | <>....<> | |<>........<>| line num spaces -2 * line -2 * line + 6 1 4 -2 4 2 2 -4 2 3 0 -6 0 general: -2 * line + 6 line num dots 4 * line 4 * line - 4 1 0 4 0 2 4 8 4 3 8 12 8 general: 4 * line - 4 Then we made a table to try and compare the two expressions for the different sizes. This is the same kind of linear relationship we represented earlier between characters and line numbers. We do the same multiplier * SIZE + offset. SIZE expression for num spaces expression for num dots 3 -2 * line + 6 4 * line - 4 4 -2 * line + 8 4 * line - 4 general: -2 * line + (2 * SIZE) 4 * line - 4 We get the 2 * SIZE in the general expression from SIZE difference in exp 3 + 6 4 + 8 2 * SIZE + 0 These general equations we used to replace the old expressions in our loop bounds for our inner for loops. We also need to replace the loop bound for our outer for loop since we need to represent the relationship: SIZE number of lines 3 3 4 4 */ public static void drawTopHalfWithSize() { for (int line = 1; line <= SIZE; line++) { System.out.print("|"); for (int spaces = 1; spaces <= (-2 * line + 2 * SIZE); 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 + 2 * SIZE); spaces++) { System.out.print(" "); } System.out.println("|"); } } /* If we do the same thing with the line of equals signs, we see: SIZE num equals 3 12 4 16 4 * SIZE + 0 Again we plug this in where we were using the inflexible number before. */ public static void drawFringeWithSize() { System.out.print("#"); for (int i = 1; i <= 4 * SIZE; i++) { System.out.print("="); } System.out.println("#"); } }