\Java
FatCalories.java
//******************************************************************** // // FatCalories.java // //******************************************************************** // // Program Description and Design Overview: // // ------------------------------------------------------------- // Program FatCalories calculates a person's percentage of // calories obtained from fat and prints an appropriate message. // ------------------------------------------------------------- // The American Heart Assn. recommends that no more than 30 percent // of a person's daily calories come from fat. Each gram of fat is 9 calories. // Given the grams of fat and the number of calories in a food item, we can // calculate the percentage of calories that comes from fat for that food item. // Program design: // 1. Prompt user to enter name of a food // "Enter the name of a food item: " // 2. Input food name // Hint: The food name may be more than one word. This variable should be a // String. Use Keyboard.readString() to input all the words typed // on a line (including the whitespace between them) into one String // variable. (If you just wanted to read the next single word typed // on a line, you should use Keyboard.readWord()) // 3. Prompt user to enter the grams of fat in the food // (for the food he/she entered in previous input) // "Enter the grams of fat: " // 4. Input grams of fat // 5. Prompt user to enter the number of calories in the food // "Enter the the number of calories: " // 6. Input the number of calories // 7. Print the food item and the percentage of calories that come from fat // (fatCalPercent) // 8. Print one of the two following messages depending on the percentage // of calories from fat (value of fatCalPercent). // "This item is Heart Heathy!" // "This item is NOT Heart Heathy!" // (Note, there are at least four different ways to code this in Java.) // // // Run your program 4 times with the following data: // Item Grams of fat Calories // -------- ------------ -------- // Tuna 1 60 // Spaetzle 2 170 // V8 Juice 0 35 // Corned Beef 7 200 // // Write down your answers. When done edit your Java source file and insert the // food item name, the percentage of calories from fat (value of fatCalPercent), // and whether that food is Heart Heathy or NOT Heart Heathy for each of the // 4 items as comments in your program. // // // Input Requirements: // // // Output Requirements: // // // // Program Preconditions: // // //******************************************************************** import cs1.Keyboard; public class FatCalories { //----------------------------------------------------------------- // //----------------------------------------------------------------- public static void main (String[] args) { double fat, fatCalPercent; int calories; System.out.println (); //skips line System.out.print ("Enter the name of a food item: "); //Enter the nameof a food item String foodName = Keyboard.readString(); //inputs user response into string variable foodName System.out.println (); //skips line System.out.print ("Enter the grams of fat: "); //Enter grams of fat: fat = Keyboard.readFloat(); //inputs user response into float variable fat System.out.println (); //skips line System.out.print ("Enter the the number of calories: "); //Enter the number of calories calories = Keyboard.readInt(); //inputs user response into int variable calories System.out.println (); fatCalPercent = ((9 * fat) / (float)(calories)) * 100; //calculates calorie to fat percentage System.out.println ("Percentage of fat calories in " + foodName + " : " + fatCalPercent); //Percentage of fat calories in (foodname entered) : (calorie to fat percentage) if (fatCalPercent <= 30) System.out.println ("This item is Heart Healthy!"); //This item is Heart Healty! else System.out.println ("This item is NOT Heart Healthy!"); //This item in NOT Heart Healthy //Results of test //Tuna: 15% (Heart Healthy) //Spaetzle: 10.588% (Heart Healthy) //V8 Juice: 0% (Heart Healthy) //Corned beef: 31.5% (NOT Heart Healthy) } //end main } // end class FatCalories