In: Computer Science
You are tasked to provide a Fat Gram Calculator program for a local Weight Loss Center. The program should ask the user for the name of a food item, the number of calories and the number of fat grams in the item. The program should display the percentage of calories that come from fat. If the calories from fat are less than 30 percent of the total calories of the food, it should display a message indicating that the food is low in fat. If the percentage is equal to or higher than 30 percent the message should indicate that the food is high in fat.
One gram of fat has 9 calories, so
calories
from fat = fat grams * 9
The percentage of calories from fat can be calculated as
Percent Fat =
Calories from fat / total calories
Input validation: The program should make sure that the number of calories is greater than 0, the number of fat grams is 0 or more, and the number of calories from fat is not greater than the number of calories input.
code:
//import java util
import java.util.Scanner;
//define a class fat gram calculator
class Main
{
//main function
public static void main(String args[])
{
//scanner class
Scanner s=new Scanner(System.in);
//scanner class
Scanner t=new Scanner(System.in);
//while loop
while(true)
{
//print statement to enter disk name
System.out.print("Please enter the name of the dish: ");
//input thr string
String name=s.nextLine();
//print statement for fat
System.out.print("Enter the fat in grams: ");
//input the fat grams
double gm_ft=s.nextDouble();
//print statement for calories
System.out.print("Enter the calories: ");
//input the calories
double cal=s.nextDouble();
if(cal<0 || gm_ft<0 || cal>(gm_ft*9))
//print there is wrong value
System.out.println("error");
//calculation
double p=(gm_ft*9)/cal;
//print calories
System.out.println("The calories percentage = "+(p)+"%");
//if the percentage is less than 0.3
if(p<0.3)System.out.println("The food is low in fat");
//check to continue
System.out.print("Do you want to continue [* to quit]: ");
//onatin input
String F=t.nextLine();
if(F.charAt(0)=='*')break;
}
}
}