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:
Console Input And Output
Exercise:
Programming Projects
Question:6 | ISBN:9780132830317 | Edition: 5

Question

(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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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. 

 

0 0

Discussions

Post the discussion to improve the above solution.