Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Write a Day class to manipulate a day of the week. The Day class should h

ID: 3707050 • Letter: I

Question

In C++


Write a Day class to manipulate a day of the week. The Day class should have at least the following methods:

The Day class can have whatever private data you would like. You may choose to save the day as a string or as an integer.

Write a main program to test your Day class. It should prompt the user for a day and how many days later. Your program must have 2 variables of type Day – one to hold the original day and another to hold the calculated new day. Then print both days.

Here’s some pseudo-code to get you going for main():

Declare 2 Day variables (current and future)
Prompt user for string and int
Call method for current variable to set the day
Call method for current variable to return string N days later
Call method for future variable to set the day
Print final message – will need to call print method for both current and future variables

Sample Run #1: (the highlighted text is what the user types)

Day + N? Monday 3

Thursday is 3 days after Monday.

Explanation / Answer

The code in C++ for the above statement is:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

//create a class named Day
class Day
{
private:
int day_no;
string str_day;
public:
Day()
{
day_no = 0;
}
void setDay(string str_days)
{
str_day = str_days;
//set day number
if(str_day == "sunday")
{
day_no = 1;
}
else if(str_day == "monday")
{
day_no = 2;
}
else if(str_day == "tuesday")
{
day_no = 3;
}
else if(str_day == "wednesday")
{
day_no = 4;
}
else if(str_day == "thursday")
{
day_no = 5;
}
else if(str_day == "friday")
{
day_no = 6;
}
else if(str_day == "saturday")
{
day_no = 7;
}
}
string getDay()
{
return str_day;
}
int getDayNo()
{
return day_no;
}
void print_day()
{
cout<<str_day;
}
string Nday(int no)
{
day_no = day_no + no;
if(day_no > 7)
{
day_no %= 7;
}

switch(day_no)
{
case 1:
{
return "sunday";
}
case 2:
{
return "monday";
}
case 3:
{
return "tuesday";
}
case 4:
{
return "wednesday";
}
case 5:
{
return "thursday";
}
case 6:
{
return "friday";
}
case 7:
{
return "saturday";
}
}
return "error";
}
};
int main()
{
Day current, future;
string day, new_day;
int no;
cout<<"Day+N? ";
cin>>day;
cin>>no;
//to convert string to lower case
transform(day.begin(), day.end(), day.begin(), ::tolower);
current.setDay(day);
int day_status = current.getDayNo();
if(day_status > 0)
{
new_day = current.Nday(no);
}
future.setDay(new_day);

//display
cout<<future.getDay()<<" is "<<no<<" days after "<<current.getDay();
return 0;
}