Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

COBOL PROGRAMMING: There is a song called \"99 Bottles of Beer\", Here are the l

ID: 3738459 • Letter: C

Question

COBOL PROGRAMMING:

There is a song called "99 Bottles of Beer", Here are the lyrics:

99 Bottles of Beer on the Wall, 99 Bottles of Beer;

Take 1 down, pass it around, 98 Bottles of Beer on the Wall...

Then it repeats:

98 Bottles of Beer on the Wall, 98 Bottles of Beer;

Take 1 down, pass it around, 97 Bottles of Beer on the Wall...

The song repeats until the final verse:

1 Bottle of Beer on the Wall, 1 Bottles of Beer;

Take 1 down, pass it around, No More Bottles of Beer on the Wall...

No More Bottles of Beer on Wall, No More bottles of Beer...

Get the Hat and pass it around, time to B Double E Double R U-N, Beer Run...

Write a program to accept the number of starting beers on the wall from 1 to 99.

The program will write out the lyrics starting with the number of bottles specified.

This can be done with an in-line perform or the performing of a paragraph where the number of bottles is decremented and put into the verse for displaying.

The last verse is different as you can see.

The program should only accept a number from 1 to 99 and re-prompt the user to enter a valid number until correctly entered.

For an extra 20 points, specify the number of beers to take down from another user input and reduce the number on the wall until the number of bottles is less that that number, then the last verse is written.

Explanation / Answer

import java.util.Scanner;

// Class Lyrics definition

public class Lyrics

{

// Static method to accept, validate and return number of bottles

static int acceptData()

{

// To store number of bottles

int numberOfBottles;

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Loops till valid number of bottles entered by the user

do

{

// Accepts number of bottles

System.out.print(" Enter number of bottles: ");

numberOfBottles = sc.nextInt();

// Checks if the number of bottles is between 1 and 99 inclusive then valid

if(numberOfBottles >= 1 && numberOfBottles <= 99)

// Returns number of bottles

return numberOfBottles;

// Otherwise, invalid bottle number message displayed

else

System.out.print(" Invalid bottle number! Please re enter.");

}while(true); // End of do - while loop

}// End of method

// Static method takes number of bottles as parameter

// Displays the lyrics

static void generateLyrics(int no)

{

// Loops from number of bottles to one in reverse order

for(int c = no; c >= 1; c--)

// Displays lyrics

System.out.println(c + " Bottles of Beer on the Wall, " + c + " Bottles of Beer;");

// Message for no more bottles

System.out.println("No More Bottles of Beer on Wall, No More bottles of Beer... " +

"Get the Hat and pass it around, time to B Double E Double R U-N, Beer Run...");

}// End of method

// main method definition

public static void main(String[] args)

{

// Calls the static method to accept number of bottles

int numberOfBottles = acceptData();

// Calls the static method to display the lyrics

generateLyrics(numberOfBottles);

}// End of main method

}// End of class

Sample Output:


Enter number of bottles: 100

Invalid bottle number! Please re enter.
Enter number of bottles: 0

Invalid bottle number! Please re enter.
Enter number of bottles: 6
6 Bottles of Beer on the Wall, 6 Bottles of Beer;
5 Bottles of Beer on the Wall, 5 Bottles of Beer;
4 Bottles of Beer on the Wall, 4 Bottles of Beer;
3 Bottles of Beer on the Wall, 3 Bottles of Beer;
2 Bottles of Beer on the Wall, 2 Bottles of Beer;
1 Bottles of Beer on the Wall, 1 Bottles of Beer;
No More Bottles of Beer on Wall, No More bottles of Beer...
Get the Hat and pass it around, time to B Double E Double R U-N, Beer Run...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote