Laboratory 1: Nursery Rhyme

Objectives

The objectives of this lab are:

Before Lab

Read section 1.6 of Java, Java, Java, 3E and study the OldMacDonald program in Figure 1.16 and self-study exercise 1.3. Bring your textbook to lab.

Problem Statement

This is Exercise 1.19. Modify the OldMacDonald class to "sing" either "Mary Had a Little Lamb" or your favorite nursery rhyme.

The solution to this problem can be modeled after the program in Figure 1.16. Modeling a solution to a problem after a problem you already know how to solve is an important problem solving technique.

You will need to use the System.out object and the println() method in your program.

Problem Decomposition

This problem can be divided into one class. Choose your own name for the class. Remember that if you name your class NurseryRhyme then the file containing it must be named NurseryRhyme.java.

Design

Like the OldMacDonald class, your class has no instance variables or instance methods. It will need a main() method. Draw a UML diagram of your class and show it to the instructor or TA.

Implementation

Setting Up: Open up a pen a terminal window by clicking the Terminal Program icon, which looks like a monitor. Create a subdirectory in your cpsc115 named lab1. You will save all the work for this lab in that directory. Open up the KWrite text editor in a second window. This is where you will code your program.

Stepwise Refinement: Stepwise refinement is an important approach to programming and problem solving. The idea is to build your program is small stages, compiling and testing after every stage. This will help you isolate syntax and semantic errors that get introduced into the code.

Here are some suggested steps for this program.

  1. Code an initial comment block in your file that looks something like:
      /*******************************************************
       * File: NurseryRhyme.java
       * Author: Your name
       * Course and lab section: CPSC 115 Tuesday lab
       * Description: A program to print a nursery rhyme.
       *******************************************************/
    

  2. Code the class header and its opening and closing braces, leaving the body of the class definition empty. Then save your program, being sure to give it an appropriate file name.

  3. Compile and run your program, using the following commands from the command-line in your terminal window:
       $  javac NurseryRhyme.java
       $  java NurseryRhyme
    
    If everything is correct, no error messages should be reported but the program won't display any output yet.

  4. Code a stub version of the main() method. That is, code the header and opening and closing braces for the main() method, leaving the body of the method blank for now. This method definition should go inside the body of your class definition. Save your program. Compile and run it until no error messages occur. Again, the program won't display any output yet.

  5. Now add code to the main() method that prints the lines of your nursery rhyme. Don't code more than two lines at a time, then save your file, compile and run the program. This time your program should display the verses of the nursery rhyme as its output. Repeat this step until you have successfully coded the entire nursery rhyme and your program works perfectly.

    Optional

    Lab Checkout

    Have your work checked by the lab instructor before you leave the lab. Print out and hand in a copy of your program. Use the Trinity College Blackboard CourseInfo Digital Drop Box to submit your file: Temperature.java. NOTE: Always select the LOGOUT option to exit CourseInfo. Closing the browser window does Not terminate your CourseInfo session.

    You're done. Great work!