SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Introduction To Java Programming
Exercise:
Programming Projects
Question:4 | ISBN:9780136091813 | Edition: 2

Question

Write a program that produces as output the words of “The Twelve Days of Christmas.” (Static methods simplify this task.) Here are the first two verses and the last verse of the song:

 

On the first day of Christmas,
my true love sent to me
a partridge in a pear tree.

On the second day of Christmas,
my true love sent to me
two turtle doves, and
a partridge in a pear tree.

...


On the twelfth day of Christmas,
my true love sent to me
Twelve drummers drumming,
eleven pipers piping,
ten lords a-leaping,
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

public class Ch01PP04
{
	public static void main(String[] args)
	{
		firstDay();
		secondDay();
		thirdDay();
		fourthDay();
		fifthDay();
		sixthDay();
		seventhDay();
		eighthDay();
		ninthDay();
		tenthDay();
		eleventhDay();
		twelfthDay();
	}

	public static void firstDay()
	{
		System.out.println("On the first day of Christmas,");
		System.out.println("my true love sent to me");
		day1();
	}

	public static void secondDay()
	{
		System.out.println("On the second day of Christmas,");
		System.out.println("my true love sent to me");
		day2();
	}

	public static void thirdDay()
	{
		System.out.println("On the third day of Christmas,");
		System.out.println("my true love sent to me");
		day3();
	}

	public static void fourthDay()
	{
		System.out.println("On the fourth day of Christmas,");
		System.out.println("my true love sent to me");
		day4();
	}

	public static void fifthDay()
	{
		System.out.println("On the fifth day of Christmas,");
		System.out.println("my true love sent to me");
		day5();
	}

	public static void sixthDay()
	{
		System.out.println("On the sixth day of Christmas,");
		System.out.println("my true love sent to me");
		day6();
	}

	public static void seventhDay()
	{
		System.out.println("On the seventh day of Christmas,");
		System.out.println("my true love sent to me");
		day7();
	}

	public static void eighthDay()
	{
		System.out.println("On the eighth day of Christmas,");
		System.out.println("my true love sent to me");
		day8();
	}

	public static void ninthDay()
	{
		System.out.println("On the ninth day of Christmas,");
		System.out.println("my true love sent to me");
		day9();
	}

	public static void tenthDay()
	{
		System.out.println("On the tenth day of Christmas,");
		System.out.println("my true love sent to me");
		day10();
	}

	public static void eleventhDay()
	{
		System.out.println("On the eleventh day of Christmas,");
		System.out.println("my true love sent to me");
		day11();
	}

	public static void twelfthDay()
	{
		System.out.println("On the twelfth day of Christmas,");
		System.out.println("my true love sent to me");
		day12();
	}

	public static void day1()
	{
		System.out.println("a partridge in a pear tree.");
		System.out.println();
	}

	public static void day2()
	{
		System.out.println("two turtle doves, and");
		day1();
	}

	public static void day3()
	{
		System.out.println("three French hens,");
		day2();
	}

	public static void day4()
	{
		System.out.println("four calling birds,");
		day3();
	}

	public static void day5()
	{
		System.out.println("five golden rings,");
		day4();
	}

	public static void day6()
	{
		System.out.println("six geese a-laying,");
		day5();
	}

	public static void day7()
	{
		System.out.println("seven swans a-swimming,");
		day6();
	}

	public static void day8()
	{
		System.out.println("eight maids a-milking,");
		day7();
	}

	public static void day9()
	{
		System.out.println("nine ladies dancing,");
		day8();
	}

	public static void day10()
	{
		System.out.println("ten lords a-leaping,");
		day9();
	}

	public static void day11()
	{
		System.out.println("eleven pipers piping,");
		day10();
	}

	public static void day12()
	{
		System.out.println("Twelve drummers drumming,");
		day11();
	}
}

Output:

On the first day of Christmas,
my true love sent to me
a partridge in a pear tree.

On the second day of Christmas,
my true love sent to me
two turtle doves, and
a partridge in a pear tree.

On the third day of Christmas,
my true love sent to me
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the fourth day of Christmas,
my true love sent to me
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the fifth day of Christmas,
my true love sent to me
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the sixth day of Christmas,
my true love sent to me
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the seventh day of Christmas,
my true love sent to me
seven swans a-swimming,
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the eighth day of Christmas,
my true love sent to me
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the ninth day of Christmas,
my true love sent to me
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the tenth day of Christmas,
my true love sent to me
ten lords a-leaping,
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the eleventh day of Christmas,
my true love sent to me
eleven pipers piping,
ten lords a-leaping,
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

On the twelfth day of Christmas,
my true love sent to me
Twelve drummers drumming,
eleven pipers piping,
ten lords a-leaping,
nine ladies dancing,
eight maids a-milking,
seven swans a-swimming,
six geese a-laying,
five golden rings,
four calling birds,
three French hens,
two turtle doves, and
a partridge in a pear tree.

 

0 0

Discussions

Post the discussion to improve the above solution.