CS 312 Assignment - Problem Decomposition

Programming Assignment 1: Individual Assignment. You must complete this assignment by yourself. You cannot work with anyone else in the class or with someone outside of the class. The code your write must be your own. You are encouraged to get help from the instructional staff.

Placed online: Thursday, August 21
10 points, ~1% of total grade
Due: no later than 11 pm, Thursday, September 11
General Assignment Requirements

Description: The purposes of this assignment are:

  1. To create a program to produce required output.
  2. To practice creating structured programs
  3. To practice removing redundancy from programs.

Steps to Complete Assignment: (If you get stuck post to the class discussion group on Piazza or go see the instructor or TA in the lab.)

  1. Go to https://apps.cs.utexas.edu/udb/newaccount/ to request a CS lab account.
     
  2. Sign up for the class discussion group on Piazza if you have not already signed up.
  3. (If you plan on working in the lab and not on your own system skip to step 5.)
    If you are planning on working at home during the semester download Java on to your computer by following the instructions on the class web page.
     
  4. If you are planning on working at home during the semester  and you want to use an interactive development environment to help you create and run Java programs I suggest you use BlueJ or Eclipse. See this page for links for downloading and running BlueJ.

    BlueJ truncates the output in the console to approximately 20 lines. This results in your output being cutoff in some cases.


    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.
     
  5. Create a new project and add the Song.java file. This page has instructions on how to create projects and add source code to BlueJ.
     
  6. Complete the required program as described below.
     
  7. Save your program! If you are working in the lab on a Windows machine it is very important to do this. The microlab Windows machines do not save your work! 

Complete a Java program (here is the program shell to start with) that when run produces the following output (The lyrics of the song There Was an Old Woman Who Swallowed a Fly):

There was an old woman who swallowed a fly.
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old woman who swallowed a spider,
That wriggled and iggled and jiggled inside her.
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old woman who swallowed a bird,
How absurd to swallow a bird.
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old woman who swallowed a cat,
Imagine that to swallow a cat.
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old woman who swallowed a dog,
What a hog to swallow a dog.
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old woman who swallowed a goat,
She just opened her throat to swallow a goat.
She swallowed the goat to catch the dog,
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old woman who swallowed a cow,
I don't know how she swallowed a cow.
She swallowed the cow to catch the goat,
She swallowed the goat to catch the dog,
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly,
I don't know why she swallowed that fly,
Perhaps she'll die.

There was an old woman who swallowed a horse,
She died of course.

Here is the output file the graders will use on assignment 1.(A1_Song_Output.txt) Your output must match this exactly or you may 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.

You may only use static methods and println statements.

Do not use print statements in your program. (or variables, parameters, loops, conditional statements, etc. Static methods, static method calls, and println statements only.)

You are to make use of static methods to avoid “simple” redundancy.  In particular, you are to make sure that you use only one println statement for each distinct line of the song.  For example, this line:

Perhaps she'll die.

appears several times in the output.  You are to have only one println statement in your program for producing this line. On the other hand, do not make methods with a single println statement and no other statements or method calls. Methods such as those take program decomposition to far. It is likely you will have methods with a single println statement in addition to other method calls or statements.

In other words this is bad:

public static void someMethod() {
    System.out.println("SOME STUF");
}

but this is okay and necessary in some cases:

public static void someMethod() {
    System.out.println("SOME STUF");
    callAnotherMethod();
}

There is a more complex redundancy that comes up with pairs of lines like these:

    There was an old woman who swallowed a horse,
  There was an old woman who swallowed a dog,

and like these:

    She swallowed the dog to eat the cat,
  She swallowed the cat to eat the bird,

It is not possible to avoid this redundancy using just methods and simple println statements, so you are not expected to do so. 

In other words the lines

    There was an old woman who swallowed a horse,
  There was an old woman who swallowed a dog,

are very similar. The only difference is the last word. It is okay to have one println for the line

    There was an old woman who swallowed a horse,

and another println for the line

    There was an old woman who swallowed a dog,

even though it seems redundant.

There is, however, a structural redundancy that you can eliminate with static methods.  The key question to ask yourself is whether or not you have repeated lines of code or statements that could be eliminated if you structured your static methods differently. These lines of codes could be calls to static methods instead of println statements.

Use static methods to capture the structure of the song.  You should, for example, have a different method for each of the eight verses of the song (verses are separated by blank lines in the output).

For this assignment, you shall limit yourself to the static methods and println statements. Do not use print statements or other Java language features.

Submission: Turn in your Song.java file with your code Canvas. Ensure you turn in the correct,If you do not turn in the correct file you will very likely end up wasting slip days or getting a 0 on the assignment. See this page for instructions to turn in a file for an assignment via Canvas.


Here is a shell of the program. Be sure to fill in the header. Replace <Name> with your name and fill in the required information.

Checklist: Did you remember to:

 Thanks to Stuart Reges for letting me use this assignment.

Back to the CS 312 homepage.