Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Inheritance (Chapter 11, Program 2, page 740) Day of the Year Assuming that a ye

ID: 3631419 • Letter: I

Question

Inheritance (Chapter 11, Program 2, page 740)

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 – known as a derived class – 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)

Now , I am giving you starting point for this program, you can use this segment and finish it or start your code from scratch. Good luck and let me know if you have any question.





// Day of the year.

// This program takes an integer representing a day of the Year and translates it to an description of the form Month - day of month.

// Assumes all years have 365 days



#include

#include



using namespace std;



class DayOfYear

{

public: static const int daysAtEndOfMonth[ ];

static const string monthName[ ];

void print();

void setDay(int day){this->day = day;}

private: int day;

};



const int DayOfYear::daysAtEndOfMonth[ ] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

const string DayOfYear::monthName[ ]= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",

"November", "December"};



//****************************************************

// DayOfYear::print. Convert and print day of year *

//****************************************************

void DayOfYear::print()

{

int month = 0;



while (daysAtEndOfMonth[month] < day)

month = (month + 1) %12;

// DaysAtEndOfMonth >= day

if (month == 0)

cout << "January " << day;

else

cout << DayOfYear::monthName[month] << " " << day - DayOfYear::daysAtEndOfMonth[month-1];

}

//=================================== Main Program ==============================================

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.";

// Get user input

cout << " Enter a number: ";

int day;

cin >> day;

if (day < 0 || day > 365)

{ cout << "Invalid range for a day.";

exit(1);

}

// Do computation and print result

DayOfYear dy;

dy.setDay(day);

dy.print();

return 0;

}

Explanation / Answer

//DayOfYear.h

#include <iostream>

#include <string>

using namespace std;

#ifndef DAYOFYEAR_H

#define DAYOFYEAR_H

class DayOfYear

{

private:

   static const string monthName[];

   static const int monthDays[];

   int dayOfYear;

   int dayOfMonth(int month) const;  

public:

   friend ostream &operator<<(ostream &strm, const DayOfYear &rhs);

   DayOfYear():dayOfYear(0) {};

   DayOfYear(int dayOfYear);

   ~DayOfYear() {};

   void print() const;

};

#endif

//DayOfYear.cpp

#include "DayOfYear.h"

//---------------------------------------------------------------------------

// const string monthName[]: Month name corresponding with monthDays

//---------------------------------------------------------------------------

const string DayOfYear::monthName[] = { "January", "February", "March", "April",

   "May", "June", "July", "August", "September", "October", "November", "December"};


//---------------------------------------------------------------------------

// const int monthDays[]: Sum of the days in the specific month and previous months

//---------------------------------------------------------------------------

const int DayOfYear::monthDays[] = { 31, 59, 90, 120, 151, 181,

                                     212, 243, 273, 304, 334, 365};


//---------------------------------------------------------------------------

// 1-argument constructor

// Prevent input out of range (less than 1 or greater than 365)

//---------------------------------------------------------------------------

DayOfYear::DayOfYear(int dayOfYear)

:dayOfYear(dayOfYear)

{

   if (this->dayOfYear < 1 || this->dayOfYear > 365)  

   {

      this->dayOfYear = 0;

   }

}

//---------------------------------------------------------------------------

// operator<<: To check object's state

//---------------------------------------------------------------------------

ostream &operator<<(ostream &strm, const DayOfYear &rhs)

{

   strm << "dayOfYear: " << rhs.dayOfYear;

   return strm;

}

//---------------------------------------------------------------------------

// int dayOfMonth(int): Return the day of month

// month: 0 for Jan, 1 for Feb, ..., 11 for Dec

// dayOfMonth of Feb, Mar, ..., Dec = dayOfYear - monthDays of Jan, Feb, ..., Nov

// dayOfMonth of Jan = dayOfYear

//---------------------------------------------------------------------------

int DayOfYear::dayOfMonth(int month) const

{

   return month > 0 ? dayOfYear - monthDays[month - 1] : dayOfYear;

}

//---------------------------------------------------------------------------

// void print(): Print out in the month-day format through stdcout

// dayOfYear == 0: either a default value or an incorrect (out of range) value,

//    return an error message

// dayOfYear in range [1..365]: find int month appropriate for dayOfYear -

//    has monthDays[month-1] < dayOfYear <= monthDays[month]

// Increment month until dayOfYear is less than or equal to monthDays[month]

// month cannot be greater than 11 since dayOfYear's max value is 365

//---------------------------------------------------------------------------

void DayOfYear::print() const

{

   if (dayOfYear == 0)

   {

      cerr << "Not a day of year." << endl;

   }

   else

   {

      int month = 0;

      while (dayOfYear > monthDays[month])

      {

         ++month;

      }

      cout << monthName[month] << " " << dayOfMonth(month)

           << endl;

   }

}

//TestDayOfYear.cpp

#include <iostream>

#include "DayOfYear.h"

using namespace std;

int main()

{

   cout << "Begin main() - TestDayOfYear" << endl << endl;

   //Test constructors

   cout << "Test default constructor:" << endl;

   DayOfYear testDefault;

   cout << testDefault << endl << endl;

   cout << "Test 1-argument constructor:" << endl;

   DayOfYear test1(2);

   DayOfYear test2(32);

   DayOfYear test3(365);

   cout << test1 << endl;

   cout << test2 << endl;

   cout << test3 << endl;

   //Test DayOfYear::print()

   cout << endl << "Test print() method:" << endl;

   testDefault.print();

   test1.print();

   test2.print();

   test3.print();

   //Test other dayOfYear values

   cout << endl << "Test dayOfYear from 1-31 (Jan):" << endl;

   for (int i = 1; i < 32; i++)

   {

      DayOfYear(i).print();

   }

   cout << endl << "Test dayOfYear from 32-59 (Feb):" << endl;

   for (int i = 32; i < 60; i++)

   {

      DayOfYear(i).print();

   }

   cout << endl << "Test dayOfYear from 60-90 (Mar):" << endl;

   for (int i = 60; i < 91; i++)

   {

      DayOfYear(i).print();

   }

   cout << endl << "Test dayOfYear from 91-120 (Apr):" << endl;

   for (int i = 91; i < 121; i++)

   {

      DayOfYear(i).print();

   }

   cout << endl << "Test dayOfYear from 335-365 (Dec):" << endl;

   for (int i = 335; i < 366; i++)

   {

      DayOfYear(i).print();

   }

   cout << endl << "End - Normal Termination" << endl << endl;

   return 0;

}

//Main.cpp

#include <iostream>

#include "DayOfYear.h"

using namespace std;

int main()

{

   //LOCAL DECLARATIONS

   int input = -1;

   cout << " This program translates an integer representing a day of the"

        << " year to a string consisting of the month followed by day of"

        << " the month." << endl << endl;

   cout << "Enter 0 to exit." << endl << endl;

   while (input != 0)

   {

      cout << "Please enter a day of the year (between 1 and 365): ";

      cin >> input;

      if (input != 0)

      {

         DayOfYear real(input);

         real.print();

         cout << endl;

      }

   }

   cout << "End - Normal Termination." << endl;

   cin.get();

   cin.get();

   return 0;

}