Question

Java hw Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the...

Java hw

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// BeerSong.java

public class BeerSong {

              // number of bottles

              private int bottles;

              // lyrics of the Song which repeats again and again. the number of bottles

              // is marked in format specifier, so that it can be given using

              // String.format()

              private final String lyricsText = "%s bottles of beer on the wall, %s bottles of beer,"

                                         + " Take one down, pass it around, %s bottles of beer on the wall\n";

              // constructor taking number of bottles

              public BeerSong(int bottles) {

                           // validating number of bottles

                           if (bottles < 0) {

                                         bottles = 0;

                           } else if (bottles > 99) {

                                         bottles = 99;

                           }

                           // assigning validated value

                           this.bottles = bottles;

              }

              // method to print the song

              public void PrintSong() {

                           int num = bottles;

                           // looping until num becomes 0

                           while (num > 0) {

                                         // formatting lyrics text, passing num and num-1 after converted to

                                         // text

                                         String text = String.format(lyricsText, numberToText(num),

                                                                    numberToText(num), numberToText(num - 1));

                                         // replacing One bottles to One bottle if exists

                                         if (text.contains("One bottles")) {

                                                       text = text.replaceAll("One bottles", "One bottle");

                                         }

                                         // displaying lyrics

                                         System.out.println(text);

                                         num--;

                           }

              }

              // helper method to convert a number between 0 and 99 to text and return it

              private String numberToText(int num) {

                           // defining an array containing text equivalent of all numbers between 0

                           // and 20

                           String numText[] = { "Zero", "One", "Two", "Three", "Four", "Five",

                                                       "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",

                                                       "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",

                                                       "Eighteen", "Nineteen", "Twenty" };

                           // defining an array containing text equivalent of all tenth numbers

                           // after 10

                           String tens[] = { "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",

                                                       "Seventy", "Eighty", "Ninety" };

                           //checking if number is less than or equal to 20

                           if (num <= 20) {

                                         //simply returning the text at num index of numText

                                         return numText[num];

                           }

                           //finding last digit

                           int lastDigit = num % 10;

                           //removing last digit

                           num = num / 10;

                           String last = "";

                           //appending text value of last digit if digit is not zero

                           if (lastDigit != 0) {

                                         last = numText[lastDigit].toLowerCase();

                                         //returning corresponding tens value with text of last digit

                                         return tens[num - 2] + "-" + last;

                           }

                           //simply returning corresponding tens value

                           return tens[num - 2];

              }

              public static void main(String[] args) {

                           //creating a BeerSong of 99 bottles

                           BeerSong song = new BeerSong(99);

                           //printing song

                           song.PrintSong();

              }

}

/*OUTPUT*/

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

Ninety-eight bottles of beer on the wall, Ninety-eight bottles of beer, Take one down, pass it around, Ninety-seven bottles of beer on the wall

Ninety-seven bottles of beer on the wall, Ninety-seven bottles of beer, Take one down, pass it around, Ninety-six bottles of beer on the wall

Ninety-six bottles of beer on the wall, Ninety-six bottles of beer, Take one down, pass it around, Ninety-five bottles of beer on the wall

Ninety-five bottles of beer on the wall, Ninety-five bottles of beer, Take one down, pass it around, Ninety-four bottles of beer on the wall

Ninety-four bottles of beer on the wall, Ninety-four bottles of beer, Take one down, pass it around, Ninety-three bottles of beer on the wall

Ninety-three bottles of beer on the wall, Ninety-three bottles of beer, Take one down, pass it around, Ninety-two bottles of beer on the wall

Ninety-two bottles of beer on the wall, Ninety-two bottles of beer, Take one down, pass it around, Ninety-one bottles of beer on the wall

Ninety-one bottles of beer on the wall, Ninety-one bottles of beer, Take one down, pass it around, Ninety bottles of beer on the wall

Ninety bottles of beer on the wall, Ninety bottles of beer, Take one down, pass it around, Eighty-nine bottles of beer on the wall

Eighty-nine bottles of beer on the wall, Eighty-nine bottles of beer, Take one down, pass it around, Eighty-eight bottles of beer on the wall

