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

A water tank contains a number of water gallons. A water tap increases the numbe

ID: 3854031 • Letter: A

Question

A water tank contains a number of water gallons. A water tap increases the number of gallons at a rate of rate In per minute and a second tap decreases the number of gallons at a rate rateOut per minute. Write a program that promts the user to enter the number of water gallons, the value of rateIn, and the value of rateOut. The program calculates and prints the time needed to empty the tank. For example if the number of gallons, is 15 gallons, rateln is 0.03 and rateOut is 0.07, then after one minute the first tap adds 10*0.03 = 0.45 gallons, while the second tap drains 15*0.07 = 1.05 gallons, so a number gallons, after one minute is 15 + 0.45 - 1.05 = 14.4 gallons. Sample inputs/outputs: Enter the water quantity: 15 Enter rate in and the rate out: 0.1 0.8 The time to empty the tank is 89 minutes

Explanation / Answer

TankEmptyTest.java

import java.text.DecimalFormat;

import java.util.Scanner;

public class TankEmptyTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Enter the water quantity: ");

double quantity = scan.nextDouble();

System.out.print("Enter rate in and rate out: ");

double rateIn = scan.nextDouble();

double rateOut = scan.nextDouble();

int minutes = 0;

DecimalFormat df = new DecimalFormat("#.#");

while(quantity>0) {

quantity = quantity + quantity*rateIn - quantity*rateOut;

quantity = Double.parseDouble(df.format(quantity));

minutes++;

}

System.out.println("The time to empty the tank is "+minutes+" minutes.");

}

}

Output:

Enter the water quantity: 15
Enter rate in and rate out: 0.1 0.8
The time to empty the tank is 5 minutes.

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