CSC 120 Lab 7

In this lab we will practice with for loops and drawing repetitive patterns with loops

  1. Create a new NetBeans project called Lab7a, and copy source files to it.

    1. File | New Project, etc. Make sure the Project Location is in your H:\CSC120\Java folder, and the Project Name is Lab7a.

    2. Right-click on the following link and save this .zip file in your H:\CSC120 folder:
      Lab7aStarterFiles.zip
    3. Use Windows Explorer to Extract All from the .zip file you saved in the previous step, and then copy BOTH files that were extracted, and paste them in the src folder of your project.

  2. Run the program.

    1. Run the program as it is given.  Five lines are displayed in the Text Area on screen.  Then examine the program in "Source" view. Look at the source code of the program -- there is only one outputArea.append statement inside the for loop (it is spread across several lines), which is executed multiple times because of the loop in the program.

  3. Make changes to the given program.

    1. Make the value of the variable down take on the values 7, 14, 21, 28, 35 as the program is executed.  Use the same type of code to do this as is used in the program to cause over take on a series of values.  If you do this properly, the values of down shown above will be printed in the Text Area on screen.  Run your program and see if you can obtain correct output.

    2. Make the program print 20 lines of output in the Text Area.  Run the program and count to make sure that 20 lines of output are printed.

  4. Add code to change other variables.

    1. Inside the body of the constructor method for the MUFrame class, declare two more Integer variables called width and height.

    2. Make these variables take on the following values:  width should start with 700, and repeatedly decrease by 10 each time through the loop; height should start with 1, and double itself each time through the loop.

    3. Add code to the outputArea.append statement inside the loop so that all four variable values are printed in the Text Area on one line, with appropriate labels preceding each numeric value.

    4. Run the program, test, and fix as needed.

  5. Create a new NetBeans project called Lab7b, and copy source files to it.

    1. Close the current Lab7a project in NetBeans.

    2. Choose "File | New Project, etc." from the NetBeans top-level menu bar.  Make sure the Project Location is in your H:\CSC120\Java folder, and the Project Name is Lab7b.

    3. Right-click on the following link and save this .zip file in your H:\CSC120 folder:
      Lab7bStarterFiles.zip
    4. Use Windows Explorer to Extract All from the .zip file you saved in the previous step, and then copy ALL THREE files that were extracted, and paste them in the src folder of your project.

    5. Run the program as it is given.  Notice that 7 blue bars are displayed in the output window.  Examine the MUPanel.java file and make sure you understand why the pattern that is drawn shows up as it does.

  6. Draw patterns using loops.

    1. Change the code in the given MUPanel so that the patterns that may be viewed by clicking THIS LINK are drawn in the output window.

    2. For each of the patterns shown, change the program and run it to produce the pattern, then capture a screen shot image of the output window and save it, either in a Word document or as a .png or .jpg file.

      • In Windows, the keyboard combination [Alt]-[PrtSc] captures a screen shot of the current window, then you can paste the image into a Word document or you can paste it into MS Paint and save it as a separate image file.  Ask your instructor for details if you need help doing this.

      • Here is a description of how this works in Microsoft Windows.  You'll want to do this if you are using the CS Lab Virtual Image, because the image is a virtual Windows computer, no matter what kind of computer you actually have.

      • Here are instructions for how to perform a screen capture in Mac OSX.

    3. Keep changing the program from pattern to pattern as you go along -- you don't need to keep each version of the program in a separate NetBeans project (unless you want to do that).

    4. Hint:  each trip through the loop, after all the drawing is done, changing the values of over, down, width and/or height will cause the next shape to be drawn differently the next time through the loop.  What is changing in the pattern from shape to shape?  Those variables are what need to be changed inside the loop body.

    5. Hint:  to make every other color (or every third color, etc.) change to a different color, you'll need to use the % operation to calculate the remainder of a division problem.

    6. Ask your instructor for help with any pattern you have trouble generating with a loop.

  7. Submit your Lab7b project for grading.
    Be sure that the screenshots of all of your patterns are saved in the src folder of your Lab7b NetBeans project folder, and then make a .zip file of the Lab7b folder.  Turn in ONLY the final version of the Lab7b project into the D2L dropbox for this assignment.  You are not required to turn in Lab7a for this assignment.