10.16 LAB: Nutritional information (classes/constructors) Complete the FoodItem class by adding a constructor to initialize a food item. The constructor should initialize the name (a string) to "None" and all other instance attributes to 0.0 by default. If the constructor is called with a food name, grams of fat, grams of carbohydrates, and grams of protein, the constructor should assign each instance attribute with the appropriate parameter value. The given program accepts as input a food item name, fat, carbs, and protein and the number of servings. The program creates a food item using the constructor parameters' default values and a food item using the input values. The program outputs the nutritional information and calories per serving for both food items. 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 Use Python, please.

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

10.16 LAB: Nutritional information (classes/constructors)

 

Complete the FoodItem class by adding a constructor to initialize a food item. The constructor should initialize the name (a string) to "None" and all other instance attributes to 0.0 by default. If the constructor is called with a food name, grams of fat, grams of carbohydrates, and grams of protein, the constructor should assign each instance attribute with the appropriate parameter value.

The given program accepts as input a food item name, fat, carbs, and protein and the number of servings. The program creates a food item using the constructor parameters' default values and a food item using the input values. The program outputs the nutritional information and calories per serving for both food items.

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

 

Use Python, please.

LAB
10.16.1: LAB: Nutritional information (classes/constructors)
0/ 10
ACTIVITY
main.py
Load default template..
1 class FoodItem:
2
# TODO: Define constructor with parameters to initialize instance
#3
attributes (name, fat, carbs, protein)
4
def get_calories(self, num_servings):
# Calorie formula
calories = ((self.fat * 9) + (self.carbs * 4) + (self.protein * 4)) * num_servings;
5
6
7
8
return calories
9
def print_info(self):
print('Nutritional information per serving of {}:'.format(self.name))
print('
print('
print('
10
11
Fat: {:.2f} g'.format (self.fat))
Carbohydrates: {:.2f} g'.format(self.carbs))
Protein: {:.2f} g'.format(self.protein))
12
13
14
15
16 if
_name_
== "_main_":
17
18
food_item1 = FoodItem()
10
Transcribed Image Text:LAB 10.16.1: LAB: Nutritional information (classes/constructors) 0/ 10 ACTIVITY main.py Load default template.. 1 class FoodItem: 2 # TODO: Define constructor with parameters to initialize instance #3 attributes (name, fat, carbs, protein) 4 def get_calories(self, num_servings): # Calorie formula calories = ((self.fat * 9) + (self.carbs * 4) + (self.protein * 4)) * num_servings; 5 6 7 8 return calories 9 def print_info(self): print('Nutritional information per serving of {}:'.format(self.name)) print(' print(' print(' 10 11 Fat: {:.2f} g'.format (self.fat)) Carbohydrates: {:.2f} g'.format(self.carbs)) Protein: {:.2f} g'.format(self.protein)) 12 13 14 15 16 if _name_ == "_main_": 17 18 food_item1 = FoodItem() 10
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Class
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
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