Question: Given main, complete the Food Item class in file Foodltem.java) with constructors to initialize each food item. The default constructor should initialize the name to


Given main, complete the Food Item class in file Foodltem.java) with constructors to initialize each food item. The default constructor should initialize the name to "None" 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: M&M's 10.0 34.0 2.0 1.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 1.0 is the number of servings, the output is: Nutritional information per serving of None: Fat: 0.00 g Carbohydrates: 0.00 g Protein: 0.00 g Number of calories for 1.00 serving (s): 0.00 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 serving (s): 234.00 The first Foodltem above is initialized using the default constructor. 289734 1743310 LAB ACTIVITY 7.10.1: LAB: Nutritional information (classes/constructors) 0/10 Current file: FoodItem.java Load default template... 1 public class FoodItem { 2 private String name; private double fat; 4 private double carbs; 5 private double protein; 6 7 // TODO: Define default constructor 8 9 // TODO: Define second constructor with arguments to initialize 10 private fields (name, fat, carbs, protein) 11 12 public String getName() { 13 return name; 14 } 15 File is marked as read only Current file: NutritionalInfo.java 4 public static void main(String[] args) { Scanner scnr = new Scanner(System.in); FoodItem food Item1 = new FoodItem(); String itemName = scnr.next(); double amountFat = scnr.nextDouble(); double amountCarbs = scnr.nextDouble(); double amount Protein = scnr.nextDouble(); FoodItem foodItem2 = new FoodItem(itemName, amountFat, amountCarbs, amount Protein); 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 double numServings = scar.nextDouble(); foodItem1.printInfo(); System.out.printf("Number of calories for %.2f serving(s): %.2f ", numServings, foodItem1.getCalories (numServings)); System.out.println(" "); foodItem2.printInfo()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
