SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Getting Started
Exercise:
Programming Projects
Question:4 | ISBN:9780132830317 | Edition: 5

Question

A government research lab has concluded that an artificial sweetener commonly used in diet soda pop will cause 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 program has no input but does have defined constants for the following items: the amount of artificial sweetener needed to kill a mouse, the weight of the mouse, the starting weight of the dieter, and the desired weight of the dieter. To ensure the safety of your friend, be sure the program uses the weight at which the dieter will stop dieting, rather than the dieter’s current weight, to calculate how much soda pop the dieter can safely drink. You may use any reasonable values for these defined constants. Assume that diet soda contains 1/10th of 1% artificial sweetener. Use another named constant for this fraction. You may want to express the percent as the double value 0.001. (If your program turns out not to use a defined constant, you may remove that defined constant from your program.)


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program Code:

//Main class name, ArtificialSweetener 
public class ArtificialSweetener
{
    //Program starts the main argument function
     public static void main(String []args)
     {
    
        //Defined four constant items
        double killMouse =0.0000013;
		double weightMouse = 0.020; 
		double dieterStart = 120.0;
		double desiredWeight =90.0;
		//Variable declaration
		double safetySoda;
		final double PERCENTAGE_OF_ArtificialSweetener = 0.001;

        // Calculate the proportionate fatal amount of 
		// soda for safety
		safetySoda = (killMouse / weightMouse)
				* desiredWeight / PERCENTAGE_OF_ArtificialSweetener;
         //Print result on screen
		System.out.printf("The dieter can safely have up" 
				+ " to "+safetySoda 
				+ " drink.."); 

     }
}

Output:

The dieter can safely have up to 5.849999999999999 drink..
0 0

Discussions

Post the discussion to improve the above solution.