In: Computer Science
CODE must using C++ language.
Write the definition of the class dayTye that implements the day of the week in a program. The class dayType should store the day of the week as integer. The program should perform the following operations on an object of type dayType
1. set the day
2. display the day as a string - Sunday, ... Saturday
3. return the day as an integer
4. return the next as an integer
5. return the previous day as an integer
6. calculate and return the day by adding to or subtracting from the current day. For example, if the current day is Monday (1) and we add 4 days the day returned is Friday (5). If today is Tuesday (2) and we subtract 13 days the day returned is Wednesday (3).
7. Add the appropriate constructors, accessors, mutators, and custom methods .
Write a main function to test your class in its own file. Do not put any class file the file that contains the main function.
Write a cpp file and an h file for your class in two files.
Upload the 2 files and the 1 h file.
If you have any doubts, please give me comment...
#ifndef DAYTYPE_H
#define DAYTYPE_H
#include<iostream>
#include<cmath>
using namespace std;
class dayType{
public:
dayType();
dayType(int day);
void setDay(int d);
int getDay() const;
void display();
int nextDay() const;
int previousDay() const;
int calculateDay(int days) const;
private:
int day;
};
#endif
dayType.cpp
#include "dayType.h"
dayType::dayType(){
day = 0;
}
dayType::dayType(int day){
this->day = day;
}
void dayType::setDay(int d){
day = d;
}
int dayType::getDay() const{
return day;
}
void dayType::display(){
if(day==0)
cout<<"Sunday";
else if(day==1)
cout<<"Monday";
else if(day==2)
cout<<"Tuesday";
else if(day==3)
cout<<"Wednesday";
else if(day==4)
cout<<"Thursday";
else if(day==5)
cout<<"Friday";
else
cout<<"Saturday";
}
int dayType::nextDay() const{
return (day+1)%7;
}
int dayType::previousDay() const{
return (7-day+1)%7;
}
int dayType::calculateDay(int days) const{
if(days>0)
return (day+days)%7;
else{
return (7-((int)(abs(day+days))%7))%7;
}
}
main.cpp
#include<iostream>
#include "dayType.h"
using namespace std;
int main(){
dayType d = dayType(1);
cout<<"Day 1 :";
d.display();
cout<<endl;
cout<<"Next Day to 1 is: "<<d.nextDay()<<endl;
cout<<"Previous Day to 1 is: "<<d.previousDay()<<endl;
cout<<"Calculate adding 4 days to day 1 is: "<<d.calculateDay(4)<<endl;
cout<<"Setting day to 2"<<endl;
d.setDay(2);
cout<<"Today is ";
d.display();
cout<<endl;
cout<<"Subtracting 13 days to day 2 is "<<d.calculateDay(-13)<<endl;
return 0;
}