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

implement the Date class and test its functionality. Consider the following clas

ID: 3880997 • Letter: I

Question

implement the Date class and test its functionality.
Consider the following class declaration for the class date:
class Date
{
public:
Date(); //default constructor; sets m=01, d=01, y =0001
Date(unsigned m, unsigned d, unsigned y); //explicit-value constructor to set date equal to today's
//date. Use 2-digits for month (m) and day (d), and 4-digits for year (y); this function should
//print a message if a leap year.
void display();//output Date object to the screen
int getMonth();//accessor to output the month
int getDay();//accessor to output the day
int getYear();//accessor to output the year
void setMonth(unsigned m);//mutator to change the month
void setDay(unsigned d);//mutator to change the day
void setYear(unsigned y);//mutation to change the year
friend ostream & operator<<(ostream & out, const Date & dateObj);//overloaded operator<< as a friend
function with chaining
//you make add other functions if necessary
private:
int myMonth, myDay, myYear; //month, day, and year of a Date obj respectively
};
You will implement all the constructors and member functions in the class Date. Please see the
comments that follow each function prototype in the Date class declaration above; these comments
describe the functionality that the function should provide to the class.
Please store the class declaration in the file “date.h” and the class implementation in the file
“date.cpp”, and the driver to test the functionality of your class in the file “date_driver.cpp”.
Below I have provided a skeleton with stubs and a driver to help you get started. Remember to
separate the skeleton into the appropriate files, and to include the appropriate libraries.

Notes:
1. ALL PROGRAMS SHOULD BE COMPILED USING MS VISUAL STUDIO C++!
2. Information on Month: 1 = January, 2 = February, 3= March, …, 12 = December
3. Test the functionality of your class in “date_driver.cpp” in the following order and include
messages for each test:
a. Test default constructor
b. Test display
c. Test getMonth
d. Test getDay
e. Test getYear
f. Test setMonth
g. Test setDay
h. Test setYear
4. See sample output below.
5. See skeleton below.
S A M P L E O U T P U T FOR Assignment#2
Default constructor has been called
01/01/0001
Explicit-value constructor has been called
12/31/1957
Explicit-value constructor has been called
Month = 15 is incorrect
Explicit-value constructor has been called
2/29/1956
This is a leap year
Explicit-value constructor has been called
Day = 30 is incorrect
Explicit-value constructor has been called
Year = 0000 is incorrect
Explicit-value constructor has been called
Month = 80 is incorrect
Day = 40 is incorrect
Year = 0000 is incorrect
12/31/1957
12
31
1957
myDate: 11/12/2015 test2Date: 02/29/1956 yourDate: 12/31/1957
Skeleton FOR Assignment#2
#include <iostream>
#include <string>
//#include "date.h"
using namespace std;
//*********************************************************************************************
//*********************************************************************************************
// D A T E . h
//This declaration should go in date.h
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(); //default constructor; sets m=01, d=01, y =0001
Date(unsigned m, unsigned d, unsigned y); //explicit-value constructor to set date equal to today's
//date. Use 2-digits for month (m) and day (d), and 4-digits for year (y); this function should
//print a message if a leap year.
void display();//output Date object to the screen
int getMonth();//accessor to output the month
int getDay();//accessor to output the day
int getYear();//accessor to output the year
void setMonth(unsigned m);//mutator to change the month
void setDay(unsigned d);//mutator to change the day
void setYear(unsigned y);//mutation to change the year
friend ostream & operator<<(ostream & out, const Date & dateObj);//overloaded operator<< as a friend
function with chaining
//you make add other functions if necessary
private:
int myMonth, myDay, myYear; //month, day, and year of a Date obj respectively
};
#endif
//*********************************************************************************************
//*********************************************************************************************
// D A T E . C P P
//This stub (for now) should be implemented in date.cpp
//*************************************************************************************
//Name: Date
//Precondition: The state of the object (private data) has not been initialized
//Postcondition: The state has been initialized to today's date
//Description: This is the default constructor which will be called automatically when
//an object is declared. It will initialize the state of the class
//
//*************************************************************************************
Date::Date()
{
//the code for the default constructor goes here
}
//*************************************************************************************
//Name: Date
//Precondition:
//Postcondition:
//Description:
//
//
//*************************************************************************************
Date::Date(unsigned m, unsigned d, unsigned y)
{
}
//*************************************************************************************
//Name: Display
//Precondition:
//Postcondition:
//Description:
//
//
//*************************************************************************************
void Date::display()
{
}
//*************************************************************************************
//Name: getMonth
//Precondition:
//Postcondition:
//Description:
//
//
//*************************************************************************************
int Date::getMonth()
{
return 1;
}
//*************************************************************************************
//Name: getDay
//Precondition:
//Postcondition:
//Description:
//
//
//*************************************************************************************
int Date::getDay()
{
return 1;
}
//*************************************************************************************
//Name: getYear
//Precondition:
//Postcondition:
//Description:
//
//
//*************************************************************************************
int Date::getYear()
{
return 1;
}
//*************************************************************************************
//Name: setMonth
//Precondition:
//Postcondition:
//Description:
//
//
//*************************************************************************************
void Date::setMonth(unsigned m)
{
}
//*************************************************************************************
//Name: setDay
//Precondition:
//Postcondition:
//Description:
//
//
//*************************************************************************************
void Date::setDay(unsigned d)
{
}
//*************************************************************************************
//Name: getYear
//Precondition:
//Postcondition:
//Description:
//
//
//*************************************************************************************
void Date::setYear(unsigned y)
{
}
ostream & operator<<(ostream & out, const Date & dateObj)
{
return out;
}
//*********************************************************************************************
//*********************************************************************************************
// D A T E D R I V E R . C P P
//EXAMPLE OF PROGRAM HEADER
/********************************************************************************************
Name: Z#:
Course: Date Structures and Algorithm Analysis (COP3530)
Professor: Dr. Lofton Bullard
Due Date: Due Time:
Total Points: 100
Assignment 3: Date program
Description:
*********************************************************************************************/
int main()
{
//Date myDate;
//Date yourDate(12,31, 1957);
//Date test1Date(15, 1, 1962); //should generate error message that bad month
//Date test2Date(2, 29, 1956); //ok, should say leep year
//Date test3Date(2, 30, 1956); //should generate error message that bad day
//Date test4Date(12,31,0000); //should generate error message that bad year
//Date test5Date(80,40,0000); //should generate error message that bad month, day and year

//yourDate.display();
//cout<<yourDate.getMonth()<<endl;
//cout<<yourDate.getDay()<<endl;
//cout<<yourDate.getYear()<<endl;
//myDate.setMonth(11);
//myDate.setDay(12);
//myDate.setYear(2015);
//cout<<"myDate: "<<myDate<<" test2Date: "<<test2Date<<" yourDate: "<<yourDate<<endl;
return 0;
}

