iven main(), complete the FoodItem class (in file Foodltem.java) with constructors to initialize each food item. The default constructor hould initialize the name to "Water" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of at, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value. x: If the input is: Water he 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 x: If the input is: M&M's 10.0 34.0 2.0 3.0 here 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 umber 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 ote: 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 utputs the calories for one serving of water. ublic 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)
iven main(), complete the FoodItem class (in file Foodltem.java) with constructors to initialize each food item. The default constructor hould initialize the name to "Water" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of at, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value. x: If the input is: Water he 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 x: If the input is: M&M's 10.0 34.0 2.0 3.0 here 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 umber 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 ote: 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 utputs the calories for one serving of water. ublic 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)
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter11: Inheritance And Composition
Section: Chapter Questions
Problem 6PE
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
Java
Input the following constructors
![### Implementing Constructors in Java: FoodItem Class
**Objective:**
Given the `main()` method, complete the `FoodItem` class (in the file `FoodItem.java`) with constructors to initialize each food item.
#### Instructions:
1. **Default Constructor:** Initializes the name to "Water" and all other fields to 0.0.
2. **Parameterized Constructor:** Takes 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.
---
### Example 1:
**Input:**
```
Water
```
**Output:**
```plaintext
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
```
### Example 2:
**Input:**
```
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.
**Output:**
```plaintext
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.
---
### Code Implementation:
```java
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)
}
```
**Action Items:**
- Define the default constructor.
- Define the parameterized constructor to initialize the private fields: `name`, `fat`, `carbs`, and `](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F4d420fdb-3f62-4ae1-a708-ec27fc9bcf4e%2Feed00c86-5a47-4c0f-944d-3a9f465a1a5e%2F8d95hev_processed.png&w=3840&q=75)
Transcribed Image Text:### Implementing Constructors in Java: FoodItem Class
**Objective:**
Given the `main()` method, complete the `FoodItem` class (in the file `FoodItem.java`) with constructors to initialize each food item.
#### Instructions:
1. **Default Constructor:** Initializes the name to "Water" and all other fields to 0.0.
2. **Parameterized Constructor:** Takes 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.
---
### Example 1:
**Input:**
```
Water
```
**Output:**
```plaintext
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
```
### Example 2:
**Input:**
```
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.
**Output:**
```plaintext
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.
---
### Code Implementation:
```java
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)
}
```
**Action Items:**
- Define the default constructor.
- Define the parameterized constructor to initialize the private fields: `name`, `fat`, `carbs`, and `
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 2 steps with 1 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
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![Microsoft Visual C#](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,