Share
Explore BrainMass

Writing a Program in Java for Outputing Lyrics

--------------------------------------------------------------------------
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();
}

}

Solution Summary

This solution includes a java program that will output lyrics.

$2.19