(This is a better version of an exercise from Chapter 1.) A government research lab has concluded that an artificial sweetener commonly used in diet soda pop causes death in laboratory mice. A friend of yours is desperate to lose weight but cannot give up soda pop. Your friend wants to know how much diet soda pop it is possible to drink without dying as a result. Write a program to supply the answer. The input to the program is the amount of artificial sweetener needed to kill a mouse, the weight of the mouse, and the desired weight of the dieter. Assume that diet soda contains 1/10th of 1% artificial sweetener. Use a named constant for this fraction. You may want to express the percent as the double value 0.001.
Java Program:
import java.util.Scanner;
//Create a class name, DietSodaDemo
public class DietSodaDemo
{
public static void main(String[] args)
{
//Define constant value
final double DIETSODA = 0.001;
//Declare names of variables and there data types
double artificialSweetener;
double weightMouse;
double weightDesired;
double mouseWeight, mouseDeath,desiredWeight;
double calcDietSoda;
/*. The input to the program is the amount of artificial sweetener
needed to kill a mouse, the weight of the mouse, and
the desired weight of the dieter.
*/
Scanner inputValue = new Scanner(System.in);
System.out.print("Please enter the amount of artificial sweetener"+
" needed to kill a mouse:");
artificialSweetener = inputValue.nextDouble();
System.out.print("Please enter the weight of the mouse:");
weightMouse = inputValue.nextDouble();
System.out.print("Please enter the desired weight of the dieter:");
weightDesired = inputValue.nextDouble();
//Calculate how much diet soda pop it is possible to drink
//without dying as a result
calcDietSoda = (artificialSweetener / weightMouse)*weightDesired / DIETSODA;
System.out.println("The diet soda can safe have to " +
+ calcDietSoda + " liters a day before dying.");
}
}
1: Output of the program code:
Please enter the amount of artificial sweetener needed to kill a mouse:10
Please enter the weight of the mouse:20
Please enter the desired weight of the dieter:5
The diet soda can safe have to 2500.0 liters a day before dying.
2: Output of the program code:
Please enter the amount of artificial sweetener needed to kill a mouse:0.8
Please enter the weight of the mouse:0.2
Please enter the desired weight of the dieter:0.5
The diet soda can safe have to 2000.0 liters a day before dying.