Design and implement a class dayType that implements the day of the week in a pr
ID: 3699405 • Letter: D
Question
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: a. Set the day. b. Print the day c. Return the day d. Return the next day. e. 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 appropriatc constructors. Write the definitions of the functions to implement the operations for the claos dayTypa. Also, write a program to test various operations on this classExplanation / Answer
#include<stdio.h>
using namespace std;
class dayType
{
public:
char day[4];
dayType() //Considering the day as Sun by default
{
day[0] = 'S';
day[1] = 'u';
day[2] = 'n';
day[3] = '';
}
dayType(char d[3]) //Constructor for initializing the day
{
for(int i = 0; i < 3; i++)
day[i] = d[i];
day[3] = '';
}
void setDay(char d[3]) //To set the day by a function
{
for(int i = 0; i < 3; i++)
day[i] = d[i];
day[3] = '';
}
void printDay() //Print the day
{
for(int i = 0; i < 3; i++)
printf("%c", day[i]);
}
char* returnDay() //Return the day
{
return(day);
}
char* returnNextDay() //Return the next day
{
if(day[0] == 'S' && day[1] == 'u')
return("Mon"); //For Sun, next day is Mon
else if(day[0] == 'M')
return("Tue"); //For Mon, next day is Tue
else if(day[0] == 'T' && day[1] == 'u')
return("Wed");
else if(day[0] == 'W') //etc.
return("Thu");
else if(day[0] == 'T')
return("Fri");
else if(day[0] == 'F')
return("Sat");
else
return("Sun");
}
char* returnNextDay(char d[3]) //This function is used only to find the next day if a day is given as a pparameter
{
if(d[0] == 'S' && d[1] == 'u')
return("Mon");
else if(d[0] == 'M')
return("Tue");
else if(d[0] == 'T' && d[1] == 'u')
return("Wed");
else if(d[0] == 'W')
return("Thu");
else if(d[0] == 'T')
return("Fri");
else if(d[0] == 'F')
return("Sat");
else
return("Sun");
}
char* returnPrevDay() //Return the previous day
{
if(day[0] == 'S' && day[1] == 'u')
return("Sat"); //For Sun, previous day is Sat
else if(day[0] == 'M')
return("Sun"); //For Mon, previous day is Sun
else if(day[0] == 'T' && day[1] == 'u')
return("Mon"); //etc.
else if(day[0] == 'W')
return("Tue");
else if(day[0] == 'T')
return("Wed");
else if(day[0] == 'F')
return("Thu");
else
return("Fri");
}
char* returnAfterDays(int n) //Returns the day after n days
{
if(n%7 == 0) //After 7*x day is same day
return(day);
else if(n%7 == 1) //After 7*x + 1 day is the next day
return(this->returnNextDay());
else if(n%7 == 2) //And so on.
return(this->returnNextDay(this->returnNextDay()));
else if(n%7 == 3)
return(this->returnNextDay(this->returnNextDay(this->returnNextDay())));
else if(n%7 == 4)
return(this->returnNextDay(this->returnNextDay(this->returnNextDay(this->returnNextDay()))));
else if(n%7 == 5)
return(this->returnNextDay(this->returnNextDay(this->returnNextDay(this->returnNextDay(this->returnNextDay())))));
else
return(this->returnNextDay(this->returnNextDay(this->returnNextDay(this->returnNextDay(this->returnNextDay(this->returnNextDay()))))));
}
};
int main()
{
dayType ob; //day is set to Sun by the constructor
ob.setDay("Tue"); //day is set to Tue by the function
printf(" Day is (using printDay()): ");
ob.printDay();
printf(" Day is (using returnDay()): %s", ob.returnDay());
printf(" Next day is: %s", ob.returnNextDay());
printf(" Prev day is: %s", ob.returnPrevDay());
printf(" The day after 13 days is: %s", ob.returnAfterDays(13));
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.