public class FoodItem {    private String name;    private double fat;    private double carbs;    private double protein;      // TODO: Define default constructor    // TODO: Define second constructor with parameters to initialize private fields (name, fat, carbs, protein)    public String getName() {       return name;    }    public double getFat() {       return fat;    }    public double getCarbs() {       return carbs;    }    public double getProtein() {       return protein;    }       public double getCalories(double numServings) {       // Calorie formula       double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings;       return calories;    }    public void printInfo() {       System.out.println("Nutritional information per serving of " + name + ":");       System.out.printf("  Fat: %.2f g\n", fat);       System.out.printf("  Carbohydrates: %.2f g\n", carbs);       System.out.printf("  Protein: %.2f g\n", protein);    } } import java.util.Scanner; public class NutritionalInfo {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       FoodItem foodItem;              String itemName = scnr.next();       if(itemName.equals("Water") || itemName.equals("water")) {          foodItem = new FoodItem();                    foodItem.printInfo();          System.out.printf("Number of calories for %.2f serving(s): %.2f\n", 1.0,                            foodItem.getCalories(1.0));       }              else {          double amountFat = scnr.nextDouble();          double amountCarbs = scnr.nextDouble();          double amountProtein = scnr.nextDouble();                 foodItem = new FoodItem(itemName, amountFat, amountCarbs, amountProtein);                 double numServings = scnr.nextDouble();                                                                 foodItem.printInfo();          System.out.printf("Number of calories for %.2f serving(s): %.2f\n", 1.0,                              foodItem.getCalories(1.0));                                        System.out.printf("Number of calories for %.2f serving(s): %.2f\n", numServings,                              foodItem.getCalories(numServings));       }    } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

public class FoodItem {
   private String name;
   private double fat;
   private double carbs;
   private double protein;  
   // TODO: Define default constructor
   // TODO: Define second constructor with parameters to initialize private fields (name, fat, carbs, protein)
   public String getName() {
      return name;
   }
   public double getFat() {
      return fat;
   }
   public double getCarbs() {
      return carbs;
   }
   public double getProtein() {
      return protein;
   }   
   public double getCalories(double numServings) {
      // Calorie formula
      double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings;
      return calories;
   }
   public void printInfo() {
      System.out.println("Nutritional information per serving of " + name + ":");
      System.out.printf("  Fat: %.2f g\n", fat);
      System.out.printf("  Carbohydrates: %.2f g\n", carbs);
      System.out.printf("  Protein: %.2f g\n", protein);
   }
}

import java.util.Scanner;

public class NutritionalInfo {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      FoodItem foodItem;
      
      String itemName = scnr.next();
      if(itemName.equals("Water") || itemName.equals("water")) {
         foodItem = new FoodItem();
         
         foodItem.printInfo();
         System.out.printf("Number of calories for %.2f serving(s): %.2f\n", 1.0, 
                          foodItem.getCalories(1.0));
      }
      
      else {
         double amountFat = scnr.nextDouble();
         double amountCarbs = scnr.nextDouble();
         double amountProtein = scnr.nextDouble();
      
         foodItem = new FoodItem(itemName, amountFat, amountCarbs, amountProtein);
      
         double numServings = scnr.nextDouble();
                                                      
         foodItem.printInfo();
         System.out.printf("Number of calories for %.2f serving(s): %.2f\n", 1.0,
                             foodItem.getCalories(1.0));
                             
         System.out.printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
                             foodItem.getCalories(numServings));
      }
   }
}

maing, complete the FoodItem Class (in
1.java) with constructors
each food item. me derauit
tructor
should initialize the name to "Water" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of
fat, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value.
Ex: If the input is:
Water
the output is:
Nutritional information per serving of Water:
Fat: 0.00 g
Carbohydrates: 0.00 g
Protein: 0.00 q
Number of calories for 1.00 serving (s): 0.00
Ex: If the input is:
M&M's
10.0
34.0
2.0
3.0
where M&M's is the food name, 10.0 is the grams of fat, 34.0 is the grams of carbohydrates, 2.0 is the grams of protein, and 3.0 is the
number of servings, the output is:
Nutritional information per serving of M&M's:
Fat: 10.00 g
Carbohydrates: 34.00 g
Protein: 2.00 g
Number of calories for 1.00
Number of calories for 3.00
serving (s): 234.00
serving (s): 702.00
Note: The program outputs the number of calories for one serving of a food and for the input number of servings as well. The program only
outputs the calories for one serving of water.
Transcribed Image Text:maing, complete the FoodItem Class (in 1.java) with constructors each food item. me derauit tructor should initialize the name to "Water" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of fat, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value. Ex: If the input is: Water the output is: Nutritional information per serving of Water: Fat: 0.00 g Carbohydrates: 0.00 g Protein: 0.00 q Number of calories for 1.00 serving (s): 0.00 Ex: If the input is: M&M's 10.0 34.0 2.0 3.0 where M&M's is the food name, 10.0 is the grams of fat, 34.0 is the grams of carbohydrates, 2.0 is the grams of protein, and 3.0 is the number of servings, the output is: Nutritional information per serving of M&M's: Fat: 10.00 g Carbohydrates: 34.00 g Protein: 2.00 g Number of calories for 1.00 Number of calories for 3.00 serving (s): 234.00 serving (s): 702.00 Note: The program outputs the number of calories for one serving of a food and for the input number of servings as well. The program only outputs the calories for one serving of water.
Expert Solution
steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education