Using the classes extDateType (Programming Exercise 8) and dayType (Chapter 10,
ID: 3665710 • Letter: U
Question
Using the classes extDateType (Programming Exercise 8) and dayType (Chapter 10, Programming Exercise 5), design the class calendarType so that, given the month and the year, we can print the calendar for that month. To print a monthly calendar, you must know the first day of the month and the number of days in that month. Thus, you must store the first day of the month, which is of the form dayType, and the month and the year of the calendar. Clearly, the month and the year can be stored in an object of the form extDateType by setting the day component of the date to 1 and the month and year as specified by the user. Thus, the class calendarType has two member variables: an object of the type dayType and an object of the type extDateType. Design the class calendarType so that the program can print a calendar for any month startingJanuary 1, 1500. Note that the day for January 1 of the year 1500 is a Monday. To calculate the first day of a month, you can add the appropriate days to Monday of January 1, 1500.
a) Add the appropriate constructors to initialize the member variables.
b) Determine the first day of the month for which the calendar will be printed. Call this operation firstDayOfMonth.
c) Set the month.
d) Set the year.
e) Return the month.
f) Return the year.
g) Print the calendar for the particular month.
h) Add the appropriate constructors to initialize the member variables.
i) Write the definitions of the member functions of the class calendarType to implement the operations of the class calendarType.
j) Write a test program to print the calendar for either a particular month or a particular year.
For my main.cpp I currently have:
#include <iostream>
#include "dayType.h"
#include "dateType.h"
#include "extDateType.h"
#include "calendarType.h"
using namespace std;
int main()
{
dateType d(1, 2, 1960);
extDateType ed(6, 10, 1981);
int num;
ed.printLongDate();
cout << endl;
ed.print();
cout << endl;
cout << "Days gone in the year: " << ed.numberOfDaysPassed();
cout << endl;
cout << "Days left in the Year: " << ed.numberOfDaysLeft() << endl;
cout << "Enter number of days to be added: ";
cin >> num;
cout << endl;
ed.incrementDate(num);
cout << "After increment date: ";
ed.print();
cout << endl;
//test day object
dayType newDay("Wednesday");
newDay.print();
cout << endl;
cout << "Next Day: " << newDay.nextDay() << endl;
newDay.setDay("Sunday");
cout << "Set Day: ";
newDay.print();
cout << endl;
newDay.prevDay();
cout << "Previous Day: " << newDay.prevDay() << endl;
newDay.addDay(12);
cout << "Add days: ";
newDay.print();
cout << endl;
//test calendar object
calendarType cal(9, 2015);
cal.printCalendar();
system("pause");
return 0;
}
This is the only code I have so far. My confusing is trying to put code in dayType.h and all the other headers and calling the info to be displayed in the int main().
Explanation / Answer
main.cpp
#include "calendarType.h"
#include <iostream>
using namespace std;
int main()
{
int month;
int year;
int SENTINEL = -9999;
int userSent = 0;
while (userSent != SENTINEL)
{
cout << "Please enter a month: ";
cin >> month;
cout << " ";
cout << "Please enter a year: ";
cin >> year;
cout << " ";
calendarType cal(month, year); //for 2014, first day for each month,
//6 is sunday
//9 is monday
//4 tuesday
//10 wednesday
//5 is thursday
//8 is friday
//11 is saturday
cal.printCalendar();
cout << "If you would like to continue, type 0 and hit enter. " << " " << "If you would like to quit, type -9999 and hit enter. ";
cin >> userSent;
cout << " ";
}
system("pause");
return 0;
}
calendarType.h
/*
* This class will create a calendar printout for any month and year after
* January 1, 1500.
*/
#ifndef calendarType_h
#define calendarType_h
#include "dateType.h"
#include "extDateType.h"
#include "dayType.h"
#include <iostream>
class calendarType
{
public:
void setMonth(int m);
void setYear(int y);
int getMonth();
int getYear();
void printCalendar();
calendarType();
calendarType(int m, int y);
private:
// Note that member functions can also be private which means only functions
// within this class can call them.
dayType firstDayOfMonth();
void printTitle();
void printDates();
// Composition rather than inheritance, although firstDate is a derived class
// from the base class dateType.
extDateType firstDate;
dayType firstDay;
};
#endif
dateType.h
#ifndef dateType_H
#define dateType_H
class dateType
{
public:
void setDate(int, int, int);
void setMonth(int);
void setDay(int);
void setYear(int);
void print() const;
int numberOfDaysPassed();
int numberOfDaysLeft();
void incrementDate(int nDays);
int getMonth() const;
int getDay() const;
int getYear() const;
int getDaysInMonth();
bool isLeapYear();
dateType(int = 1, int = 1, int = 1900);
private:
int dMonth;
int dDay;
int dYear;
};
#endif
dayType.h
#ifndef dayType_H
#define dayType_H
#include <string>
using namespace std;
class dayType
{
public:
static string weekDays[7];
void print() const;
string nextDay() const;
string prevDay() const;
void addDay(int nDays);
void setDay(string d);
string getDay() const;
dayType();
dayType(string d);
private:
string weekDay;
};
#endif
extDateType.h
#ifndef extDateType_H
#define extDateType_H
#include <string>
#include "dateType.h"
using namespace std;
class extDateType: public dateType
{
public:
static string eMonths[12];
void printLongDate();
void setDate(int, int, int);
void setMonth(int m);
void printLongMonthYear();
extDateType();
extDateType(int, int, int);
private:
string eMonth;
};
#endif
dateTypeImp.cpp
#include <iostream>
#include "dateType.h"
using namespace std;
void dateType::setDate(int month, int day, int year)
{
if (year >= 1)
dYear = year;
else
dYear = 1900;
if (1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
switch (dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (1 <= day && day <= 31)
dDay = day;
else
dDay = 1;
break;
case 4:
case 6:
case 9:
case 11:
if (1 <= day && day <= 30)
dDay = day;
else
dDay = 1;
break;
case 2:
if (isLeapYear())
{
if (1 <= day && day <= 29)
dDay = day;
else
dDay = 1;
}
else
{
if (1 <= day && day <= 28)
dDay = day;
else
dDay = 1;
}
}
}
void dateType::setMonth(int m)
{
dMonth = m;
}
void dateType::setDay(int d)
{
dDay = d;
}
void dateType::setYear(int y)
{
dYear = y;
}
void dateType::print() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getYear() const
{
return dYear;
}
int dateType::getDaysInMonth()
{
int noOfDays;
switch (dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
noOfDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
noOfDays = 30;
break;
case 2:
if (isLeapYear())
noOfDays = 29;
else
noOfDays = 28;
}
return noOfDays;
}
bool dateType::isLeapYear()
{
if (((dYear % 4 == 0) && (dYear % 100 != 0)) || dYear % 400 == 0)
return true;
else
return false;
}
dateType::dateType(int month, int day, int year)
{
setDate(month, day, year);
}
int dateType::numberOfDaysPassed()
{
int monthArr[13] = {0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int sumDays = 0;
int i;
for (i = 1; i < dMonth; i++)
sumDays = sumDays + monthArr[i];
if (isLeapYear() && dMonth > 2)
sumDays = sumDays + dDay + 1;
else
sumDays = sumDays + dDay;
return sumDays;
}
int dateType::numberOfDaysLeft()
{
if (isLeapYear())
return 366 - numberOfDaysPassed();
else
return 365 - numberOfDaysPassed();
}
void dateType::incrementDate(int nDays)
{
int monthArr[13] = {0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int daysLeftInMonth;
daysLeftInMonth = monthArr[dMonth] - dDay;
if (daysLeftInMonth >= nDays)
dDay = dDay + nDays;
else
{
dDay = 1;
dMonth++;
nDays = nDays - (daysLeftInMonth + 1);
while (nDays > 0)
if (nDays >= monthArr[dMonth])
{
nDays = nDays - monthArr[dMonth];
if ((dMonth == 2) && isLeapYear())
nDays--;
dMonth++;
if (dMonth > 12)
{
dMonth = 1;
dYear++;
}
}
else
{
dDay = dDay+nDays;
nDays = 0;
}
}
}
dayTypeImp.cpp
#include <iostream>
#include <string>
#include "dayType.h"
using namespace std;
string dayType::weekDays[7] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday"};
void dayType::print() const
{
cout << weekDay;
}
string dayType::nextDay() const
{
int i;
for (i = 0; i < 7; i++)
if (weekDays[i] == weekDay)
break;
return weekDays[(i + 1) % 7];
}
string dayType::prevDay() const
{
if (weekDay == "Sunday")
return "Saturday";
else
{
int i;
for (i = 0; i < 7; i++)
if (weekDays[i] == weekDay)
break;
return weekDays[i - 1];
}
}
void dayType::addDay(int nDays)
{
int i;
for (i = 0; i < 7; i++)
if (weekDays[i] == weekDay)
break;
weekDay = weekDays[(i + nDays) % 7];
}
void dayType::setDay(string d)
{
weekDay = d;
}
string dayType::getDay() const
{
return weekDay;
}
dayType::dayType()
{
weekDay = "Sunday";
}
dayType::dayType(string d)
{
weekDay = d;
}
extDateTypeImp.cpp
#include <iostream>
#include <string>
#include "dateType.h"
#include "extDateType.h"
using namespace std;
string extDateType::eMonths[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
void extDateType::printLongDate()
{
cout << eMonth << " " << getDay() << ", " << getYear();
}
void extDateType::printLongMonthYear()
{
cout << eMonth << " " << getYear();
}
void extDateType::setDate(int m, int d, int y)
{
dateType::setDate(m, d, y);
eMonth = eMonths[m - 1];
}
void extDateType::setMonth(int m)
{
dateType::setMonth(m);
eMonth = eMonths[m - 1];
}
extDateType::extDateType()
{
eMonth = "January";
}
extDateType::extDateType(int m, int n, int d)
: dateType(m, n, d)
{
eMonth = eMonths[m - 1];
}
calendarTypeImp.cpp
#include "calendarType.h"
//defaults constructor to January 1, 1500
calendarType::calendarType()
{
setMonth(1);
setYear(1500);
}
//sets input month and year
calendarType::calendarType(int m, int y)
{
setMonth(m);
setYear(y);
}
void calendarType::setMonth(int m)
{
firstDate.setMonth(m);
}
void calendarType::setYear(int y)
{
firstDate.setYear(y);
}
int calendarType::getMonth()
{
return firstDate.getMonth();
}
int calendarType::getYear()
{
return firstDate.getYear();
}
void calendarType::printCalendar()
{
printTitle();
printDates();
}
//private
void calendarType::printTitle()
{
firstDate.printLongMonthYear();
cout << " ";
for (int i = 0; i != 8; i++)
{
string shortName = firstDay.weekDays[i].substr(0, 3);
cout << " " << shortName; //4 spaces
}
}
void calendarType::printDates()
{
int i = 0;
dayType day = firstDayOfMonth();
if (day.getDay() == "Sunday")
{
while (firstDate.getDay() < firstDate.getDaysInMonth())
{
i = 0;
cout << " ";
while (i < 7)
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
}
cout << " ";
}
if (day.getDay() == "Monday")
{
// day specific loop
cout << " ";
for (int k = 0; k < 1; k++) //k boolean literal gets +1 per day of the week
{
cout << " " << " "; //to move one column right. Additional space in second quotes.
}
while (i < 6) //the literal is decremented as day of week moves further right in columns
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
//copypaste loop
while (firstDate.getDay() < firstDate.getDaysInMonth())
{
i = 0;
cout << " ";
while (i < 7)
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
}
cout << " ";
}
if (day.getDay() == "Tuesday")
{
// day specific loop
cout << " ";
for (int k = 0; k < 2; k++) //k boolean literal gets +1 per day of the week
{
cout << " " << " "; //to move one column right. Additional space in second quotes.
}
while (i < 5) //the literal is decremented as day of week moves further right in columns
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
//copypaste loop
while (firstDate.getDay() < firstDate.getDaysInMonth())
{
i = 0;
cout << " ";
while (i < 7)
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
}
cout << " ";
}
if (day.getDay() == "Wednesday")
{
// day specific loop
cout << " ";
for (int k = 0; k < 3; k++) //k boolean literal gets +1 per day of the week
{
cout << " " << " "; //to move one column right. Additional space in second quotes.
}
while (i < 4) //the literal is decremented as day of week moves further right in columns
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
//copypaste loop
while (firstDate.getDay() < firstDate.getDaysInMonth())
{
i = 0;
cout << " ";
while (i < 7)
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
}
cout << " ";
}
if (day.getDay() == "Thursday")
{
// day specific loop
cout << " ";
for (int k = 0; k < 4; k++) //k boolean literal gets +1 per day of the week
{
cout << " " << " "; //to move one column right. Additional space in second quotes.
}
while (i < 3) //the literal is decremented as day of week moves further right in columns
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
//copypaste loop
while (firstDate.getDay() < firstDate.getDaysInMonth())
{
i = 0;
cout << " ";
while (i < 7)
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
}
cout << " ";
}
if (day.getDay() == "Friday")
{
// day specific loop
cout << " ";
for (int k = 0; k < 5; k++) //k boolean literal gets +1 per day of the week
{
cout << " " << " "; //to move one column right. Additional space in second quotes.
}
while (i < 2) //the literal is decremented as day of week moves further right in columns
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
//copypaste loop
while (firstDate.getDay() < firstDate.getDaysInMonth())
{
i = 0;
cout << " ";
while (i < 7)
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
}
cout << " ";
}
if (day.getDay() == "Saturday")
{
// day specific loop
cout << " ";
for (int k = 0; k < 6; k++) //k boolean literal gets +1 per day of the week
{
cout << " " << " "; //to move one column right. Additional space in second quotes.
}
while (i < 1) //the literal is decremented as day of week moves further right in columns
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
//copypaste loop
while (firstDate.getDay() < firstDate.getDaysInMonth())
{
i = 0;
cout << " ";
while (i < 7)
{
if (firstDate.getDay() <= 9)
cout << " " << " " << firstDate.getDay(); //4 spaces + 2 for the day letters
else
cout << " " << " " << firstDate.getDay(); //4 spaces + 1 for two digit day
if (firstDate.getDay() < firstDate.getDaysInMonth())
{
firstDate.incrementDate(1);
}
i++;
}
}
cout << " ";
}
}
dayType calendarType::firstDayOfMonth()
{
dayType day;
int countResult;
int y = getYear();
int m = getMonth();
int d = 1;
if (firstDate.getMonth() == 1 && firstDate.getYear() == 1500)
{
day.setDay(firstDay.weekDays[1]);
}
else
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
countResult = (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
day.setDay(firstDay.weekDays[countResult]);
}
return day;
}
sample output
Please enter a month: 1
Please enter a year: 1500
January 1500
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.