CS 305J Assignment, Problem Decomposition

Programming Assignment 2 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: Wednesday, September 3
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.

Write a Java program 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 horse,
    She died of course.

You must exactly reproduce the format of this output.

You are to make use of static methods to avoid the “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.  So you may have static methods with a single println in them.

The more complex redundancy has to do 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 six verses of the song (verses are separated by blank lines in the output).

You are not allowed to use more advanced features than what we have covered in class.  For this assignment, you should limit yourself to the Java features covered in chapter 1 of the text.

When finished turn in your Song.java program using the turnin program.

Files
File Responsibility
Song.java. This is the shell of the program. You and me
Checklist Did you remember to:
  • review the general assignment requirements?
  • work on the assignment individually?
  • fill in the header in your file Song.java program including replacing <NAME> with your name stating the work you are turning in is your own?
  • make your program structured and remove redundancy?
  • ensure your program does not suffer a compile error or runtime error?
  • ensure your program matches the output exactly?
  • turn in your Java source code in a file named Song.java to the proper account in the Microlab via the turnin program before 11 pm, Thursday, September 11?

  Thanks to Stuart Reges for letting me use this assignment.

Back to the CS 305j homepage.