14.12 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 "Water" 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, amount of 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 a food item. 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 g 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 serving (s): 234.00 Number of calories for 3.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.
14.12 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 "Water" 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, amount of 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 a food item. 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 g 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 serving (s): 234.00 Number of calories for 3.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.
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
Related questions
Concept explainers
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
Question
![14.12 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
"Water" 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, amount of 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 a food item.
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 g
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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fa332979b-3709-48a2-942a-205e2ae2d053%2Fb9a3e05a-b900-49c6-865c-33052da93903%2Fxnb07vj_processed.png&w=3840&q=75)
Transcribed Image Text:14.12 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
"Water" 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, amount of 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 a food item.
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 g
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.
![1 class FoodItem:
2
3
4
5
6
7
8
9
0
5
8
#TODO: Define constructor with parameters to initialize instance
attributes (name, fat, carbs, protein)
#
1
2
3
4
5
6 if __name__
7
8
9
2
1
2
0
1
2
3
4
def get calories(self, num_servings):
#Calorie formula
calories = ((self. fat* 9) + (self.carbs * 4) + (self.protein * 4)) num_servings;
return calories
def print_info(self):
print(f'Nutritional information per serving of {self.name} :')
print(f' Fat: {self.fat:.2f} g')
print(f' Carbohydrates: {self.carbs:.2f} g')
print(f Protein: {self.protein: .2f} g')
==
"__main__'
__":
item_name = input()
if item_name == 'Water' or item_name == 'water':
food_item = FoodItem()
else:
food item.print_info()
print (f'Number of calories for {1.0:.2f} serving(s): {food_item.get_calories (1.0):.2f}')
*
amount_fat = float(input())
amount_carbs= float(input())
amount_protein = float(input())
num_servings = float(input())
food item FoodItem(item_name, amount_fat, amount_carbs, amount_protein)
food item.print_info()
print (f'Number of calories for {1.0:.2f} serving(s): {food_item.get_calories (1.0):.2f}')
print(f'Number of calories for {num_servings:.2f} serving(s): {food_item.get_calories(num_servings)](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fa332979b-3709-48a2-942a-205e2ae2d053%2Fb9a3e05a-b900-49c6-865c-33052da93903%2F4kxc8tg_processed.png&w=3840&q=75)
Transcribed Image Text:1 class FoodItem:
2
3
4
5
6
7
8
9
0
5
8
#TODO: Define constructor with parameters to initialize instance
attributes (name, fat, carbs, protein)
#
1
2
3
4
5
6 if __name__
7
8
9
2
1
2
0
1
2
3
4
def get calories(self, num_servings):
#Calorie formula
calories = ((self. fat* 9) + (self.carbs * 4) + (self.protein * 4)) num_servings;
return calories
def print_info(self):
print(f'Nutritional information per serving of {self.name} :')
print(f' Fat: {self.fat:.2f} g')
print(f' Carbohydrates: {self.carbs:.2f} g')
print(f Protein: {self.protein: .2f} g')
==
"__main__'
__":
item_name = input()
if item_name == 'Water' or item_name == 'water':
food_item = FoodItem()
else:
food item.print_info()
print (f'Number of calories for {1.0:.2f} serving(s): {food_item.get_calories (1.0):.2f}')
*
amount_fat = float(input())
amount_carbs= float(input())
amount_protein = float(input())
num_servings = float(input())
food item FoodItem(item_name, amount_fat, amount_carbs, amount_protein)
food item.print_info()
print (f'Number of calories for {1.0:.2f} serving(s): {food_item.get_calories (1.0):.2f}')
print(f'Number of calories for {num_servings:.2f} serving(s): {food_item.get_calories(num_servings)
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education