Purchase Solution

Writing a Program in Java for Outputing Lyrics

Not what you're looking for?

Ask Custom Question

--------------------------------------------------------------------------
Instructions are as follows.

Write a program that outputs the lyrics for "Ninety Nine Bottles of Beer on the Wall". Your program should print the number of bottles in English, not as a number.

For example:

Ninety nine bottles of beer on the wall,
Ninety nine bottles of beer,
Take one down, pass it around,
Ninety eight bottles of beer on the wall.
...
One bottle of beer on the wall,
One bottle of beer,
Take one down, pass it around,
Zero bottles of beer on the wall.
Your program should not use ninety nine different output statements!
Design your program with a class named BeerSong whose constructor takes an integer parameter that is the number of bottles of beer that are initially on the wall. If the parameter is less than zero, set the number of bottles to zero. Similarly, if the parameter is greater than 99, set the number of beer bottles to 99. Then make a public method called printSong that outputs all stanzas from the number of bottles of beer down to zero. Add any additional private methods you find helpful.
Use the / and % operators to extract the tens digit and the ones digit from the number to be printed. Note that only values between 0 and 99 (inclusive) will be passed to printNumInEnglish.
You may specifically need to test for certain values, such as 0 and 10 through 19
The constructor that you implement should do appropriate range checking for the number of bottles. Specifically, it should check for values that are less than 0 and greater than 99. (Review the problem description for an explanation.)

/**
* This program outputs the 99 bottles of beer on the wall song.
* A simple loop calls a function that outputs each stanza
* for a particular number of bottles of beer.
* <P>
* We use another function that takes a integer from 0-99 and
* outputs that integer as a word. It uses / and % to extract the
* tens digit and the ones digit so we don't need 100 different
* if-then-else statements.
*/
public class BeerSong {
/**
* Number of bottles that we start out with
*/
private int bottles = 0;

// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------

// --------------------------------
// --------- END USER CODE --------
// --------------------------------
/**
* Outputs an entire stanza for n bottles.
*/
public void printStanza(int n) {
// output n in English
printNumInEnglish(n);

// account for "one bottle" vs. many "bottles"
if (n == 1) {
System.out.println("bottle of beer on the wall, ");
}
else {
System.out.println("bottles of beer on the wall, ");
}

printNumInEnglish(n);
if (n == 1) {
System.out.println("bottle of beer, ");
}
else {
System.out.println("bottles of beer, ");
}
System.out.println("Take one down, pass it around,");
n--;

printNumInEnglish(n);
if (n == 1) {
System.out.println("bottle of beer on the wall.");
}
else {
System.out.println("bottles of beer on the wall.");
}
System.out.println();
}

public void printSong() {
// Loop from 99 down to 0
for (int num = bottles; num > 0; num--) {
printStanza(num);
}
}

public static void main(String[] args) {
BeerSong bs = new BeerSong(23);
bs.printSong();
}

}

Purchase this Solution

Solution Summary

This solution includes a java program that will output lyrics.

Purchase this Solution


Free BrainMass Quizzes
C++ Operators

This quiz tests a student's knowledge about C++ operators.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.