Eighty-eight bottles of beer on the wall, Eighty-eight bottles of beer, Take one down, pass it around, Eighty-seven bottles of beer on the wall

Eighty-seven bottles of beer on the wall, Eighty-seven bottles of beer, Take one down, pass it around, Eighty-six bottles of beer on the wall

Eighty-six bottles of beer on the wall, Eighty-six bottles of beer, Take one down, pass it around, Eighty-five bottles of beer on the wall

Eighty-five bottles of beer on the wall, Eighty-five bottles of beer, Take one down, pass it around, Eighty-four bottles of beer on the wall

Eighty-four bottles of beer on the wall, Eighty-four bottles of beer, Take one down, pass it around, Eighty-three bottles of beer on the wall

Eighty-three bottles of beer on the wall, Eighty-three bottles of beer, Take one down, pass it around, Eighty-two bottles of beer on the wall

Eighty-two bottles of beer on the wall, Eighty-two bottles of beer, Take one down, pass it around, Eighty-one bottles of beer on the wall

Eighty-one bottles of beer on the wall, Eighty-one bottles of beer, Take one down, pass it around, Eighty bottles of beer on the wall

Eighty bottles of beer on the wall, Eighty bottles of beer, Take one down, pass it around, Seventy-nine bottles of beer on the wall

Seventy-nine bottles of beer on the wall, Seventy-nine bottles of beer, Take one down, pass it around, Seventy-eight bottles of beer on the wall

Seventy-eight bottles of beer on the wall, Seventy-eight bottles of beer, Take one down, pass it around, Seventy-seven bottles of beer on the wall

Seventy-seven bottles of beer on the wall, Seventy-seven bottles of beer, Take one down, pass it around, Seventy-six bottles of beer on the wall

Seventy-six bottles of beer on the wall, Seventy-six bottles of beer, Take one down, pass it around, Seventy-five bottles of beer on the wall

Seventy-five bottles of beer on the wall, Seventy-five bottles of beer, Take one down, pass it around, Seventy-four bottles of beer on the wall

Seventy-four bottles of beer on the wall, Seventy-four bottles of beer, Take one down, pass it around, Seventy-three bottles of beer on the wall

Seventy-three bottles of beer on the wall, Seventy-three bottles of beer, Take one down, pass it around, Seventy-two bottles of beer on the wall

Seventy-two bottles of beer on the wall, Seventy-two bottles of beer, Take one down, pass it around, Seventy-one bottles of beer on the wall

Seventy-one bottles of beer on the wall, Seventy-one bottles of beer, Take one down, pass it around, Seventy bottles of beer on the wall

Seventy bottles of beer on the wall, Seventy bottles of beer, Take one down, pass it around, Sixty-nine bottles of beer on the wall

Sixty-nine bottles of beer on the wall, Sixty-nine bottles of beer, Take one down, pass it around, Sixty-eight bottles of beer on the wall

Sixty-eight bottles of beer on the wall, Sixty-eight bottles of beer, Take one down, pass it around, Sixty-seven bottles of beer on the wall

Sixty-seven bottles of beer on the wall, Sixty-seven bottles of beer, Take one down, pass it around, Sixty-six bottles of beer on the wall

Sixty-six bottles of beer on the wall, Sixty-six bottles of beer, Take one down, pass it around, Sixty-five bottles of beer on the wall

Sixty-five bottles of beer on the wall, Sixty-five bottles of beer, Take one down, pass it around, Sixty-four bottles of beer on the wall

Sixty-four bottles of beer on the wall, Sixty-four bottles of beer, Take one down, pass it around, Sixty-three bottles of beer on the wall

Sixty-three bottles of beer on the wall, Sixty-three bottles of beer, Take one down, pass it around, Sixty-two bottles of beer on the wall

Sixty-two bottles of beer on the wall, Sixty-two bottles of beer, Take one down, pass it around, Sixty-one bottles of beer on the wall

Sixty-one bottles of beer on the wall, Sixty-one bottles of beer, Take one down, pass it around, Sixty bottles of beer on the wall

