After having done the hand calculation and pseudocode for a program that models
ID: 3573367 • Letter: A
Question
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++;
}
}
}
Expected output should've looked like this:
or
Explanation / Answer
Please follow the code and comments for description :
CODE :
import java.util.Scanner; // required imports for the class
class Balloon { // class to run the code
public static void main(String[] args) { // driver method
Scanner in = new Scanner(System.in); // scanner class to get the data
System.out.print("Diameter: "); // message
double diameter = in.nextDouble(); // prompt
double growth = 1; // local variables
int c = 0;
while (c < 10) { // iterate over the loop
double radius = diameter / 2; // calculate the radius
double vol = ((double)4 / 3) * 3.14 * radius * radius * radius; // calculate the initial volume
diameter = growth + diameter; // update the diamter
System.out.printf(" Increase in the Diameter is : %.0f", growth); // message
System.out.println(" New Diameter is : " + diameter); // print the diameter
radius = diameter / 2; // calculate the new diameter
double growthvol = ((double)4 / 3) * 3.14 * radius * radius * radius; // calculate the new volume
System.out.printf("Increase in the Volume is : %.0f", growthvol - vol); // message
System.out.println(" New Volume is : " + growthvol); // message
c++; // increment the count
}
}
}
OUTPUT :
run 1 :
Diameter: 10
Increase in the Diameter is : 1
New Diameter is : 11.0
Increase in the Volume is : 173
New Volume is : 696.5566666666667
Increase in the Diameter is : 1
New Diameter is : 12.0
Increase in the Volume is : 208
New Volume is : 904.3199999999998
Increase in the Diameter is : 1
New Diameter is : 13.0
Increase in the Volume is : 245
New Volume is : 1149.7633333333333
Increase in the Diameter is : 1
New Diameter is : 14.0
Increase in the Volume is : 286
New Volume is : 1436.0266666666666
Increase in the Diameter is : 1
New Diameter is : 15.0
Increase in the Volume is : 330
New Volume is : 1766.25
Increase in the Diameter is : 1
New Diameter is : 16.0
Increase in the Volume is : 377
New Volume is : 2143.5733333333333
Increase in the Diameter is : 1
New Diameter is : 17.0
Increase in the Volume is : 428
New Volume is : 2571.1366666666668
Increase in the Diameter is : 1
New Diameter is : 18.0
Increase in the Volume is : 481
New Volume is : 3052.08
Increase in the Diameter is : 1
New Diameter is : 19.0
Increase in the Volume is : 537
New Volume is : 3589.5433333333335
Increase in the Diameter is : 1
New Diameter is : 20.0
Increase in the Volume is : 597
New Volume is : 4186.666666666667
run 2 :
Diameter: 7.5
Increase in the Diameter is : 1
New Diameter is : 8.5
Increase in the Volume is : 101
New Volume is : 321.39208333333335
Increase in the Diameter is : 1
New Diameter is : 9.5
Increase in the Volume is : 127
New Volume is : 448.6929166666667
Increase in the Diameter is : 1
New Diameter is : 10.5
Increase in the Volume is : 157
New Volume is : 605.82375
Increase in the Diameter is : 1
New Diameter is : 11.5
Increase in the Volume is : 190
New Volume is : 795.9245833333334
Increase in the Diameter is : 1
New Diameter is : 12.5
Increase in the Volume is : 226
New Volume is : 1022.1354166666666
Increase in the Diameter is : 1
New Diameter is : 13.5
Increase in the Volume is : 265
New Volume is : 1287.59625
Increase in the Diameter is : 1
New Diameter is : 14.5
Increase in the Volume is : 308
New Volume is : 1595.4470833333332
Increase in the Diameter is : 1
New Diameter is : 15.5
Increase in the Volume is : 353
New Volume is : 1948.8279166666664
Increase in the Diameter is : 1
New Diameter is : 16.5
Increase in the Volume is : 402
New Volume is : 2350.87875
Increase in the Diameter is : 1
New Diameter is : 17.5
Increase in the Volume is : 454
New Volume is : 2804.7395833333335
Description :
During the casting or the calculation of the volume and the radius the RHS needs to be casted for the numerator with the double datatype so as to clearly get the result. And that the formula is also been updated.
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.