Define a struct, fruit Type, to store the following data about a fruit: name (st
ID: 3765005 • Letter: D
Question
Define a struct, fruit Type, to store the following data about a fruit: name (string), color (string), fat (int), sugar (int), and carbohyderate (int). Write a program that declares a variable of type fruitType. prompts the user to input data about a fruit, and outputs the fruit data. Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type dayType: Set the day. Print the day. Return the day. Return the next day. Return the previous day. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday. Add the appropriate constructors. Write the definitions of the functions to implement the operations for the class dayType. Also, write a program to test various operations on this class.Explanation / Answer
These two below c++ programs written as per given problem statements and written the comments to understand the what code is doing ..see the below code for
Problem 1:
#include <iostream>
using namespace std;
struct fruitType
{
// Declaring fruitType parameters
private:
string name;
string color;
int fat;
int sugar;
int carbohyderate;
public:
//Default values of fruitType parameters
fruitType()
{
name = "Apple";
color = "Red";
fat = 100;
sugar = 20;
carbohyderate = 50;
}
//Getting values of fruitType parameters from console
void get_data()
{
cout<<"Enter Fruit Name: "<<endl;
cin>>name;
cout<<"Enter Fruit color: "<<endl;
cin>>color;
cout<<"Enter Fruit fat: "<<endl;
cin>>fat;
cout<<"Enter Fruit sugar: "<<endl;
cin>>sugar;
cout<<"Enter Fruit carbohyderate: "<<endl;
cin>>carbohyderate;
}
//Displaying values of fruitType parameters
void display()
{
cout<<"Fruit Name: "<<name<<endl;
cout<<"Fruit color: "<<color<<endl;
cout<<"Fruit fat: "<<fat<<endl;
cout<<"Fruit sugar: "<<sugar<<endl;
cout<<"Fruit carbohyderate: "<<carbohyderate<<endl;
}
};
int main()
{
// fruit Type object declaration
fruitType ft;
// Displaying Default value of fruitType
ft.display();
// Getting values of fruitType from console
ft.get_data();
// Displaying value of fruitType
ft.display();
return 1;
}
Problem 2:
#include <iostream>
#include <string>
using namespace std;
class dayType
{
public:
void setDay(string dayOfWeek);
void printDay() const;
string returnDay() const;
string returnNextDay() const;
string returnPreviousDay() const;
string returnDayByAdding(int num);
dayType();
private:
string dayList[7];
int numberOfDay; //variable to hold the index of array dayList
};
int main()
{
dayType myWeek;
string weekDay;
int increaseDay;
cout << "Today is ";
myWeek.printDay();
cout << "Enter the day you would like to begin with.";
cin >> weekDay;
myWeek.setDay(weekDay);
cout << "After setting the day to your desired day, the day is ";
myWeek.printDay();
cout << "The day of the week is " << myWeek.returnDay() << endl;
cout << "The next day is " << myWeek.returnNextDay() << endl;
cout << "Yesterday was " << myWeek.returnPreviousDay() << endl;
cout << "Enter the number of days to increase.";
cin >> increaseDay;
cout << "After " << increaseDay << " days, the day of the week is " << myWeek.returnDayByAdding(increaseDay) << endl;
return 0;
}
void dayType::setDay(string dayOfWeek)
{
for(int index=0;index<7;index++)
if(dayList[index]==dayOfWeek)
{
numberOfDay=index;
break;
}
}
void dayType::printDay() const
{
cout << dayList[numberOfDay] << endl;
}
string dayType::returnDay() const
{
return dayList[numberOfDay];
}
string dayType::returnNextDay() const
{
return dayList[numberOfDay+1];
}
string dayType::returnPreviousDay() const
{
return dayList[numberOfDay-1];
}
string dayType::returnDayByAdding(int num)
{
int tempDay=numberOfDay+num;
numberOfDay=tempDay%7;
return dayList[numberOfDay];
}
dayType::dayType()
{
dayList[0]="Sunday";
dayList[1]="Monday";
dayList[2]="Tuesday";
dayList[3]="Wednesday";
dayList[4]="Thursday";
dayList[5]="Friday";
dayList[6]="Saturday";
numberOfDay=0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.