123k views
3 votes
After having done the hand calculation and pseudocode for a program that models inflating a spherical balloon, now implement the task in Java. Remember, first the balloon is inflated to have a certain diameter (which is provided as an input). Then you inflate the balloon so that the diameter grows by an inch, and display the amount the volume has grown. Repeat that step: grow the diameter by another inch and show the growth of the volume.

Complete the modifing program.
the program will get the input from other method which I can't access.
Please just assume you'll get the input from the user.
import java.util.Scanner;
class Balloon
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Diameter: ");
double diameter = in.nextDouble();
double growth=1;
int c=0;
while (c<10)
{
double vol=diameter*diameter*diameter*3.14/6;
diameter = growth + diameter;
System.out.printf("Increase in diameter: %.0f",growth);
System.out.println("New Diameter "+ diameter);
double growthvol=diameter*diameter*diameter*3.14/6;
System.out.printf("Increase in volume: %.0f",growthvol-vol);
System.out.println("New Volume "+growthvol);
c++;
}
}
}

User Rpoleski
by
7.4k points

1 Answer

6 votes

Final answer:

To implement the balloon inflation program in Java, create a balloon class with a main method. Use the provided code and modify it to prompt the user for the diameter, calculate the volume, and display the increase in diameter and volume for each iteration.

Step-by-step explanation:

To implement the program in Java, you can use the provided code and modify it as necessary. Here's how you can do it:

  1. First, create a balloon class with a main method.
  2. Inside the main method, create a scanner object to get input from the user.
  3. Prompt the user to enter the diameter of the balloon using the scanner object.
  4. Initialize variables for growth (which is 1 inch in this case) and count (to keep track of the number of iterations).
  5. Create a while loop that will run for a maximum of 10 iterations.
  6. Inside the while loop, calculate the volume of the balloon using the formula provided in the code.
  7. Increase the diameter by the growth value.
  8. Calculate the new volume of the balloon using the updated diameter.
  9. Print the increase in diameter and the new diameter to the console using the printf method.
  10. Print the increase in volume and the new volume to the console using the printf method.
  11. Increment the count variable.

User Mykel Alvis
by
8.3k points