CS 312 Assignment 2 - Using Loops

Programming Assignment 2: Individual Assignment. You must complete this assignment by yourself. You cannot copy code from anyone else in the class or from someone outside of the class. You cannot show your code to any other student in the class. You are encouraged to get help from the instructional staff. You may post general questions to Piazza. Do not post more than 2 lines of code to Piazza.

Placed online: Wednesday, September 10
20 points, ~2% of total grade
Due: no later than 11 pm, Thursday, September 18
General Assignment Requirements

Description: The purposes of this assignment are:

  1. decompose a complex problem into simpler parts
  2. generalize a problem
  3. practice writing static methods
  4. practice using print and println statements
  5. practice identifying and using appropriate program constants.
  6. Practicing using for loops and nested for loops.

The UT TOWER
Create a program that produces an ASCII art representation of the UT Tower. he output will vary based on a constant in the program. By simply changing the constant the output will change (somewhat) dramatically. In the end your program must match the outputs shown for the give value of the SIZE constant EXACTLY.

Here is the program output with the constant named SIZE set to 3.

              #####
              |||||
              |||||
              |||||
              |||||
              #####
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
            |-O-O-O-|
            ~~~~~~~~~
   /"'"'"'"'"'"'"'"'"'"'"'"'"\
/"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"\
/"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"\
/"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"\
/"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"\

When the SIZE constant is changed to 4, the following output is produced:

                  #######
                  |||||||
                  |||||||
                  |||||||
                  |||||||
                  |||||||
                  |||||||
                  #######
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
                |-O-O-O-O-|
                ~~~~~~~~~~~
      /"'"'"'"'"'"'"'"'"'"'"'"'"'"'"\
   /"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"\
/"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"\
/"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"\
/"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"\
/"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"\
/"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"O"\

This assignment is meant to give you practice with the constructs from chapters 1 and 2 of the textbook. (println and print statements, static methods, decomposition, loops, nested loops, and class / program constantsThis will require you to create nested for loops with print and println statements that use the class constant and local variables. You may not use anything besides the constructs from chapters 1 and 2. Specifically you MAY NOT use parameters or methods that return values (chapter 3 in the book) on this assignment or conditional statements (if, if/else statements)

You shall use static methods to structure your solution.  You shall avoid significant redundancy and you shall structure your program in such a way that the methods match the structure of the output itself.  You are required to properly indent your source code and will lose points if your indenting is not readable and consistent  You should localize variables whenever possible. You shall have class constant named SIZE. You will also use, but not change, another class that sets the size variable.

Do not alter this line of code in the Tower program:

public static final int SIZE = TowerSize.getSize();

To change the size constant go to the TowerSize class and change this line with a new value:

private static int size = 3;

Note how the SIZE constant will determine several things such as the height of the top of the tower, the number of floors, the number of lines that make up the top of the base, and the number of lines in the bottom of the base. Note how the SIZE constant is used to determine the number of windows (the capital Os) in the floors of the main tower and the base and things such as the number of spaces before the lines at the top of the tower and the main part of the tower.

The SIZE constant must be greater than or equal to 2.

As a reference my solution consists of approximately 115 lines including blank lines and comments. The problem is broken up into 7 - 9 methods not counting the main method.

Complete the comment at the beginning of your Tower.java program. Include a comment for each method you write explaining the purpose of that method.

On any given execution your program will produce just one version of this figure, but it should be possible to change the value of the program constant to have your program produce a Tower of a different size. Here are files with the expected out for size 2, size 3, size 4, size 5, size 6, and size 7. Your output must match these exactly for a given size or you will lose points for correctness. Use a diff tool such as the one at this website ( www.quickdiff.com) to ensure your program produces the correct output.

If you are using BlueJ: Recall, Bluej truncates the output in the console to approximately 20 lines. This results in your output being cutoff in some cases. You can change this. Run your program. In the Options menu on the Terminal Window that pops up when you run your program, ensure ensure the menu options Clear screen at method call and Unlimited buffering are checked. After making these changes you may have to stop and restart BlueJ for the choice to take effect.

When finished turn in your Tower.java via Canvas.

Provided File Responsibility
TowerSize.java (Provided file) Provided by me. Do not turn in.
Tower.java (Provided shell) You and me. (Okay, mostly you.)

Checklist: Did you remember to:

Thanks to Stuart Reges and Marty Stepp for this assignment idea.

Tips: Development strategy. The program is not trivial and will take some time to do. You should complete it in stages and may have to spend several hours on it. Work with pen and paper before writing code. Some ideas:

  1. Tables: Examine the output for a given value of SIZE. Examine the output and create tables for number of lines, spaces, windows, etc. to discover the patterns for each line and for sections of the figure. Compare the pattern with different values for SIZE to check your pattern.
  2. Decompose the problem. Break the problem up based on the sections of the tower.
  3. Code without constant. Write the code to display the UT Tower with the default SIZE of 3.
  4. Code with the constant. Add the constant to your code and try different values to see if you program scales correctly. If not you need to correct your pattern / code. Do this with pencil and paper, not by fiddling with the code. (One experiment is okay, but if that doesn't work it is usually necessary to back up and revert to pencil and paper.)
  5. Review examples: Look at the examples from the book, especially chapter 2 to see how to develop patterns for output and the for loops.

Here is some really good advice from a former student,  Jeff Lyon, on how to approach this assignment and what to do if you get stuck.

  1. Go back and review sections 1.5 and 2.5 of the text regarding how to do structural decomposition.
     
  2. Review the material in chapter 2 on nested loops and code a couple of simple little "toy-code" examples, tweaking them to change the output until you're solid on the syntax of making those work.  If you don't nail down that part, you won't be able to do the assignment.
     
  3. Step through, space by space, line by line what the output should look like.  Imagine your where trying to explain to someone by phone how to reproduce the figure exactly on a typewriter.  Write out instructions for them.  Once you start seeing the patterns, then you can start to generalize and match it up with things you did in #2 above, but first do it space by space.
     
  4. Writing lots of detailed comments before the code really helped me get it clear in my head what each segment was doing.  In my case, the comments where essentially pseudocode once I was done.
     
  5. As you code, write from the outside in; code empty "shells" for your methods, compile and error check, then go a in level, repeat. This is a top down implementation approach.

As you can see, you really need to pay attention to details on this assignment. And that is true of computer science and software development in general.

Back to the CS 312 homepage.