Sixty bottles of beer on the wall, Sixty bottles of beer, Take one down, pass it around, Fifty-nine bottles of beer on the wall

Fifty-nine bottles of beer on the wall, Fifty-nine bottles of beer, Take one down, pass it around, Fifty-eight bottles of beer on the wall

Fifty-eight bottles of beer on the wall, Fifty-eight bottles of beer, Take one down, pass it around, Fifty-seven bottles of beer on the wall

Fifty-seven bottles of beer on the wall, Fifty-seven bottles of beer, Take one down, pass it around, Fifty-six bottles of beer on the wall

Fifty-six bottles of beer on the wall, Fifty-six bottles of beer, Take one down, pass it around, Fifty-five bottles of beer on the wall

Fifty-five bottles of beer on the wall, Fifty-five bottles of beer, Take one down, pass it around, Fifty-four bottles of beer on the wall

Fifty-four bottles of beer on the wall, Fifty-four bottles of beer, Take one down, pass it around, Fifty-three bottles of beer on the wall

Fifty-three bottles of beer on the wall, Fifty-three bottles of beer, Take one down, pass it around, Fifty-two bottles of beer on the wall

Fifty-two bottles of beer on the wall, Fifty-two bottles of beer, Take one down, pass it around, Fifty-one bottles of beer on the wall

Fifty-one bottles of beer on the wall, Fifty-one bottles of beer, Take one down, pass it around, Fifty bottles of beer on the wall

Fifty bottles of beer on the wall, Fifty bottles of beer, Take one down, pass it around, Fourty-nine bottles of beer on the wall

Fourty-nine bottles of beer on the wall, Fourty-nine bottles of beer, Take one down, pass it around, Fourty-eight bottles of beer on the wall

Fourty-eight bottles of beer on the wall, Fourty-eight bottles of beer, Take one down, pass it around, Fourty-seven bottles of beer on the wall

Fourty-seven bottles of beer on the wall, Fourty-seven bottles of beer, Take one down, pass it around, Fourty-six bottles of beer on the wall

Fourty-six bottles of beer on the wall, Fourty-six bottles of beer, Take one down, pass it around, Fourty-five bottles of beer on the wall

Fourty-five bottles of beer on the wall, Fourty-five bottles of beer, Take one down, pass it around, Fourty-four bottles of beer on the wall

Fourty-four bottles of beer on the wall, Fourty-four bottles of beer, Take one down, pass it around, Fourty-three bottles of beer on the wall

Fourty-three bottles of beer on the wall, Fourty-three bottles of beer, Take one down, pass it around, Fourty-two bottles of beer on the wall

Fourty-two bottles of beer on the wall, Fourty-two bottles of beer, Take one down, pass it around, Fourty-one bottles of beer on the wall

Fourty-one bottles of beer on the wall, Fourty-one bottles of beer, Take one down, pass it around, Fourty bottles of beer on the wall

Fourty bottles of beer on the wall, Fourty bottles of beer, Take one down, pass it around, Thirty-nine bottles of beer on the wall

Thirty-nine bottles of beer on the wall, Thirty-nine bottles of beer, Take one down, pass it around, Thirty-eight bottles of beer on the wall

Thirty-eight bottles of beer on the wall, Thirty-eight bottles of beer, Take one down, pass it around, Thirty-seven bottles of beer on the wall

Thirty-seven bottles of beer on the wall, Thirty-seven bottles of beer, Take one down, pass it around, Thirty-six bottles of beer on the wall

Thirty-six bottles of beer on the wall, Thirty-six bottles of beer, Take one down, pass it around, Thirty-five bottles of beer on the wall

Thirty-five bottles of beer on the wall, Thirty-five bottles of beer, Take one down, pass it around, Thirty-four bottles of beer on the wall

Thirty-four bottles of beer on the wall, Thirty-four bottles of beer, Take one down, pass it around, Thirty-three bottles of beer on the wall

Thirty-three bottles of beer on the wall, Thirty-three bottles of beer, Take one down, pass it around, Thirty-two bottles of beer on the wall

Thirty-two bottles of beer on the wall, Thirty-two bottles of beer, Take one down, pass it around, Thirty-one bottles of beer on the wall

