CS 312 Assignment - Problem Decomposition - static methods and printlns

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: Tuesday, August 16
10 points, ~1% of total grade
Due: no later than 11 pm, Thursday, September 1
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.

Complete the items on the class start up page. If you get stuck post to the class discussion group on Piazza or go see the instructor or a TA in the lab.

If you are using BlueJ:

Complete a Java program (here is the program shell to start with. Replace <Name> with your name and fill in the required information.) 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. Please note, if you view the output above via a web browser, the browser will very likely remove the last newline character. This causes a minor difference between the expected output and your output. The last line of the song, "She died of course.", is printed out with a println statement, just like all the other lines of the song.

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.)

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).

Make use of static methods to avoid 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 STUFF");
}

but this is okay and necessary in some cases:

public static void someMethod() {
    System.out.println("SOME STUFF");
    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.

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.  If you do not turn in the correct file with the correct name 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. Your submission must be named Song.java.


Checklist: Did you remember to:

 Thanks to Stuart Reges for sharing this assignment with me.

Back to the CS 312 homepage.