Lab DesignDisplayer: for loops Goals: - Write more sophisticated programs. - Experience pair programming. - Thoroughly understand "for loops". - Practice writing programs with nested for loops. - Practice using objects of type char. - Practice debugging and using the debugger. - Practice creating test suites and testing programs. Preliminaries: A count-controlled loop is one where we can tell how many times the body of the loop will be executed by looking at the code. Count-controlled loops have the general form int loop_control_var; loop_control_var = initial value; while (loop_control_var <= final value) { // body of the loop, whatever that may be loop_control_var ++: } Count_controlled looops are so common in programming that there is a shorthand way of writing them, called a "for loop". The general form of a for loop is follows: for ( int loop_control_var = initial value; loop_control_var <= final value; loop_control_var ++: ) { // body of the loop, whatever that may be } The for loop and the count-controlled while loops that are shown above do exactly the same thing! The for loop exists because most programmers find that it is a more convenient way of writing a count-controlled loop. 1. Work with a partner from our course to solve the following problems. The two of you MUST NOT divide the work. Instead, you MUST work together on all parts of this project, looking over each other'shoulder, and using a single keyboard and mouse. This is a programming methodology called "pair programming" that has been shown to result in productivity gains and more reliable code. Create a new BlueJ project called DesignDisplayer. In the project, create a new class called DesignDisplayer and open it in the editor. Paste the contents of DesignDisplayer.java from our website into the editor. Its functionality and implementation are described below in designs 1 and 2. Study the current DesignDisplayer class functionality and implementation with your partner, then enhance the class with implementations of the remaining designs in this lab. Design 1: Implement a method public void displayDesign1 () that displays on the screen **** **** **** A possible implementation is: // displays design 1 public void displayDesign1 () { int l; // line counter for (l=0; l<3; l++) displayALineOfDesign1 (); } // displays a line with 4 asterisks private void displayALineOfDesign1 () { int a; // asterisk counter for (a=0; a<4; a++) System.out.print ("*"); System.out.println (); // wrap up the line } // pedagogical note: you probably recognize this method // from our class discussions Design 2: Implement a method public void displayDesign2 () that displays on the screen **** **** **** **** **** Implementation hints: Design 2 is very similar to design 1. You can even get away with a simple cut-and-paste and a small modification if you can find (by careful analysis) the one small difference between the two designs. After you have successfully tested your implementation of design 2, answer the following: Did you change the name of the method originally called displayALineOfDesign1? If you didn't, consider doing so to reflect it's new extended role... Did you change its javadoc comments? Design 3: Implement a method public void displayDesign3 () that displays on the screen ********** ********** Implementation hints: How is design 3 similar to and different from design 2? Can you use a code structure for design 3 similar to the structure of displayDesign2? Design 4: Implement a method public void displayDesign4 ( int numLines, // desired number of lines; > 0 int numAsterisks ) // > 0 that displays on the screen a "solid rectangle" of asterisks with the desired number (numLines) of lines, and with numAsterisks asterisks per line. Implementation hints: Does you helper method need information from its caller (parameters)? Are you going to do error validation on the input parameter values? Design 5: Similar to the previous design, but the user can also specify the symbol used in the design: public void displayDesign5 ( int numLines, // desired number of lines; > 0 int numSymbols, // > 0 char desiredSym ) that displays on the screen a "rectangle" with numLines lines, and with numSymbols desiredSym per line. Implementation hints: Does you helper method need additional parameters? Design 6: The design is an "right triangle" instead of a "rectangle", with the "base" equal to the "height". For example, if the user wants the size of the design to be 5 and the selected symbol to be '@', the program will display the following design: @ @@ @@@ @@@@ @@@@@ Verify that the value of size is between 1 and 20, inclusive. Implementation hints: Java has a primitive data type, called char that will be useful here. Read up on char with your partner. Design 7: The design is a "slightly different triangle." For example, if the user wants the size of the design to be 5 and the selected symbol to be '@', the program will display the following design: @ @@ @@@ @@@@ @@@@@ Hint: The design consists of 'size' lines. Line 1 consists of 4 leading blanks, followed by 1 selected symbol. Line 2 consists of 3 leading blanks, followed by 2 selected symbol. Line 3 consists of 2 leading blanks, followed by 3 selected symbol. LIne 4 ... Can you detect the "pattern" in each line? Design 8: The design is a bit more complex. For example, if the user wants the size of the design to be 5 and the selected symbol to be '#', the program will display the following design: # ## ### #### ##### #### ### ## # Hint: To display this design, you can first display its top "half", and then its bottom "half". Verify that the value of size is between 1 and 10, inclusive. Design 9: This design is similar to the previous one, except that the design is only the "outline". For example, if the user wants the size of the design to be 5 and the selected symbol to be '#', the program will display the following design: # ## # # # # # # # # # # ## # Hint: Look at a typical line, say line 4. What is the composition of this line? Can you detect the "pattern" in each line? Design 10: This design is similar to the previous one, except using a fixed symbol of '!' every third line. For example, if the user wants the size of the design to be 5 and the selected symbol to be '#', the program will display the following design: # ## ! ! # # # # ! ! # # ## ! Hint: You may want to use the "%" operator, and also a named constant static final char FIXED_SYMBOL = '!'; EXTRA CREDIT Design 11: This design is similar to the previous one, except that it is shaped like an X. For example, if the user wants the size of the design to be 9 and the selected symbol to be '#', the program will display the following design: # # # # ! ! # # # ! ! # # # # ! ! Notice that the size for this design must be odd.