Question

In: Computer Science

7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp)...

7.26 LAB: Nutritional information (classes/constructors)

Given main(), complete the FoodItem class (in files FoodItem.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
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

__________________________________________________________

Given Code:

main.cpp

#include "FoodItem.h"
#include <stdio.h>
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
FoodItem FoodItem1;

string itemName;
double amountFat, amountCarbs, amountProtein;

cin >> itemName;
cin >> amountFat;
cin >> amountCarbs;
cin >> amountProtein;

FoodItem FoodItem2 = FoodItem(itemName, amountFat, amountCarbs, amountProtein);

double numServings;
cin >> numServings;

FoodItem1.PrintInfo();
printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
FoodItem1.GetCalories(numServings));
cout << endl << endl;

FoodItem2.PrintInfo();
printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
FoodItem2.GetCalories(numServings));

return 0;
}

______________________________________________________________
FoodItem.h

#ifndef FOODITEMH
#define FOODITEMH

#include <string>

using namespace std;

class FoodItem {
public:
// TODO: Declare default constructor

// TODO: Declare second constructor with arguments
// to initialize private data members

string GetName();

double GetFat();

double GetCarbs();

double GetProtein();

double GetCalories(double numServings);

void PrintInfo();

private:
string name;
double fat;
double carbs;
double protein;
};

#endif

_____________________________________________________________

FoodItem.cpp

#include "FoodItem.h"
#include <stdio.h>

// Define default constructor

// Define second constructor with arguments
// to initialize private data members

string FoodItem::GetName() {
return name;
}

double FoodItem::GetFat() {
return fat;
}

double FoodItem::GetCarbs() {
return carbs;
}

double FoodItem::GetProtein() {
return protein;
}

double FoodItem::GetCalories(double numServings) {
// Calorie formula
double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings;
return calories;
}

void FoodItem::PrintInfo() {
printf("Nutritional information per serving of %s:\n", name.c_str());
printf(" Fat: %.2f g\n", fat);
printf(" Carbohydrates: %.2f g\n", carbs);
printf(" Protein: %.2f g\n", protein);
}

Solutions

Expert Solution

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IF YOU WANT ANY CHANGES REGARDING THE SOLUTION OR IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CODE TO COPY

main.cpp

#include "FoodItem.h"
#include <stdio.h>
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
FoodItem FoodItem1;

string itemName;
double amountFat, amountCarbs, amountProtein;

cin >> itemName;
cin >> amountFat;
cin >> amountCarbs;
cin >> amountProtein;

FoodItem FoodItem2 = FoodItem(itemName, amountFat, amountCarbs, amountProtein);

double numServings;
cin >> numServings;

FoodItem1.PrintInfo();
printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
FoodItem1.GetCalories(numServings));
cout << endl << endl;

FoodItem2.PrintInfo();
printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
FoodItem2.GetCalories(numServings));

return 0;
}

FoodItem.cpp

#include "FoodItem.h"
#include <stdio.h>

FoodItem::FoodItem()
{
   name = "None";
   fat = 0.0;
   carbs = 0.0;
   protein = 0.0;
}

FoodItem::FoodItem(string itemName,double amountFat,double amountCarbs,double amountProtein){
   name = itemName;
   fat = amountFat;
   carbs = amountCarbs;
   protein = amountProtein;
}

string FoodItem::GetName() {
return name;
}

double FoodItem::GetFat() {
return fat;
}

double FoodItem::GetCarbs() {
return carbs;
}

double FoodItem::GetProtein() {
return protein;
}

double FoodItem::GetCalories(double numServings) {
// Calorie formula
double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings;
return calories;
}

void FoodItem::PrintInfo() {
printf("Nutritional information per serving of %s:\n", name.c_str());
printf(" Fat: %.2f g\n", fat);
printf(" Carbohydrates: %.2f g\n", carbs);
printf(" Protein: %.2f g\n", protein);
}

FoodItem.h

#ifndef FOODITEMH
#define FOODITEMH

#include <string>

using namespace std;

class FoodItem {
public:
   FoodItem();
   FoodItem(string itemName,double amountFat,double amountCarbs,double amountProtein);
  
   string GetName();
   double GetFat();
   double GetCarbs();
   double GetProtein();
   double GetCalories(double numServings);
   void PrintInfo();
private:
   string name;
   double fat;
   double carbs;
   double protein;
};

#endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

EXPLANATION

IN FoodItem.h

FoodItem();//declaring no argument construtoe

FoodItem(string itemName,double amountFat,double amountCarbs,double amountProtein); // declaring arguments and their datatype along with their name

In FoodItem.cpp

FoodItem::FoodItem() // CREATING DEFINITION TO THE NO ARGUMENT CONSTRUCTOR
{
   name = "None"; // assigning name as NONE
   fat = 0.0; // assigning all other variables as 0.0
   carbs = 0.0;
   protein = 0.0;
}

//creating definition to argumented constructor

FoodItem::FoodItem(string itemName,double amountFat,double amountCarbs,double amountProtein){
   name = itemName; //assigning the name
   fat = amountFat; // assigning the fat
   carbs = amountCarbs; // assigning the carbs
   protein = amountProtein; //assigning the fat
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IF YOU HAVE ANY DOUBTS REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Related Solutions

7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp)...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors...
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set...
Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set and get the purchase price of a car (SetPurchasePrice(), GetPurchasePrice()), and to output the car's information (PrintInfo()). Ex: If the input is: 2011 18000 2018 where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is: Car's information: Model year: 2011 Purchase price: 18000 Current value: 5770 Note: printInfo() should use three spaces for...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the...
9.11 LAB: Winning team (classes) Complete the Team class implementation. For the class method get_win_percentage(), the formula is: team_wins / (team_wins + team_losses) Note: Use floating-point division. Ex: If the input is: Ravens 13 3 where Ravens is the team's name, 13 is the number of team wins, and 3 is the number of team losses, the output is: Congratulations, Team Ravens has a winning average! If the input is Angels 80 82, the output is: Team Angels has a...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int main() { SinglyLinkedList<std::string> list; list.Add("Hello"); list.Print(); list.Add("Hi"); list.Print(); list.InsertAt("Bye",1); list.Print(); list.Add("Akash"); list.Print(); if(list.isEmpty()){ cout<<"List is Empty "<<endl; } else{ cout<<"List is not empty"<<endl; } cout<<"Size = "<<list.Size()<<endl; cout<<"Element at position 1 is "<<list.get(1)<<endl; if(list.Contains("X")){ cout<<"List contains X"<<endl; } else{ cout<<"List does not contain X"<<endl; } cout<<"Position of the word Akash is "<<list.IndexOf("Akash")<<endl; cout<<"Last Position of the word Akash is "<<list.LastOf("Akash")<<endl; list.RemoveElement("Akash"); cout<<"After removing Akash...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class,...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 Sortedlist.java import java.util.Scanner; public class SortedList { public static void main (String[] args)...
10.5 LAB: Air-traffic control (queue using a linked list) Given a partial main() and PlaneQueue class,...
10.5 LAB: Air-traffic control (queue using a linked list) Given a partial main() and PlaneQueue class, write the push() and pop() methods for PlaneQueue. Then complete the main() to read in whether flights are arriving or have landed at an airport. An "arriving" flight is pushed onto the queue. A "landed" flight is popped from the front of the queue. Output the queue after each plane is pushed or popped. Entering -1 exits the program. Ex: If the input is:...
First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT