Design a program that asks the user for the number of fat grams and calories in
ID: 3652136 • Letter: D
Question
Design a program that asks the user for the number of fat grams and calories in a food item.The program should calculate the % of calories from fat in a food and signals when the food is low in fat.
Validate the input as follows.
Make sure the number of fat grams and calories are not less than 0. Reject users answer if negative amount is entered.
According to nutritional formulas, the number of calories cannot exceed fat grams * 9.
Make sure that the number of calories entered is not greater than fat grams * 9.
Once correct data has been entered, the program should calculate and display the percentage of calories that come from fat.
Use the following formula: Percentage of calories from fat=( Fat grams * 9) / Calories
Some nutritionists classify food as
Explanation / Answer
Please rate...
Program GramCalories.java
====================================================
import java.util.Scanner;
class GramCalories
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
Scanner s1=new Scanner(System.in);
while(true)
{
System.out.print("Enter fat grams: ");
double fg=s.nextDouble();
System.out.print("Enter calories: ");
double c=s.nextDouble();
if(c<0 || fg<0 || c>(fg*9))
System.out.println("Wrong value entered, enter again");
double p=(fg*9)/c;
System.out.println("Percentage of calories from fat= "+(p)+"%");
if(p<0.3)System.out.println("The food is low in fat");
System.out.print("Do you want to continue [c to quit]: ");
String ch=s1.nextLine();
if(ch.charAt(0)=='c')break;
}
}
}
=================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.