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 output statements!
Design your program with a class named BeerSong whose constructor takes an integer parameter that is the number of bottles of beer 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 following two classes:
public class BeerSong {
private int numberofBottles;
BeerSong(int number) {
if (number < 0)
numberofBottles = 0;
else if (number > 99)
numberofBottles = 99;
else
numberofBottles = number;
}
public String getSingular(int i) {
switch (i) {
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
case 0: return "Zero";
default:return "Error";
}
}
public String getMultiplesOfTen(int i) {
switch (i) {
case 2: return "Twenty";
case 3: return "Thirty";
case 4: return "Forty";
case 5: return "Fifty";
case 6: return "Sixty";
case 7: return "Seventy";
case 8: return "Eighty";
case 9: return "Ninety";
default: return "Error";
}
}
public String getTeens(int i) {
switch (i) {
case 0: return "Ten";
case 1: return "Eleven";
case 2: return "Twelve";
case 3: return "Thirteen";
case 4: return "Fourteen";
case 5: return "Fifteen";
case 6: return "Sixteen";
case 7: return "Seventeen";
case 8: return "Eighteen";
case 9: return "Nineteen";
default:return "Error";
}
}
public String getNumberInEnglish(int i) {
int ones = i % 10;
int tens = i / 10;
if (i > 9) {
if (tens == 1)
return getTeens(ones);
else if (ones == 0)
return getMultiplesOfTen(i / 10);
else
return getMultiplesOfTen(tens) + '-' + getSingular(ones);
} else
return getSingular(ones);
}
public String printSong() {
String bottleString = "bottles";
//Output to store the song
String Output = "";
while (numberofBottles > 0) {
if (numberofBottles == 1)
bottleString = "bottle";
Output = Output + getNumberInEnglish(numberofBottles) + " " + bottleString
+ " of beer on the wall.\n";
Output = Output + getNumberInEnglish(numberofBottles) + " " + bottleString
+ " of beer on the wall,\n";
Output = Output + "Take one down, pass it around,\n";
numberofBottles = numberofBottles - 1;
if (numberofBottles == 0)
bottleString = "bottles";
else if (numberofBottles == 1)
bottleString = "bottle";
}
//When number of bottles is zero
Output = Output + "Zero bottles of beer on the wall.";
return Output;
}
}
//Class Sing with main method and uses BeerSong class:
import java.util.Scanner;
public class Sing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number:");
int numberOfBottles = sc.nextInt();
BeerSong song = new BeerSong(numberOfBottles);
System.out.print(song.printSong());
sc.close();
}
}
Output is given below:
Ninety-Nine bottles of beer on the wall.
Ninety-Nine bottles of beer on the wall,
Take one down, pass it around,
Ninety-Eight bottles of beer on the wall.
Ninety-Eight bottles of beer on the wall,
Take one down, pass it around,
Ninety-Seven bottles of beer on the wall.
Ninety-Seven bottles of beer on the wall,
Take one down, pass it around,
Ninety-Six bottles of beer on the wall.
Ninety-Six bottles of beer on the wall,
Take one down, pass it around,
....
....
....
Three bottles of beer on the wall.
Three bottles of beer on the wall,
Take one down, pass it around,
Two bottles of beer on the wall.
Two bottles of beer on the wall,
Take one down, pass it around,
One bottle of beer on the wall.
One bottle of beer on the wall,
Take one down, pass it around,
Zero bottles of beer on the wall.