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

One of the common things that developers do in the course of their work is to ta

ID: 3775773 • Letter: O

Question

One of the common things that developers do in the course of their work is to take a previously-written code and update it -- usually because they have learned a new technique or new requirements come up that the old code didn't handle well. This is called refactoring. Now that we know about subroutines inJava, refactor your Bottles program to take advantage of them:

Use member variables (also known as 'global variables') to track the state of the Crate, Wall, and Floor

Use a subroutine to do the work of adding bottles to the wall

Use a subroutine to handle the singing of the song by passing it some variables as an argument -- so that when it's done, your main() can ask the user if they want to sing it again!

--------------------------------------

Explanation / Answer

import java.util.Scanner;

class MilkBottles {
   public static void addBottles(int bottles,String str) //function to add bottles to the wall
   {
       System.out.println(" Okay. Taking the bottles out of the crate and placing them on the wall!");
int k = 1;
for (int i = (bottles-1); i >= 0 ;i-- )
{
System.out.println("Moving 1 bottle of " + str + " to the wall. " + i + " in the crate. " + k + " on the wall!");
k++;
}
   }
   public static void singSong(int bottles,String str) //function to sing the song
   {
       System.out.println();

for (int i =bottles; i >= 1; i-- )
{
System.out.println(i + " bottles of " + str + " on the wall, " + i + " bottles of " + str + "; take one down, pass it around, " + (i-1) + " bottles of " + str + " on the wall! (" + (bottles-i+1) + " on the floor)") ;
}
   }
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int bottles;
String str;

System.out.print("How many bottles are in 1 crate? ");
bottles = sc.nextInt();
System.out.print("What's in the bottles? ");
str = sc.next();

addBottles(bottles,str);
  
singSong(bottles,str);

       System.out.println("Do you want to sing the song again? <press 1 for yes, 0 for no>");
       int option = sc.nextInt();
       if(option == 1)
       singSong(bottles,str);
  
}
}

output:

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