Help Computer Java Eclipse: Your job is to write a command line program in Java
ID: 3662434 • Letter: H
Question
Help Computer Java Eclipse:
Your job is to write a command line program in Java that prints the song lyrics, nicely formatted as follows:
99 bottles of beer on the wall.
99 bottles of beer!
Take one down. Pass it around.
98 bottles of beer on the wall.
98 bottles of beer on the wall.
98 bottles of beer!
Take one down. Pass it around.
97 bottles of beer on the wall.
... and so on until ...
2 bottles of beer on the wall.
2 bottles of beer!
Take one down. Pass it around.
1 bottle of beer on the wall.
1 bottle of beer on the wall.
1 bottle of beer!
Take one down. Pass it around.
No more bottles of beer on the wall.
Be very careful with the singular / plural of the word bottle(s) as it appears in the lyrics. There are two things I
am looking for in this assignment:
1. Can you use a loop to control the number of times code is run
2. Can you use that loop counter to alter the program behavior
You want to keep an eye on the number of bottle to get the correct singular or plural format of the word bottle,
and you also need to alter that last line of the verse when we run out of bottles.
While this may seem a trivial or silly assignment at first, using a loop counter and altering program behavior
based on the value of that counter is a very common requirement in many programs. So pay attention to the
details here and write the most elegant code that you can to solve this seemingly silly assignment.
Explanation / Answer
This below Java program will print the plural bottles lyrics upto 2 and then will go to the else condition print the singular bottle lyrics..
I have written the class called Lyrics and used the for loop from 99 to 0 by decresing the counter it will process the if condition with not equal to 1 and then else condition..
See the below java code for
public class Lyrics
{
public static void main(String[] args)
{
int i = 0;
for( i=99; i<0; --i)
{
System.out.println("Hello World");
if(i !=1 )
{
System.out.println(i +" bottles of beer on the wall.");
System.out.println(i +" bottles of beer!");
System.out.println("Take one down. Pass it around.");
}
else
{
System.out.println(i +" bottle of beer on the wall.");
System.out.println(i +" bottle of beer!");
System.out.println("Take one down. Pass it around.");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.