In a hypothetical calendar, there are 28 days in every month. The first day of e
ID: 3837449 • Letter: I
Question
In a hypothetical calendar, there are 28 days in every month. The first day of every month is always a Monday. Design a simple class definition for working with dates in this calendar. Store the day, month, and year as integers and add the following features:
Class Design:
Default constructor.
Parameterized constructor to initialize a date.
The following accessors (which should not be able to mutate the object)
getMonth()
getDay()
getYear()
A member function called print() to print the date in standard format mm/dd/yyyy. For example, today's date would print 05/08/2017. (function should not be able to change the object)
A mutator called changeDate(). This should have three inputs to update the day, month, and year. A mutator called nextDay() to change the current day to the next day in the week. If the next day would be larger than 28, this should change the month to the next month and set the day appropriately. If the current month is already December, this should change the year to the next year, and adjust the month and day appropriately .
A member function called isTuesday(). This member function should return true if the current day in the date object is a Tuesday, false otherwise. (should not be able to mutate the object)
Main()
Develop a main() function to test your Date class. Do the following in main():
a) You have two important dates coming up: (1) movie night on Friday 5/6 and (2) a huge concert on Saturday 5/7. Create two date objects movieNight and concertNight and construct them appropriately.
b) Your friend, who is a savvy programmer, texts you through your C++ console to let you know they aren’t free on movie night anymore, and want to meet 4 days later (than the original Friday, but your code should move the data 4 days later from whatever it already was). Advance your date for movieNight using the appropriate member functions.
c) You’re pretty busy with school on Tuesdays this semester. Check to see if movieNight now falls on a Tuesday. If it does: Print out a message that reads “Sorry, I can’t make it on Tuesday mm/dd/yyyy”. Since you are free on Wednesdays, move the movieNight date forward one day (with an appropriate member function), and print out a message that reads “Lets meet on Wednesday mm/dd/yyyy”. Submission Instructions: Upload a single .cpp file that includes your class design, all member functions, and your main() function to test. Your solution must be submitted in one file
Explanation / Answer
#include<iostream>
using namespace std;
class Date{
private:
int day;
int month;
int year;
public:
//default constructor
Date(){
day=1;
month=1;
year=2017;
}
//constructor to initialize date object
Date(int d, int m, int y){
day=d;
month=m;
year=y;
}
//to return month of the date object
int getMonth(){
return month;
}
//to return day of the date object
int getDay(){
return day;
}
////to return year of the date object
int getYear(){
return year;
}
//to print the date in mm/dd/yyyy format
void print(){
cout<<month<<"/"<<day<<"/"<<year;
}
// to modify the date object
void changeDate(int m, int d, int y){
month=m;
day=d;
year=y;
}
//to set the current day to the next day
void nextDay(){
day++;
// if day is greater than 28 change month
if(day>28){
day=day%28;
month++;
// if month is greater than 12 than change year
if(month>12){
month=month%12;
year++;
}
}
}
bool isTuesday(){
//Tuesday falls on second day of the week i.e. 2,9,16,23
if(day%7==2)
return true;
else
return false;
}
};
int main(){
//a. to initialize two date objects
Date movieNight=Date(5,5,2017);
Date concertNight=Date(6,5,2017);
//b. to shift the dates 4 days ahead
int i;
for(i=1;i<=4;i++)
movieNight.nextDay();
// c. to check if tuesday falls on that day and shift the dates accordingly
if(movieNight.isTuesday()){
cout<<"Sorry, I can't make it on Tuesday ";
movieNight.print();
movieNight.nextDay();
cout<<" Let's meet on Wednesday"<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.