sample code:

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

        int day = 1, month = 1, year = 1;

        cout << setfill('0') << setw(2) << day << endl;

        cout << setfill('0') << setw(2) << month << endl;

        cout << setfill('0') << setw(4) << year << endl;

        return 0;

}

Explanation / Answer

Date_Driver.cpp
-----------------------------------------------------------------------
#include "date.h"

int main()
{
   Date myDate;
   Date yourDate(12,31, 1957);
   Date test1Date(15, 1, 1962); //should generate error message that bad month
   Date test2Date(2, 29, 1956); //ok, should say leep year
   Date test3Date(2, 30, 1956); //should generate error message that bad day
   Date test4Date(12,31,0000); //should generate error message that bad year
   Date test5Date(80,40,0000); //should generate error message that bad month, day and year
   yourDate.display();
   cout<<yourDate.getMonth()<<endl;
   cout<<yourDate.getDay()<<endl;
   cout<<yourDate.getYear()<<endl;
   myDate.setMonth(11);
   myDate.setDay(12);
   myDate.setYear(2015);
   cout << "myDate: "<< myDate << " test2Date: " << test2Date << " yourDate: "<< yourDate << endl;
   return 0;
}
-----------------------------------------------------------------------------------------------------------------
date.cpp
-----------------------------------------------
#include "date.h"

//*************************************************************************************
//Name: Date
//Precondition: The state of the object (private data) has not been initialized
//Postcondition: The state has been initialized to 01/01/0001
//Description: This is the default constructor which will be called automatically when
//an object is declared. It will initialize the state of the class
//*************************************************************************************
Date::Date()
{
   cout << "Default constructor has been called. ";
   myDay = myMonth = myYear = 1;
   display();
}

//*************************************************************************************
//Name: Date
//Precondition: The state of the object (private data) has not been initialized to today's date
//Postcondition: The state has been initialized to today's date.
//Description: This is the explicit value constructor which initializes the date to
// today's date.
//*************************************************************************************
Date::Date(unsigned m, unsigned d, unsigned y)
{
   cout << "Explicit-value constructor has been called. ";
   myMonth = m;
   myDay = d;
   myYear = y;
   display();  
}
//*************************************************************************************
//Name: Display
//Precondition: The contents have not been printed to the screen
//Postcondition: The contents have been printed to the screen
//Description: Prints the date if valid or specificies whether the month, day, or year is invalid
// Also prints whether or not it is a leap year.
//*************************************************************************************
void Date::display()
{
   if (getMonth() == -1 || getDay() == -1 || getYear() == -1)
   {
       if (getMonth() == -1)
       {
           cout << "Month = " << setfill('0') << setw(2) << myMonth << " is incorrect. ";
       }
       if (getDay() == -1)
       {
           cout << "Day = " << setfill('0') << setw(2) << myDay << " is incorrect. ";
       }
       if (getYear() == -1)
       {
           cout << "Year = " << setfill('0') << setw(4) << myYear << " is incorrect. ";
       }
       cout << endl << endl;
   }
   else if (getMonth() == myMonth && getDay() == myDay && getYear() == myYear)
   {
       cout << setfill('0') << setw(2) << myMonth << "/";
       cout << setfill('0') << setw(2) << myDay << "/";
       cout << setfill('0') << setw(4) << myYear << endl;
       if (getLeapYear() == 1)
       {
           cout << "This is a leap year. ";
       }
       cout << endl << endl;
   }
}

