Session 5

OldMacDonaldv1.java

public class OldMacDonaldv1 {
    public static void main(String args[]) {
        System.out.println("Old MacDonald had a farm.");
        System.out.println("E-I-E-I-O");
        System.out.println("And on this farm, they had a cow.");
        System.out.println("E-I-E-I-O");
        System.out.println("With a moo moo here and a moo moo there");
        System.out.println("Here a moo, there a moo, everywhere a moo moo");
        System.out.println("Old MacDonald had a farm");
        System.out.println("E-I-E-I-O");
    }
}

OldMacDonaldv2.java

public class OldMacDonaldv2 {
    public static void intro() {
        System.out.println("Old MacDonald had a farm");
        chorus();
    }

    public static void chorus() {
        System.out.println("E-I-E-I-O");
    }

    public static void main(String[] args) {
        intro();
        System.out.println("And on that farm they had a cow.");
        chorus();
        System.out.println("With a moo moo here and a moo moo there");
        System.out.println("Here a moo, there a moo, everywhere a moo moo");

        // TODO:
        // 1. Call the method intro()

        // 2. Print out the line "And on that farm..."
        //    with a duck or another animal

        // 3. Call the method chorus

        // 4. Print out the lines with the appropriate animal sounds

        // 5. Call the method intro again

    }
}

OldMacDonaldv3.java

public class OldMacDonaldv3 {
    public static void main(String args[]) {
        System.out.println("Old MacDonald had a farm.");
        System.out.println("E-I-E-I-O");
        System.out.println("And on this farm, they had a cow.");
        System.out.println("E-I-E-I-O");
        System.out.println("With a moo moo here and a moo moo there");
        System.out.println("Here a moo, there a moo, everywhere a moo moo");
        System.out.println("Old MacDonald had a farm");
        System.out.println("E-I-E-I-O");
        System.out.println("And on this farm, they had a duck.");
        System.out.println("E-I-E-I-O");
        System.out.println("With a quack quack here and a quack quack there");
        System.out.println("Here a quack, there a quack, everywhere a quack quack");
        System.out.println("Old MacDonald had a farm");
        System.out.println("E-I-E-I-O");
    }
}

OldMacDonaldv4.java

public class OldMacDonaldv4 {
    public static void main(String args[]) {
        intro();
        verse("cow", "moo");
        intro();
        verse("duck", "quack");
        intro();
    }

    /**
     * The verse method prints out a verse for any given animal and sound.
     *
     * @param animal the name of the animal
     * @param sound  the sound the animal makes
     */
    public static void verse(String animal, String sound) {
        System.out.println("And on this farm, they had a " + animal);
        chorus();
        System.out.println("With a " + sound + " " + sound
                + " here and a " + sound + " " + sound + " there");
        System.out.println("Here a " + sound
                + ", there a " + sound
                + ", everywhere a " + sound + " " + sound);
    }

    /**
     * The intro method prints the introductory line of the song
     * and then calls the chorus method.
     */
    public static void intro() {
        System.out.println("Old MacDonald had a farm");
        chorus();
    }

    /**
     * The chorus method prints the repeating "E-I-E-I-O" line.
     */
    public static void chorus() {
        System.out.println("E-I-E-I-O");
    }
}

AntsSong.java

public class AntsSong
{
    public static void chorus(String num)
    {
        System.out.println("The ants go marching " + num
                         + " by " + num + " hurrah, hurrah");
        System.out.println("The ants go marching " + num
                         + " by " + num + " hurrah, hurrah");
    }

    public static void verse(String num, String action)
    {
        System.out.println("The ants go marching " + num + " by " + num);
        System.out.println("The little one stops to " + action);
        System.out.println("And they all go marching down to the ground");
        System.out.println("To get out of the rain, BOOM! BOOM! BOOM! BOOM!\n");
    }

    public static void main(String args[])
    {
        // Call the chorus and verse methods
        // with the correct arguments
        // to print out all three verses above.



    }
}

Last updated

Was this helpful?