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:6 | ISBN:9780136091813 | Edition: 2

Question

Write a program that produces as output the words of “Bought Me a Cat.” Use methods for each verse and for repeated text. Here are the song’s complete lyrics:
 

Bought me a cat and the cat pleased me,
I fed my cat under yonder tree.
Cat goes fiddle-i-fee.

Bought me a hen and the hen pleased me,
I fed my hen under yonder tree.
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

Bought me a duck and the duck pleased me,
I fed my duck under yonder tree.
Duck goes quack, quack,
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

Bought me a hen and the hen pleased me,
I fed my hen under yonder tree.
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

Bought me a duck and the duck pleased me,
I fed my duck under yonder tree.
Duck goes quack, quack,
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

Bought me a goose and the goose pleased me,
I fed my goose under yonder tree.
Goose goes hissy, hissy,
Duck goes quack, quack,
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

Bought me a sheep and the sheep pleased me,
I fed my sheep under yonder tree.
Sheep goes baa, baa,
Goose goes hissy, hissy,
Duck goes quack, quack,
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

public class Ch01PP06
{
	public static void main(String[] args)
	{
		boughtMeCat();
		boughtMeHen();
		boughtMeDuck();
		boughtMeGoose();
		boughtMeSheep();
	}
	
	public static void boughtMeCat()
	{
		System.out.println("Bought me a cat and the cat pleased me,");
		System.out.println("I fed my cat under yonder tree.");
		cat();
	}
	
	public static void cat()
	{
		System.out.println("Cat goes fiddle-i-fee.");
		System.out.println();
	}
	
	public static void boughtMeHen()
	{
		System.out.println("Bought me a hen and the hen pleased me,");
		System.out.println("I fed my hen under yonder tree.");
		hen();
	}
	
	public static void hen()
	{
		System.out.println("Hen goes chimmy-chuck, chimmy-chuck,");
		cat();
	}
	
	public static void boughtMeDuck()
	{
		System.out.println("Bought me a duck and the duck pleased me,");
		System.out.println("I fed my duck under yonder tree.");
		duck();
	}
	
	public static void duck()
	{
		System.out.println("Duck goes quack, quack,");
		hen();
	}
	
	public static void boughtMeGoose()
	{
		System.out.println("Bought me a goose and the goose pleased me,");
		System.out.println("I fed my goose under yonder tree.");
		goose();
	}
	
	public static void goose()
	{
		System.out.println("Goose goes hissy, hissy,");
		duck();
	}
	
	public static void boughtMeSheep()
	{
		System.out.println("Bought me a sheep and the sheep pleased me,");
		System.out.println("I fed my sheep under yonder tree.");
		sheep();
	}
	
	public static void sheep()
	{
		System.out.println("Sheep goes baa, baa,");
		goose();
	}
}

Output:

Bought me a cat and the cat pleased me,
I fed my cat under yonder tree.
Cat goes fiddle-i-fee.

Bought me a hen and the hen pleased me,
I fed my hen under yonder tree.
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

Bought me a duck and the duck pleased me,
I fed my duck under yonder tree.
Duck goes quack, quack,
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

Bought me a goose and the goose pleased me,
I fed my goose under yonder tree.
Goose goes hissy, hissy,
Duck goes quack, quack,
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

Bought me a sheep and the sheep pleased me,
I fed my sheep under yonder tree.
Sheep goes baa, baa,
Goose goes hissy, hissy,
Duck goes quack, quack,
Hen goes chimmy-chuck, chimmy-chuck,
Cat goes fiddle-i-fee.

 

0 0

Discussions

Post the discussion to improve the above solution.