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

Design and implement a class DayType that implements the day of the week in a pr

ID: 3819874 • Letter: D

Question

Design and implement a class DayType that implements the day of the week in a program. The class 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:
1. Set the day.
2. Print the day.
3. Return the day.
4. Return the next day.
5. Return the previous day.
6. Calculate and return the day by adding certain days to the current day.
7. Calculate and return the day by subtracting certain days to the current day.

Add the appropriate constructors.

Then, write the definitions of the functions to implement the operations for the class DayType. Also, write a program to test all 7 operations on this class and display a menu to the user. The program should run until menu choice 8 (Exit) is chosen.

Explanation / Answer

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Day
{
string day;
public:
void printDay();
void setDay(string);
string next();
string prev();
};
void Day :: setDay(string b)
{
if(b=="Sun")
day="Sun";
if(b=="Mon")
day="Mon";
if(b=="Tue")
day="Tue";
if(b=="Wed")
day="Wed";
if(b=="Thu")
day="Thu";
if(b=="Fri")
day="Fri";
if(b=="Sat")
day="Sat";
}
void Day :: printDay()
{
cout<<"Day is : "<<day<<endl;
}
string Day :: next()
{
string n;
if(day=="Sun")
n="Mon";
if(day=="Mon")
n="Tue";
if(day=="Tue")
n="Wed";
if(day=="Wed")
n="Thu";
if(day=="Thu")
n="Fri";
if(day=="Fri")
n="Sat";
if(day=="Sat")
n="Sun";
return n;
}
string Day :: prev()
{
string p;
if(day=="Sun")
p="Sat";
if(day=="Mon")
p="Sun";
if(day=="Tue")
p="Mon";
if(day=="Wed")
p="Tue";
if(day=="Thu")
p="Wed";
if(day=="Fri")
p="Thu";
if(day=="Sat")
p="Fri";
return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
Day Try;
int a,d;
string b="Sun";
string c;
string z;
cout<<" 1 = Set Day 2 = Print Day 3 = Return Day 4 = Next Day 5 = Previous Day 6 = Add no. of days 7 = Subtract no. of days 8 = Exit"<<endl;
do
{
cin>>a;
if(a==1)
{
b="Sun";
Try.setDay(b);
cout<<"Enter Day : "<<endl;
cin>>b;
Try.setDay(b);
}
if(a==2)
{
Try.setDay(b);
Try.printDay();
}
if(a==3)
{
Try.setDay(b);
Try.printDay();
}
if(a==4)
{
Try.setDay(b);
z=Try.next();
cout<<"Tomorrow is : "<<z<<endl;
}
if(a==5)
{
Try.setDay(b);
z=Try.prev();
cout<<"Yesterday is : "<<z<<endl;
}
if(a==6)
{
cout<<"Enter of number of days to add : "<<endl;
cin>>d;
Try.setDay(b);
do
{
d=d-1;
Try.next();
}
while(d!=1);
z=Try.next();
cout<<"After adding "<<d<<" days , Day is : "<<z;
}
if(a==7)
{
cout<<"Enter of number of days to subtract : "<<endl;
cin>>d;
Try.setDay(b);
do
{
d=d-1;
Try.prev();
}
while(d!=1);
z=Try.prev();
cout<<"After subtracting "<<d<<" days , Day is : "<<z;
}
}
while(a!=8);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote