Lab #1: Hello, World and Old McDonald

The goal of this lab is to write simple two simple classes using method calls to either System.out.print/println or those defined inside the classes. The two classes are HelloWorldPlus and class OldMcDonald and the score breakdown is 50-50 between the two.

Part (a) Hello World Plus

Write a class HelloWorldPlus that produces three lines of output as follows:

% java HelloWorldPlus
Hello, World!
My name is XXX.
My favorite word is “YYY”.

Note that the first line % java HelloWorldPlus is the command for executing the code.

Here XXX and YYY are placeholders and up to your choice. The main method of this class has three System.out.println statements, which are respectively responsible for the three lines of output. The word YYY should be printed with double quotation marks surrounding it. Note that to include a double quotation mark in the character sequence to be printed in the System.out.println (or print) you need to put a backslash in front of it. For example, to print

My favorite word is “endurance”.

with a newline, you need to use

System.out.println( "My favorite word is \"endurance\"." );

Part (b) Old McDonald

Write a class OldMcDonald that produces three verses of a children's rhyme Old McDonald Had a Farm as follows:

% java OldMcDonald
Old McDonald had a farm, E-I-E-I-O
And on this farm he had a cow, E-I-E-I-O
With a Moo, Moo here, and a Moo, Moo there
Here a Moo, there a Moo, everwhere a Moo, Moo
Old McDonald had a farm, E-I-E-I-O

Old McDonald had a farm, E-I-E-I-O
And on this farm he had a sheep, E-I-E-I-O
With a Baa, Baa here, and a Baa, Baa there
Here a Baa, there a Baa, everwhere a Baa, Baa
With a Moo, Moo here, and a Moo, Moo there
Here a Moo, there a Moo, everwhere a Moo, Moo
Old McDonald had a farm, E-I-E-I-O

Old McDonald had a farm, E-I-E-I-O
And on this 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, everwhere a Quack, Quack
With a Baa, Baa here, and a Baa, Baa there
Here a Baa, there a Baa, everwhere a Baa, Baa
With a Moo, Moo here, and a Moo, Moo there
Here a Moo, there a Moo, everwhere a Moo, Moo
Old McDonald had a farm, E-I-E-I-O

%

As you can see there shall be one empty line should be printed the last “... E-I-E-I-O” line of each verse.

We will assign a single or pair of lines a specific role as follows:

  1. The opening line of each verse, which also happens to be the last nonempty line of each verse.
  2. The empty line at the end of each verse.
  3. The “cow” line; i.e., the second line of Verse 1.
  4. The “sheep” line; i.e., the second line of Verse 2.
  5. The “duck” line; i.e., the second line of Verse 3.
  6. The two “Moo” lines, which appear in the 3rd and 4th lines of Verse 1, in the 5th and 6th lines of Verse 2, and then in the 7th and 8th lines of Verse 3.
  7. The two “Baa” lines, which appear in the 3rd and 4th lines of Verse 2 and in the 5th and 6th lines of Verse 3.
  8. The two “Quack” lines, which appear in the 3rd and 4th lines of Verse 3.

We treat these eight modules as individual methods:

  1. public static void oldMc()
  2. public static void empty()
  3. public static void hadACow()
  4. public static void hadASheep()
  5. public static void hadADuck()
  6. public static void withAMoo()
  7. public static void withABaa()
  8. public static void withAQuack()

  9. Using these modules we build three verses, again reprented as methods
  10. public static void verseOne()
  11. public static void verseTwo()
  12. public static void verseThree()

Then the main method shall be:

public static void main( String[] args ) {
  verseOne();
  verseTwo();
  verseThree();
}

Your task is to complete the template given to you. For the methods numbered 1 - 5, the body of the method is a single System.out.println statement; for the methods numbered 6 - 8, the body of the method is two System.out.println statements. For the methods numbered 9 - 11, they are built using method calls. The first verse (method number 9) is already coded (and some other methods, too), and you can use its construction to come up with how to write the other two verses.

Here is a template: OldMcDonald.java

Go back to the lecture page

Go back to the home page