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

Modify the DayOfYear class, written in Programming Challenge 2, to add a constru

ID: 3687692 • Letter: M

Question

Modify the DayOfYear class, written in Programming Challenge 2, to add a constructor that takes two parameters: a string object representing a month and an integer in the range 0 through 31 representing the day of the month. The constructor should then initialize the integer member of the class to represent the day specified by the month and day of month parameters. The constructor should terminate the program with an appropriate error message if the number entered for a day is outside the range of days for the month given.
Add the following overload 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.

Header Files: DayOfYear.h

*******************************************************************

#include <string>
using namespace std;

class DayOfYear
{
private:
   int day;
   string months[12];
   int endOfMonth[13];

   static string dayMonth;
   static int restDays;

public:
   DayOfYear(int d);
   void print();
   void setEndOfMonth();
   void setMonthName();

};

Main.cpp

*******************************************************************

#include <iostream>
#include <string>
#include "DayOfYear.h"

using namespace std;

int main()
{
   int dayNum;
   cout << "Please enter the day number in a year: " << endl;
   cin >> dayNum;

   DayOfYear d(dayNum);
   d.setEndOfMonth();
   d.setMonthName();
   d.print();

   system("pause");
   return 0;
}

DayOfYear.cpp

**************************************************************

#include <iostream>
#include <string>
#include "DayOfYear.h"

using namespace std;

//Initialize of static variables
string DayOfYear::dayMonth = "";
int DayOfYear::restDays = 0;

// constructor
DayOfYear::DayOfYear(int d)
{
   day = d;
}

// Create a function to set the end of each month in a year
void DayOfYear::setEndOfMonth()
{
   endOfMonth[0] = 0;
   endOfMonth[1] = 31;
   endOfMonth[2] = 59;
   endOfMonth[3] = 90;
   endOfMonth[4] = 120;
   endOfMonth[5] = 151;
   endOfMonth[6] = 181;
   endOfMonth[7] = 212;
   endOfMonth[8] = 243;
   endOfMonth[9] = 273;
   endOfMonth[10] = 304;
   endOfMonth[11] = 334;
   endOfMonth[12] = 365;
}

// Create a function to name each month in a year
void DayOfYear::setMonthName()
{
   months[0] = "January";
   months[1] = "February";
   months[2] = "March";
   months[3] = "April";
   months[4] = "May";
   months[5] = "June";
   months[6] = "July";
   months[7] = "August";
   months[8] = "September";
   months[9] = "October";
   months[10] = "November";
   months[11] = "December";
}

void DayOfYear::print()
{
   int month = 0;
   while (endOfMonth[month]        month++;

   dayMonth += months[month -1 ];
   restDays += day- endOfMonth[month - 1];

   cout << "The corresponding day is: " << dayMonth << " " << restDays << endl;
}

Explanation / Answer

DayOfYear.h

DayOfYear.cpp


#include "DayOfYear.h"
#include <iostream>
#include <stdlib.h>

const int DayOfYear::monthDays[] = { 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::DayOfYear(string m, int d){
    month = m;
    day = d;
    for (int i=0; i<12; i++){
        if (m==monthName[0]){
            numDays = 0;
            break;
        }          
        if (m==monthName[i])
            numDays = monthDays[i-1];
    }
    if (d<1 || d>31)
        exit(0);
    numDays += d;  
}
DayOfYear::DayOfYear(){
    month = "January";
    day = 1;
    numDays =1;
}

DayOfYear::~DayOfYear() {
}

void DayOfYear::setNumDays(int d)
{
    numDays = d;
}

int DayOfYear::getNumDays()
{
    return numDays;
}

void DayOfYear::print()
{
    int m = 0;
    while (monthDays[m] < numDays)
        m = (m + 1) % 12;
    if (numDays < 32)
        cout << monthName[m] << " " << numDays << endl;
    else
        cout << monthName[m] << " " << numDays - monthDays[m - 1] << endl;
}

DayOfYear& DayOfYear::operator ++(){
    ++numDays;
    if (numDays>365)
        numDays = 1;
    return *this;
}

DayOfYear DayOfYear::operator ++(int){
    DayOfYear temp;
    temp.numDays++;
    if (temp.numDays>365)
        temp.numDays = 1;
    return temp;
}

DayOfYear& DayOfYear::operator --(){
    --numDays;
    if (numDays==0)
        numDays = 365;
    return *this;
}

DayOfYear DayOfYear::operator --(int){
    DayOfYear temp;
    temp.numDays--;
    if (temp.numDays==0)
        temp.numDays = 365;
    return temp;
}


DayOfYear.h

#ifndef DAYOFYEAR_H
#define   DAYOFYEAR_H

#include <string>
using namespace std;

class DayOfYear
{
public:
    DayOfYear();
    DayOfYear(string, int);
    virtual ~DayOfYear();
    void setNumDays(int);
    int getNumDays();
    void print();
    static const string monthName[];
    static const int monthDays[];
  
    DayOfYear& operator++(); //prefix
    DayOfYear& operator--();
    DayOfYear operator++(int); //postfix
    DayOfYear operator--(int);
private:
    int day;
    string month;
    int numDays;
};

#endif   /* DAYOFYEAR_H */


main.cpp

//System Libraries
//System Libraries
#include <iostream>
#include "DayOfYear.h"

using namespace std;

//User Libraries

//Global Constants

//Function Prototypes

//Execution begins here
int main(int argc, char** argv) {
    string m;
    int d;
    cout << "Please enter a month: ";
    cin >> m;
    cout << "Please enter a day of the month: ";
    cin >> d;
    DayOfYear date1(m,d);
    DayOfYear date2;
    date2 = date1--;
    date2.print();
    return 0;
}

sample output

Please enter a month: 31                                                                                                                                    
Please enter a day of the month: January                                                                                                                    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote