Day of the Year Assuming that a year has 365 days, write a class named DayofYear
ID: 3537933 • Letter: D
Question
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 translate it to a 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 construct for the class should take a 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 variable of type string to assist in the translation from the integer format to the month-day format.
Test your class by inputting various integer representing days and printing out their representation in the month-day format.
This program is a good review of material you have study regarding Inheritance ( is the process by which a new class %u2013 known as a derived class %u2013 is created from another class, called the base class. A derived class automatically has all the member variable and all the ordinary member functions that the base class has, and can have additional member functions and additional member variables)
Explanation / Answer
#include<iostream>
#include <sstream>
using namespace std;
class DayofYear
{
private:
int day;
public:
static string day_month;
DayofYear(int k)
{
if(k>=1 and k<=365)
day = k;
else
{
cout << " invalid day entered " << endl;
day =0;
}
}
void print()
{
int monthDays[] ={31,59,90,120,151,181,212,243,273,304,334,365};
string monthName[] = { "January", "February", "March", "April","May", "June", "July", "August", "September", "October", "November", "December"};
if(day == 0)
cout << " Invalid day cant represent in day-month format";
else
{
int mon=0;
while (day > monthDays[mon])
{
++mon;
}
stringstream local;
local << ((mon>0? day - monthDays[mon - 1]:day));
day_month = monthName[mon] + " " + local.str();
//cout << monthName[mon] << " " << (mon>0? day - monthDays[mon - 1]:day);
cout << day_month << endl;
}
}
};
string DayofYear::day_month="";
int main()
{
DayofYear D1(2);
D1.print();
DayofYear D2(32);
D2.print();
DayofYear D3(365);
D3.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.