Using the classes extDateType from the Chapter 11 class projects and dayType fro
ID: 3870665 • Letter: U
Question
Using the classes extDateType from the Chapter 11 class projects and dayType from the Chapter 10 class projects, design the class calendarType so that, given the month and the year, you 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. Your calendarType class will have two private instance variables: firstDay of type dayType to hold the first day of the month and date of type extDateType to hold the month and year for the calendar. The calendarType object must use as much of the functionality of its object variables as possible. In fact, the only functionality required of calendarType is to determine the first day of the month and to actually print the calendar. Here is the UML diagram for this class:
Note that the return type of the member function firstDayOfMonth is dayType. This return value should be stored in the firstDay variable.
Design the class calendarType so that the program can print a calendar for any month starting January 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.
Write a test driver which uses a loop to produce output similar to the following:
Turn in all your header files, all your implementation files, and your test program. Also turn in one or more screen shots showing the results of your testing.
Once this project is completed you will demonstrate that you are able to:
Call the constructor of an aggregate member
Access the functions of an aggregate member
Apply the functionality of aggregate object members to minimize coding effort in a project
Utilize a member function that returns an object
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
/*A Function that returns the index of the day
Index Day
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday*/
int Number_of_day(int day, int month, int year)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1,
4, 6, 2, 4 };
year -= month < 3;
return ( year + year/4 - year/100 +
year/400 + t[month-1] + day) % 7;
}
/*
A Function that returns the name of the month
Month N0. Name
0 January
1 February
2 March
3 April
4 May
5 June
6 July
7 August
8 September
9 October
10 November
11 December */
string getMonthName(int monthNumber)
{
string months[] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (months[monthNumber]);
}
/* A Function to return the number of days in
a month
Month Number Name No. of Days
0 January 31
1 February 28 / 29
2 March 31
3 April 30
4 May 31
5 June 30
6 July 31
7 August 31
8 September 30
9 October 31
10 November 30
11 December 31
*/
int numberOfDays (int monthNumber, int year)
{
// January
if (monthNumber == 0)
return (31);
// February
if (monthNumber == 1)
{
// If the year is leap then February has
// 29 days
if (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0))
return (29);
else
return (28);
}
// March
if (monthNumber == 2)
return (31);
// April
if (monthNumber == 3)
return (30);
// May
if (monthNumber == 4)
return (31);
// June
if (monthNumber == 5)
return (30);
// July
if (monthNumber == 6)
return (31);
// August
if (monthNumber == 7)
return (31);
// September
if (monthNumber == 8)
return (30);
// October
if (monthNumber == 9)
return (31);
// November
if (monthNumber == 10)
return (30);
// December
if (monthNumber == 11)
return (31);
}
// Function to print the calendar of the given year
void printCalendar(int month,int year)
{
printf (" Calendar - %d ", year);
int days;
// Index of the day from 0 to 6
int current = Number_of_day (1, 1, year);
// i --> Iterate through all the months
// j --> Iterate through all the days of the
// month - i
for (int i = 0; i < 1; i++)
{
days = numberOfDays (month, year);
// Print the current month name
printf(" ------------%s------------- ",
getMonthName (month).c_str());
// Print the columns
printf(" Sun Mon Tue Wed Thu Fri Sat ");
// Print appropriate spaces
int k;
for (k = 0; k < current; k++)
printf(" ");
for (int j = 1; j <= days; j++)
{
printf("%5d", j);
if (++k > 6)
{
k = 0;
printf(" ");
}
}
if (k)
printf(" ");
current = k;
}
return;
}
// Driver Program to check above funtions
int main()
{
int year,month;
cout<<"month: ";
cin>>month;
cout<<"year: ";
cin>>year;
printCalendar(month,year);
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.