Question

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

Problem 2

Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1.

Include an accessor method that returns the current count value (getCount()). There should be no input / setter method or other mutator methods. The only method that can set the counter is the one that sets it to zero (and the constructor).

This class should also override the Object class toString() and equals() methods - refer to the Java APIs for more guidance on how to override this (Chapter 8 also describes this in a little detail, but the API may be more useful): https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html

Finally, write a main method (in this class) to test all the methods in your class definition.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Problem 1: Please see the below code for BeerSong class, screenshots for code and output are attached as well.

public class BeerSong {
    int count;

    String[] hundredthName = new String[]{"Twenty", "Thirty", "Forty", "Fifty","Sixty","Seventy","Eighty","Ninety"};
    String[] unitName = new String[]{"zero","one","two","three","four","five","six","seven","eight","nine","ten",
            "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public BeerSong(int count) {
        if (count<0){
            this.count = 0;
        }else if(count>99){
            this.count = 99;
        }else{
            this.count = count;
        }
    }

    public void PrintSong(){
        for (int i = count; i >= 0 ; i--) {
            if(i>=20) {
                if(i%10==0){
                    System.out.println(hundredthName[i/10-2]+" bottles of beer on the wall.");
                }else{
                    System.out.println(hundredthName[i/10-2]+"-"+unitName[i%10]+" bottles of beer on the wall.");
                }
            }else{
                System.out.println(unitName[i%10]+" bottles of beer on the wall.");
            }
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
Problem 1 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
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