//*************************************************************************************
//Name: getMonth
//Precondition: Has not checked to see if the month is valid or not
//Postcondition: Has checked to see if the month is valid
//Description: If the month is valid returns the month, else it returns -1
//*************************************************************************************
int Date::getMonth()
{
   if (myMonth > 0 && myMonth < 13)
   {
       return myMonth;
   }
   else
       return -1;
}

//*************************************************************************************
//Name: getDay
//Precondition: Day has not been checked for validity.
//Postcondition: The day has been checked for validity.
//Description: Checks the day to see if it is a valid date and returns myDate if valid and -1 if not.
//*************************************************************************************
int Date::getDay()
{
   if (myDay < 1)
   {
       return -1;
   }
   else if ((myMonth == 1 || myMonth == 3 || myMonth == 5 || myMonth == 7 || myMonth == 8 || myMonth == 10 || myMonth == 12
) && myDay < 32)
   {
       return myDay;
   }
   else if (myMonth == 2 && myDay < 29 && getLeapYear() == -1)
   {
       return myDay;
   }
   else if (myMonth == 2 && myDay < 30 && getLeapYear() == 1)
   {
       return myDay;
   }
   else if ((myMonth == 4 || myMonth == 6 || myMonth == 9 || myMonth == 11) && myDay < 31)
   {
       return myDay;
   }
   else if (getMonth() == -1 && myDay < 32)
   {
       //month is incorrect but day COULD be correct so return day
       return myDay;
   }
   else
   {
       return -1;
   }
}

//*************************************************************************************
//Name: getYear
//Precondition: Year has not been checked for validity.
//Postcondition: Year has been checked for validity.
//Description: Checks the year to see if it is greater than 0. If not returns -1;
//*************************************************************************************
int Date::getYear()
{
   if (myYear > 0)
   {
       return myYear;
   }
   else
   {
       return -1;
   }
}

//*************************************************************************************
//Name: getLeapYear
//Precondition: Year has not been checked to see if it is a leap year.
//Postcondition: Year has been checked to for leap year status.
//Description: If the year is a leap year this will return 1, otherwise -1.
//*************************************************************************************
int Date::getLeapYear()
{
   if (myYear % 4 == 0)
   {
       if (myYear % 100 == 0)
       {
           if (myYear % 400 == 0)
           {
               return 1;
           }
           else
           {
               return -1;
           }
       }
       return 1;
   }
   return -1;
}

//*************************************************************************************
//Name: setMonth
//Precondition: The user has not set the value of the month
//Postcondition: Month has been set to the value the user has given
//Description: Takes an input month from the user and sets the month to that value if valid
//*************************************************************************************
void Date::setMonth(unsigned m)
{
   myMonth = m;

}

//*************************************************************************************
//Name: setDay
//Precondition: The user has not set the value of the day
//Postcondition: day has been set to the value the user has given
//Description: Takes an input day from the user and sets the day to that value if valid
//*************************************************************************************
void Date::setDay(unsigned d)
{
   myDay = d;
}

//*************************************************************************************
//Name: setYear
//Precondition: The user has not set the value of the year
//Postcondition: year has been set to the value the user has given
//Description: Takes an input year from the user and sets the year to that value if valid
//*************************************************************************************
void Date::setYear(unsigned y)
{
   myYear = y;
}

/****************************************************************************************************************************/
//Name: operator<<
//Precondition: Extraction operator has not been overloaded
//Postcondition: Extraction operator has been ovreloaded
//Decription: Friend function that overloads the extraction operator to print the date.
/***************************************************************************************************************************/
ostream & operator<<(ostream & out, const Date & dateObj)
{  
       out << setfill('0') << setw(2) << dateObj.myMonth << "/";
       out << setfill('0') << setw(2) << dateObj.myDay << "/";
       out << setfill('0') << setw(4) << dateObj.myYear;
  
   return out;
}
--------------------------------------------------------------------------------------------------------------------
date.h
---------------------------------------------------------------------
#ifndef DATE_H
#define DATE_H

#include <iostream>
#include <iomanip>
using namespace std;

class Date
{
public:
   Date(); //default constructor; sets m=01, d=01, y =0001
   Date(unsigned m, unsigned d, unsigned y); //explicit-value constructor to set date equal to today's
   //date. Use 2-digits for month (m) and day (d), and 4-digits for year (y); this function should //print a message if a leap year.
   void display();//output Date object to the screen
   int getMonth();//accessor to output the month
   int getDay();//accessor to output the day
   int getYear();//accessor to output the year
   int getLeapYear(); //Checks if a year is a leap year
   void setMonth(unsigned m);//mutator to change the month
   void setDay(unsigned d);//mutator to change the day
   void setYear(unsigned y);//mutation to change the year
   friend ostream & operator<<(ostream & out, const Date & dateObj);//overloaded operator<< as a friend function with chaining
   //you may add other functions if necessary
private:
   int myMonth, myDay, myYear; //month, day, and year of a Date obj respectively
};
#endif

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