Help Computer Java Eclipse: Your job is to write a command line program in Java
ID: 3662480 • 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
/*Java Code for printing the song lyrics*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String line1 = " bottles of beer";
String line2 = "Take it down. ";
String line3 = "Pass it arround.";
int bottle_num = 0;
int temp_bottle_num = 0;
int loop;
int run_num = 0;
int run_loop;
Scanner in = new Scanner(System.in);
int len1 = line1.length();
int len2 = line2.length();
int len3 = line3.length();
char[] l1 = new char[len1];
char[] l2 = new char[len2];
char[] l3 = new char[len3];
System.out.println("Enter the number of times you want to run this code::");
run_num = in.nextInt();
/*1. Controlling the number of times code is run*/
for(run_loop = 1; run_loop <= run_num; run_loop++)
{
System.out.println("Run cycle :: " + run_loop + " ");
/*2. In order to use the loop counter to alter the program behavior
ask the user "Do you want to use the run loop as your number of bottles(Y/N)::");
If Y, then bottle_num = run_loop
else, the below
*/
System.out.println("Enter the number of bottles you want to start with::");
bottle_num = in.nextInt();
System.out.println(" ");
temp_bottle_num = bottle_num;
for(loop = 0; loop <= bottle_num; loop++)
{
if (temp_bottle_num == 1)
{
System.out.println(temp_bottle_num + " bottle of beer on the wall. " + line2 + line3);
temp_bottle_num = temp_bottle_num - 1;
}
if(temp_bottle_num == 0)
{
System.out.println("No more bottle of beer on the wall." + " ");
break;
}
System.out.println(temp_bottle_num + line1 + " on the wall. " + temp_bottle_num + line1 + "! " + line2 + line3);
temp_bottle_num = temp_bottle_num - 1;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.