Write a program that asks the user to enter the number of calories and fat grams
ID: 3527276 • Letter: W
Question
Write a program that asks the user to enter the number of calories and fat grams in a food item. The program should display the percentage of the calories that come from 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: (Calories from fat) / (total calories) If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. Note: The number of calories from fat cannot be greater than the total number of calories. If the program determines that the number of calories from fat is greater than the number calories in the food item, it should display an error message indicating that the input is invalid.Explanation / Answer
import java.util.Scanner; //Needed for the Scanner Class
import java.text.DecimalFormat; //Needed for the Decimal Class
/** This program asks the user to enter the number of calories and fat grams ina food item, then displays
the percentage of the calories that come from fat.
*/
public class Calories
{
public static void main (String [ ] args)
{
double Calories; //# of calories in a food item.
double FatGrams; //# of grams of fat in a food item.
double CaloriesFromFat; //Total number of calories from fat.
double PercentOfCalories;
//Create a Scanner object for keyboard input
Scanner keyboard = new Scanner (System.in);
//Get Calories from user
System.out.print("Enter number of calories:");
Calories = keyboard.nextDouble();
//Get FatGrams from user
System.out.print("Enter number of grams of fat:");
FatGrams = keyboard.nextDouble();
CaloriesFromFat = FatGrams * 9;
PercentOfCalories = CaloriesFromFat / Calories;
if (Calories < 0)
{
System.out.println( "Error: the number of calories cannot be less than zero.");
System.exit(0);
}
if (PercentOfCalories < 30)
{
System.out.println("This food is low in fat.");
System.exit(0);
}
if (FatGrams < 0)
{
System.out.println("Error: the number of grams of fat cannot be less than zero.");
System.exit(0);
}
if (CaloriesFromFat > Calories)
{
System.out.println("Error: the number of grams of fat cannot exceed total calories.");
System.exit(0);
}
//Create a DecimalFormat object
DecimalFormat formatter = new DecimalFormat("##");
System.out.println("Calories from fat: " +
formatter.format(PercentOfCalories*100) + "%");
}
}
Change this line
PercentOfCalories = CaloriesFromFat / Calories;
to
PercentOfCalories = CaloriesFromFat / Calories * 100;
Change this line
System.out.println("Calories from fat: " +
formatter.format(PercentOfCalories*100… + "%");
to
System.out.println("Calories from fat: " +
formatter.format(PercentOfCalories) + "%");
An ideal programming target is to only have one entry point to a program and only one exit.
It takes some thinking but I am sure you can do it.
Another important thing to note. There are no wrong answers in computing as long as the program does what the user needs.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.