Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

C++ class 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

Day of the Year
Assuming that a year has 365 days, write a C++ class named DayOfYear that takes an integerrepresenting 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 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 strings that can be used
to assist in the translation from the integer format to the month-day format.
Test your class by inputting: various integers representing days and printing out their
representation in the month-day format.

Answer:




// Day of the year.
// (Assumes all years have 365 days)
#include <iostream>
#include <string>
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];
    }    
}


int main()
{
    // Tell user what program does
    cout << "This program converts a day given by a number 1 through 365" << 
            "\ninto a month and a day.";
    // Get user input         
    cout << "\nEnter a number: "; int day; cin >> day;
    if (day < 0 || day > 365)
    {
        cout << "Invalid range for a day.";
        exit(EXIT_FAILURE);
    }
    // Do computation and print result
    DayOfYear dy;
    dy.setDay(day);
    dy.print();            
    return 0;
}
                                  




About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!