14.02-03) Day of the Year Assuming that a year has 365 days, write a class named
ID: 3773668 • Letter: 1
Question
14.02-03)
Day of the Year
Assuming that a year has 365 days, write a class named DayOfYear that takes an integer representing a day of the year and translates it to a string consisting of the month followed by day of the month. For example,
Day 2 would be January 2.
Day 32 would be February 1. Day 365 would be December 31.
The first constructor for the class should take as parameter an integer representing the day of the year, and the class should have a member function print() that prints the day in the month–day format. The class should have an integer member variable to represent the day and should have static member variables holding string objects that can be used to assist in the translation from the integer format to the month-day format. The second constructor should take two parameters : a string object representing a month and aninteger in the range 0 through 31 representing the day of the month. The constructor should then initilize the integer member of theclass , which ranges from 1 to 365 for the days of the year, from the month parameter and day of the month parameter . The firstconstructor should terminate the program with an appropriate error message if an integer less then 1 or greater then 365 is entered. The second constructor should terminate the program if they month day entered is greater then the number of days in that month.
Add the following overloaded operators :
++ prefix and postfix increment operators . These operators should modify the DayOfYear object so that it represents the next day. If the day is already the end of the year, the new value of the object will represent the first day of the year.
-- prefix and postfix decrement operators . These operators should modify the DayOfYear object so that it represents the previous day. If the day is already the first day of the year, the new value of the object will represent the last day of the year.
Test your class by inputting various integers representing days and printing out their representation in the month–day format.
Explanation / Answer
DayOfYear.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class DayOfYear
{
public:
DayOfYear(int n);
DayOfYear(string month, int n);
static const int daysAtMonthEnd[];
static const string nameOfMonth[];
void print();
void print(string month, int n);
DayOfYear &DayOfYear::operator++();
DayOfYear &DayOfYear::operator--();
private:
int day;
};
DayOfYear,cpp
#include "DayOfYear.h"
const int DayOfYear::daysAtMonthEnd[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
const string DayOfYear::nameOfMonth[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
DayOfYear::DayOfYear(int day)
{
if (day < 1 || day>365)
{
cout << "Invalid Day entered" << endl;
exit(1);
}
this->day = day;
}
DayOfYear::DayOfYear(string month, int dayOfMonth)
{
int i = 0;
while (month != nameOfMonth[i] && i < 12)
{
i++;
}
if (i == 0)
{
if (dayOfMonth > daysAtMonthEnd[0])
{
cout << "The day entered is greater than the number of day in the month " + month << endl;
return;
}
day = dayOfMonth;
}
else
{
if (i == 12)
{
cout << "Invalid Month" << endl;
return;
}
else if (dayOfMonth > (daysAtMonthEnd[i] - daysAtMonthEnd[i - 1]))
{
cout << "The day entered is greater than the number of day in the month " + month << endl;
exit(1);
}
else
{
day = daysAtMonthEnd[i - 1] + dayOfMonth;
}
}
}
void DayOfYear::print()
{
int month = 0;
while (daysAtMonthEnd[month] < day && month < 12)
{
month++;
}
if (month == 0)
{
cout << DayOfYear::nameOfMonth[month] << " " << day << endl;
}
else
{
cout << DayOfYear::nameOfMonth[month] << " " << day - DayOfYear::daysAtMonthEnd[month - 1] << endl;
}
}
//Overloading the ++operator to return next day
DayOfYear &DayOfYear::operator++()
{
if (day == 365)
{
day = 1;
}
else
{
++day;
}
return *this;
}
//Overloading the --operator to return previous day
DayOfYear &DayOfYear::operator--()
{
if (day == 1)
{
day = 365;
}
else
{
--day;
}
return *this;
}
Main.cpp(To test the DayOfYear class)
#include<iostream>
#include "DayOfYear.h"
using namespace std;
int main()
{
// Tell user what program does
cout << "This program converts a day given by a number 1 through 365" << " into a month and a day."<<endl;
// Get user input
cout << "Enter a number: " << endl;
int day;
cin >> day;
// Do computation and print result
DayOfYear dy(day);
dy.print();
dy++;
dy.print();
DayOfYear doy("January", 15);
doy.print();
doy--;
doy.print();
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.