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

Question

Write a program that produces as output the words of “The House That Jack Built.” Use methods for each verse
and for repeated text. Here are lyrics to use:


This is the house that Jack built.


This is the malt
That lay in the house that Jack built.


This is the rat,
That ate the malt
That lay in the house that Jack built.


This is the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.


This is the dog,
That worried the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.


This is the cow with the crumpled horn,
That tossed the dog,
That worried the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.

This is the maiden all forlorn
That milked the cow with the crumpled horn,
That tossed the dog,
That worried the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

public class Ch01PP05
{
	public static void main(String[] args)
	{
		house();
		malt();
		rat();
		cat();
		dog();
		cow();
		maiden();
	}
	
	public static void house()
	{
		System.out.println("This is the house that Jack built.");
		System.out.println();
	}
	
	public static void malt()
	{
		System.out.println("This is the malt");
		verse1();
	}
	
	public static void rat()
	{
		System.out.println("This is the rat,");
		verse2();
	}
	
	public static void cat()
	{
		System.out.println("This is the cat,");
		verse3();
	}
	
	public static void dog()
	{
		System.out.println("This is the dog,");
		verse4();
	}
	
	public static void cow()
	{
		System.out.println("This is the cow with the crumpled horn,");
		verse5();
	}
	
	public static void maiden()
	{
		System.out.println("This is the maiden all forlorn");
		verse6();
	}
	
	public static void verse1()
	{
		System.out.println("That lay in the house that Jack built.");
		System.out.println();
	}
	
	public static void verse2()
	{
		System.out.println("That ate the malt");
		verse1();
	}
	
	public static void verse3()
	{
		System.out.println("That killed the rat,");
		verse2();
	}
	
	public static void verse4()
	{
		System.out.println("That worried the cat,");
		verse3();
	}
	
	public static void verse5()
	{
		System.out.println("That tossed the dog,");
		verse4();
	}
	
	public static void verse6()
	{
		System.out.println("That milked the cow with the crumpled horn,");
		verse5();
	}
}

Output:

This is the house that Jack built.

This is the malt
That lay in the house that Jack built.

This is the rat,
That ate the malt
That lay in the house that Jack built.

This is the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.

This is the dog,
That worried the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.

This is the cow with the crumpled horn,
That tossed the dog,
That worried the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.

This is the maiden all forlorn
That milked the cow with the crumpled horn,
That tossed the dog,
That worried the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.

 

0 0

Discussions

Post the discussion to improve the above solution.