Thirty-one bottles of beer on the wall, Thirty-one bottles of beer, Take one down, pass it around, Thirty bottles of beer on the wall

Thirty bottles of beer on the wall, Thirty bottles of beer, Take one down, pass it around, Twenty-nine bottles of beer on the wall

Twenty-nine bottles of beer on the wall, Twenty-nine bottles of beer, Take one down, pass it around, Twenty-eight bottles of beer on the wall

Twenty-eight bottles of beer on the wall, Twenty-eight bottles of beer, Take one down, pass it around, Twenty-seven bottles of beer on the wall

Twenty-seven bottles of beer on the wall, Twenty-seven bottles of beer, Take one down, pass it around, Twenty-six bottles of beer on the wall

Twenty-six bottles of beer on the wall, Twenty-six bottles of beer, Take one down, pass it around, Twenty-five bottles of beer on the wall

Twenty-five bottles of beer on the wall, Twenty-five bottles of beer, Take one down, pass it around, Twenty-four bottles of beer on the wall

Twenty-four bottles of beer on the wall, Twenty-four bottles of beer, Take one down, pass it around, Twenty-three bottles of beer on the wall

Twenty-three bottles of beer on the wall, Twenty-three bottles of beer, Take one down, pass it around, Twenty-two bottles of beer on the wall

Twenty-two bottles of beer on the wall, Twenty-two bottles of beer, Take one down, pass it around, Twenty-one bottles of beer on the wall

Twenty-one bottles of beer on the wall, Twenty-one bottles of beer, Take one down, pass it around, Twenty bottles of beer on the wall

Twenty bottles of beer on the wall, Twenty bottles of beer, Take one down, pass it around, Nineteen bottles of beer on the wall

Nineteen bottles of beer on the wall, Nineteen bottles of beer, Take one down, pass it around, Eighteen bottles of beer on the wall

Eighteen bottles of beer on the wall, Eighteen bottles of beer, Take one down, pass it around, Seventeen bottles of beer on the wall

Seventeen bottles of beer on the wall, Seventeen bottles of beer, Take one down, pass it around, Sixteen bottles of beer on the wall

Sixteen bottles of beer on the wall, Sixteen bottles of beer, Take one down, pass it around, Fifteen bottles of beer on the wall

Fifteen bottles of beer on the wall, Fifteen bottles of beer, Take one down, pass it around, Fourteen bottles of beer on the wall

Fourteen bottles of beer on the wall, Fourteen bottles of beer, Take one down, pass it around, Thirteen bottles of beer on the wall

Thirteen bottles of beer on the wall, Thirteen bottles of beer, Take one down, pass it around, Twelve bottles of beer on the wall

Twelve bottles of beer on the wall, Twelve bottles of beer, Take one down, pass it around, Eleven bottles of beer on the wall

Eleven bottles of beer on the wall, Eleven bottles of beer, Take one down, pass it around, Ten bottles of beer on the wall

Ten bottles of beer on the wall, Ten bottles of beer, Take one down, pass it around, Nine bottles of beer on the wall

Nine bottles of beer on the wall, Nine bottles of beer, Take one down, pass it around, Eight bottles of beer on the wall

Eight bottles of beer on the wall, Eight bottles of beer, Take one down, pass it around, Seven bottles of beer on the wall

Seven bottles of beer on the wall, Seven bottles of beer, Take one down, pass it around, Six bottles of beer on the wall

Six bottles of beer on the wall, Six bottles of beer, Take one down, pass it around, Five bottles of beer on the wall

Five bottles of beer on the wall, Five bottles of beer, Take one down, pass it around, Four bottles of beer on the wall

Four bottles of beer on the wall, Four bottles of beer, Take one down, pass it around, Three bottles of beer on the wall

Three bottles of beer on the wall, Three bottles of beer, Take one down, pass it around, Two bottles of beer on the wall

Two bottles of beer on the wall, Two bottles of beer, Take one down, pass it around, One bottle 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

Add a comment
Answer #2

Here is a Python implementation of the BeerSong class that prints out the lyrics of "Ninety-nine Bottles of Beer on the Wall" song:

class BeerSong:

    def __init__(self, num_bottles):

        self.num_bottles = max(0, min(num_bottles, 99))

        

    def _bottle_str(self, num):

        if num == 0:

            return "no more bottles"

        elif num == 1:

            return "1 bottle"

        else:

            ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

            tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]

            if num < 10:

                return ones[num] + " bottles"

            elif num < 20:

                return ones[num % 10] + "teen bottles"

            else:

                return tens[num // 10] + "-" + ones[num % 10] + " bottles"

        

    def PrintSong(self):

        for i in range(self.num_bottles, 0, -1):

            print(self._bottle_str(i).capitalize() + " of beer on the wall, " +

                  self._bottle_str(i) + " of beer.")

            if i == 1:

                print("Take it down, pass it around, " +

                      "no more bottles of beer on the wall.")

            else:

                print("Take one down, pass it around, " +

                      self._bottle_str(i - 1) + " of beer on the wall.")

The _bottle_str method takes a number and returns the corresponding string for that number of bottles. The PrintSong method uses a for loop to iterate over the number of bottles and print each stanza of the song, using the _bottle_str method to convert the numbers to English strings. The max and min functions are used in the constructor to ensure that the number of bottles is within the valid range of 0 to 99.
answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
Java hw Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1 Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the...

    Problem 1 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...

  • (Python) 3. 99 Bottles of Beer on the Wall song, recursively Write a function Recurse that...

    (Python) 3. 99 Bottles of Beer on the Wall song, recursively Write a function Recurse that prints out the verses of the famous song, Lyrics of the song 99 Bottles of Beer (shown below). You must called this function recursively. 99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take one down and...

  • Write a for loop that produces the song Bottles of Beer on the Wall: 10 bottles...

    Write a for loop that produces the song Bottles of Beer on the Wall: 10 bottles of beer on the wall, 10 bottles of beer Take one down, pass it around, 9 bottles of beer on the wall 9 bottles of beer on the wall, 9 bottles of beer Take one down, pass it around, 8 bottles of beer on the wall ... (output continues in the same pattern) ... 1 bottles of beer on the wall, 1 bottles of...

  • C++ Help!    For this lab, we are going to practice a few different aspects of...

    C++ Help!    For this lab, we are going to practice a few different aspects of programming in C++. ·        Practice using cin and cout. ·        Practice using a loop (for or while) ·        Practice using a c-string. For this lab, you are going to create the code that writes the lyrics for the old song, “99 Bottles of Beer” with a slight change. If you have never seen the song, you can view the lyrics here: http://www.99-bottles-of-beer.net/lyrics.html Our version...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • Java program GCompound Practice Exercise CS141 Assignment Write a class that represents a digital snowman. Your...

    Java program GCompound Practice Exercise CS141 Assignment Write a class that represents a digital snowman. Your class should follow these guidelines: 1. Store the following private class variables bodyColor of type Color b. int x, int y for the upper left corner Graphics g. a. C. 2. Create two different constructor methods a. A (int x, int y, Graphics myG) parameter constructor that makes the snowman a light gray color by default and makes x and y the upper left...

  • Please write a Java program that ask the user how many beers he or she expects...

    Please write a Java program that ask the user how many beers he or she expects to consume each day, on average, as well as the average amount of money he or she spends on a single 12-ounce can of beer. Studies have shown that, on average, someone who consumes a single 12-ounce can of beer every day without compensating for the calorie intake through diet modifications or extra exercise, will gain approximately 15 pounds in one year. You can...

  • Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit....

    Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating- point number for the temperature and a character for the scale, eitherでfor Celsius or 'F' for fahrenheit. The class should have Four constructors: one for the number of degrees, one for the scale, one for both the degrees and scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and celsius if no...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • Write a program (C++) that requests the current time and a waiting time as two integers...

    Write a program (C++) that requests the current time and a waiting time as two integers for the number of hours and the number of minutes to wait. The program then outputs what the time will be after the waiting period. Use 24-hour notation for the times. Include a loop that lets the user repeat this calculation for additional input values until the user says she or he wants to end the program.You can assume the wait time will always...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT