53.5k views
4 votes
Please note that the numbers in the square brackets [ ] before the task you are required to do is the number of points rewarded for the task if done correctly. The maximum number of points is 10 for this question.

The air leaks out of a balloon at a fixed rate. The volume after each second is reduced by 8.5%. The number " 8.5 " in your code should be made into a constant.
[5] Write a method that takes two arguments:
[1] 1 - An integer representing the elapsed time in seconds
[1] 2 - A double representing the starting volume
[1] The method should return the volume left inside the balloon after the given elapsed time.
Hint: You will need a loop where the volume is update after each iteration.
[2] Write the code statements that you would add inside the Main() method to invoke this new method five times with values (1,100),(3,100),(4,100),(7,100),(8, 100 ) and display the return value of each call. The number "100" in your code should be made into a constant.

1 Answer

1 vote

Final answer:

To solve this problem, we can use a loop to calculate the volume of the balloon after each second. The volume is reduced by 8.5% every second, so we can update the volume by multiplying it by 0.915. We repeat this process for the given elapsed time and return the final volume.

Step-by-step explanation:

To solve this problem, we can use a loop to calculate the volume of the balloon after each second. The volume is reduced by 8.5% every second, so we can update the volume by multiplying it by 0.915. We repeat this process for the given elapsed time and return the final volume.

public class Balloon {

public static double calculateVolume(int time, double startingVolume) {
double volume = startingVolume;
for (int i = 0; i < time; i++) {
volume *= 0.915;
}
return volume;
}

public static void main(String[] args) {
double startingVolume = 100.0;
System.out.println(calculateVolume(1, startingVolume));
System.out.println(calculateVolume(3, startingVolume));
System.out.println(calculateVolume(4, startingVolume));
System.out.println(calculateVolume(7, startingVolume));
System.out.println(calculateVolume(8, startingVolume));
}

}

User Manushi
by
8.4k points