Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a complete program that uses loops and methods to print the 9 9 Bottles of Beer song. Your song will print the words
Write a complete program that uses loops and methods to print the Bottles of Beer" song. Your song will print the words of the numbers eg Ninetynine instead of
The Song
Bottles of Beer" is a traditional, repeating singalong song that counts down from some number to zero. If you are unfamiliar with the song, you can read the lyrics hereLinks to an external site. or watch it being sung hereLinks to an external site..
Part One: Read in the number and validate with a method points
Your program will allow the user to specify a starting number of bottles. This number can be anywhere between and inclusive Use a loop to continue asking the user for a number until it is valid, meaning in the specified range.
Write a method that determines if the number if valid. Invoke that method to determine whether you need to keep looping.
For full credit, use constants instead of hardcoded values to make code more readable and easier to maintain.
Part Two: Print the song with words points
Write a method that prints the song starting at the user's number. Use a loop to print each verse.
Write one or more helper methods that convert a number into the words eg into "Fortythree"
For full credit, you should have no more than hardcoded Strings. All of the numbers between and can be represented with some combination of these Strings:
Ninety, Eighty, Thirty, Twenty strings
Nineteen, Eighteen, Eleven, Ten strings
Nine, Eight, One, Zero strings
Hint: think about how you can use integer division and modulus to access a number's tensplace digit and onesplace digit eg for the number is the tensplace digit and is the onesplace digit
Recommendation: Incremental Development
I strongly suggest you work on building up your program a little at a time: code and test, code and test, etc. Here is one possible approach you might use in development.
First, sketch out in wordspseudocode your approach. As part of this, write out what you think the method headers will be: the inputs, outputs, and a descriptive name. Make note of which methods you plan to call from other methods. See below for the extra credit related to this step!
Then begin coding. After each step of development, test and revise until that step is fully functional.
Read in and validate a user number using a method
Print out the song using numbers not wordseg bottles not Ninetynine bottles
Convert the numbers to words
Start with the numbers only
Then add code for the numbers
Finally, add code for the numbers
Note that you do not have to follow this approach. It is just a suggestion. But you should follow some plan of incremental development. Code and test small pieces at a time as you build up your full solution.
General Coding Requirements points
For full credit, your program should have at least methods including main Also:
code should compile
choose the best loops and conditional structures for a task
use constants instead of hardcoded values when possible to improve clarity and readability and to make code easier to maintain
reduce duplicated code consider how to replace repeated code with a method
follow Java naming conventions for variables and methods lower camel case with no underscores
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started