Ch 3 Lab: Old MacDonald

Authors: Ralph Morelli & Bronzell Dinkins
Trinity College, Hartford, CT
For use with Chapter 3

Brief description

This lab is designed to be used in conjunction with Chapter 3 of Java, Java, Java, 3E. Its emphasizes the writing and use of methods and parameters to pass information to and from an object. In this case, the OldMacDonald class contains methods that generalize the singing of the familiar nursery rhyme. It uses System.out.println() for output and requires no command-line input. It illustrates the concept of procedural abstraction and promotes the use of the stepwise refinement coding approach.

Objectives

The objectives of this lab are:

Before Lab

Read Chapter 3 in Java, Java, Java, 3E and read through this document. Bring your copy of the textbook to lab.

Problem Statement

Write a Java application that sings at least three verses of "Old MacDonald Had a Farm." The output from your program should be something like:
Old MacDonald had a farm
E I E I O
And on his farm he had a duck
E I E I O
With a quack quack here
And a quack quack there
Here a quack, there a quack
Everywhere a quack quack.
Old MacDonald had a farm
E I E I O

Old MacDonald had a farm
E I E I O
And on his farm he had a pig
E I E I O
With a oink oink here
And a oink oink there
Here a oink, there a oink
Everywhere a oink oink.
Old MacDonald had a farm
E I E I O

Old MacDonald had a farm
E I E I O
And on his farm he had a dog
E I E I O
With a arf arf here
And a arf arf there
Here a arf, there a arf
Everywhere a arf arf.
Old MacDonald had a farm
E I E I O

Problem Decomposition and Design

The OldMacDonald class will sing verses of the Old MacDonald nursery rhyme. In order to do this it does not have to store any particular data. Therefore it won't have any instance variables. On the other hand, it should have the following methods, which sing various portions of the verse:

Implementation

Step 0. Create the lab2 Project

Close all applications that may be running on your computer. Run the Metrowerks Codewarrior IDE. Create a new project named lab2 and configure it as a Java application and save it on your desktop. Rename the TrivialApplication class to OldMacDonald and make the corresponding change to the name of the Java source file and to the project's target.

Step 1. Edit and Run the First Version of OldMacDonald

This version should consist of a basic class definition with a minimal main() method. The main method should create a local instance of the OldMacDonald object and then simply print the string "OldMacDonald" to verify that the application is correctly formatted and that it runs -- e.g.,
   OldMacDonald mac = new OldMacDonald();  // Create an instance
   System.out.println("Old MacDonald");    // Print a message

Step 2. Edit and Run the Second Version of OldMacDonald

This version should contain the oldMac() and eieio()methods, described above. It should invoke the methods in main() to verify that they are working properly. For example, your main() should now contain:
  System.out.println( mac.oldMac() ); // Prints "Old MacDonald Had A Farm"
  System.out.println( mac.eieio() );  // Prints "E I E I O"

Step 3. Edit and Run the Third Version of OldMacDonald

This version should contain the hadAnX(String) method, described above. It should invoke the method in main() to verify that it is working properly.

Step 4. Edit and Run the Fourth Version of OldMacDonald

This version should contain the withA(String) method, described above. It should invoke the method in main() to verify that it is working properly. Once this method is added, the program should be correctly displaying an entire verse. NOTE: If you want the output to contain line breaks as shown in the above example, use the escape sequence, "\n", wherever appropriate in the method.

Step 5. Edit and Run the Fifth Version of OldMacDonald

This version should incorporate the verse(String,String) method into the application. This will require that you completely revise main(), which should now consist of the following kinds of statements for each verse:
  System.out.println( mac.verse("duck", "quack" ) );

Step 6. Edit and Run the Sixth Version of OldMacDonald

This version should print at least three verses of the rhyme (ducks,pigs,dogs), by repeatedly invoking the verse() method with different arguments. It should be clear from this exercise that the procedural abstraction afforded by using methods provides a substantial advantage when it comes to incorporating new verses into the song.

Step 7. (Optional Challenge)

Add one or more verses (cows).

Step 8. Lab Checkout

Have your work checked by the lab instructor before you leave the lab. Hand in a copy of your source code for this lab.

You're done. Great work!