Question

Nutritional information (classes/constructors)

7.16 LAB: Nutritional information (classes/constructors)

Given main(), complete the FoodItem class (in file FoodItem.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 FoodItem above is initialized using the default constructor.


2 1
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
import java.util.Scanner;

public class NutritionalInfo {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      
      FoodItem foodItem1 = new FoodItem();
      
      String itemName = scnr.next();
      double amountFat = scnr.nextDouble();
      double amountCarbs = scnr.nextDouble();
      double amountProtein = scnr.nextDouble();
      
      FoodItem foodItem2 = new FoodItem(itemName, amountFat, amountCarbs, amountProtein);
      
      double numServings = scnr.nextDouble();
      
      foodItem1.printInfo();
      System.out.printf("Number of calories for %.2f serving(s): %.2f\n", numServings, 
                          foodItem1.getCalories(numServings));
                           
      System.out.println("\n");
                           
      foodItem2.printInfo();
      System.out.printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
                          foodItem2.getCalories(numServings));
   }
}
public class FoodItem {
   
   private String name;
   private double fat;
   private double carbs;
   private double protein;
   
   FoodItem() {
      
      name = "None";
      fat = 0;
      carbs = 0;
      protein = 0;
      
   }
   
   FoodItem(String name, double fat, double carbs, double protein) {
      
      this.name = name;
      this.fat = fat;
      this.carbs = carbs;
      this.protein = 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) {
      
      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);
      
   }
   
}


Add a comment
Know the answer?
Add Answer to:
Nutritional information (classes/constructors)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • In C++ please. 7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in...

    In C++ please. 7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the Food Item class (in files Foodltem.h and Fooditem.cpp) 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...

  • Question 5 (4 points) other nutritional data for each serving of an item. Carbohydrates provide a...

    The nutritional information label.. Question 5 (4 points) other nutritional data for each serving of an item. Carbohydrates provide about 17 kJ/g of energy, fats provide about 38 k/g, and protein, though usually not broken down like carbohydrates and fats, can provide an additional 17 kJ/g. The total energy content in a food item is found by adding the energies from each of the components (carbohydrates, fats, and protein. The nutritional label of an original Krispy KremeB donut lists 32...

  • post 5 - Nutritional energy Wrong- 248 as well as 250 is wrong ± Nutritional Energy...

    post 5 - Nutritional energy Wrong- 248 as well as 250 is wrong ± Nutritional Energy Alexander, who weighs 189 lb , decides to climb Mt. Krumpett, which is 5560 m high. For his food supply, he decides to take nutrition bars. The label on the bars states that each 100-g bar contains 10 g of fat, 40 g of protein, and 50 g of carbohydrates. One gram of fat contains 9 Calories, whereas each gram of protein and carbohydrates...

  • Reading Food Labels Following are food labels from a beef burger and a vegetable burger. Compare...

    Reading Food Labels Following are food labels from a beef burger and a vegetable burger. Compare and contrast their nutritional content. Beef burger (3 oz.) Vegetable burger (2.5 oz.) Nutrition Facts Amount per serving Calories 230 Calories from Fat 140 % Daily Value Total Fat 16 g 25% Saturated Fat 6 g 30% Polyunsaturated 1g Monounsaturated 7 g Cholesterol 74 g 25% Sodium 180 mg 8% Total Carbohydrates og 0% Dietary Fiber Og 0% Sugars 0 g Protein 21 g...

  • Estimation and Analysis of Daily Caloric Intake The purpose of this Laboratory Experience is to analyze...

    Estimation and Analysis of Daily Caloric Intake The purpose of this Laboratory Experience is to analyze your caloric intake to determine the number of calories and the percentage of these calories that come from fat, carbohydrates, and protein. Instructions You must record everything that you eat and drink for 2 days- a weekday and a weekend day. Try to select typical days that will reflect your true eating habits. Simply fill out the first two columns for each day on...

  • 6. You know the following nutritional information about a food: Carbohydrate - 8 grams, Fat -...

    6. You know the following nutritional information about a food: Carbohydrate - 8 grams, Fat - 8 grams, Saturated Fat - 4 grams, Sodium - 490 mg, Protein - 12 grams. Based on this information, estimate the number of kilocalories in this food. Show your work.

  • Hi there, this is due tonight and first one is question and the other one is...

    Hi there, this is due tonight and first one is question and the other one is info require for that question. Hope I can get it on time. Thank you! 3. Compare the percentages of total calories due to fat, carbohydrates, and protein found in soy milk with the percentages you calculated for one serving of peanut butter in the textbooks Critical Thinking box on page 1041. How do the percentages relate to the ideal percentages for optimum energy balance...

  • 1. A meal of pasta contains 16 grams protein, 80 grams Carb and 8grams fat. a....

    1. A meal of pasta contains 16 grams protein, 80 grams Carb and 8grams fat. a. How many Calories does the meal provide from these energy nutrients? (Remember Calories and kcals are the same thing) ________*___________= ________ kcal protein ________*___________ = ________kcal carb ________*___________ = ________kcal fat Total = _______ kcal b. What percentage of the Calories in the meal comes from each of the energy-yielding nutrients? (this relates to the second part of practice question 2) _____________________ = __________%...

  • nutrition Calculate the following using the information from the Starbucks Frappuccino. 1. What percent of calories...

    nutrition Calculate the following using the information from the Starbucks Frappuccino. 1. What percent of calories in the frappuccino come from carbohydrates? 2. What percent of calories come from fat? 3. What percent of calories come from protein? Nutrition Information Nutrition information is calculated based on our standard recipes. Only changing drink size will update this information. Other customizations will not Serving Size 16 fl oz Calories 240 Calories from Fat 30 Total Fat 3g Saturated Fat 2 g Trans...

  • how to calculate the nutrition label on the following: Label Reading Assignment Label Reading Worksheet Online...

    how to calculate the nutrition label on the following: Label Reading Assignment Label Reading Worksheet Online Updated 3 1doc You will be using this information to calculate the label worksheet Serving One Cookie: Total Fat 9 gm (Saturated Fat 6 gms, Trans Fat 0 gm) Cholesterol 0 mg Sodium 35 mg Total Carbohydrates 20 gm (Dietary Fiber 1 gm. Sugars 14 grm) Protein 1 g Ingredients List. Sugar, vegetable oil (palm oil with TBHQ for freshness. Partially hydrogenated palm kernel,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT