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

Programming Project #5 Date Design a class called Date. The class should store a

ID: 3671819 • Letter: P

Question

Programming Project #5 Date Design a class called Date. The class should store a specific date in three integers: month, day, and year. There should be member functions to print the date in the following four forms, if it is valid: 12/25/2012 (US) December 25, 2012 (US expanded) 25 December 2012 (US Military) 2012-12-25 (International) There should be member functions to get and/or set value of month, day, and year. There should be member functions to check for invalid date entered, such as 4/31/2012, or 2/29/2011, which will be rejected with an error message, and the program should ask for another date entered. Write a complete C++ program to demonstrate the class. Allow your program to run continuously as long as the user wishes. Input validation: Do not accept values for the day greater than 31 or less than 1. Do not accept values for the month greater than 12 or less than 1. Do not accept any invalid date. If it is, display the message “The date is invalid”. Required output (should be similar with the sample run) The program displays a valid date in three different formats. Note: All junk data will be rejected! Enter a date (mm/dd/yyyy) or -1 to end: 2/29/2008 Invalid date/ wrong format: For both month and day, use two digits. Re-Enter a date (mm/dd/yyyy) or -1 to end: 02/29/2009 2009 -> NOT Leap Year! 29 is not a valid day of Febraury Error!!! The entered date is invalid! Re-Enter, Please! Enter a date (mm/dd/yyyy) or -1 to end: 02/29/2008 2008 -> Leap Year! Date: 2/29/2008 is valid. 2/29/2008 (US). Febraury 29, 2008 (US Expanded). 29 Febraury 2008 (US Military). 2008-02-29 (International). Run again (y/n)? y The program displays a valid date in three different formats. Note: All junk data will be rejected! Enter a date (mm/dd/yyyy) or -1 to end: 4/31/1975 Invalid date/ wrong format: For both month and day, use two digits. Re-Enter a date (mm/dd/yyyy) or -1 to end: 04/31/1975 31 is not a valid day of April Error!!! The entered date is invalid! Re-Enter, Please! Enter a date (mm/dd/yyyy) or -1 to end: 04/30/1975 Date: 4/30/1975 is valid. 4/30/1975 (US). April 30, 1975 (US Expanded). 30 April 1975 (US Military). 1975-04-30 (International). Run again (y/n)? n Press key to end ...

Explanation / Answer

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

const int DAY = 1;
const int MONTH = 0;
const int YEAR = 2;


class Date {
private:
   int day;
   int month;
   int year;
public:
   Date(int d, int m, int y) {
       day = d;
       month = m;
       year = y;
   }

   Date() {
           day = 0;
           month = 0;
           year = 0;
       }

   int getDay() {
       return day;
   }

   void setDate(int d) {
       day = d;
   }

   int getMonth() {
       return month;
   }

   void setMonth(int m) {
       month = m;
   }

   int getYear() {
       return year;
   }

   void setYear(int y) {
       year = y;
   }

   bool leapYear(int year) {
       return (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);
   }

   bool validateDay(int dob[3]) {
       if (dob[DAY] < 1 || dob[DAY] > 31) {
           cout << "Enter a valid day" <<endl;
           return false;
       }
       if (dob[MONTH] < 1 || dob[MONTH] > 12) {
           cout << "Enter a valid month" <<endl;
           return false;
       }

       //30 days for jan,apr,jun,sep,nov , 31 days = mar, may, july, aug,oct, dec, 28/29 - feb
       if (dob[MONTH] == 1 || dob[MONTH] == 4 || dob[MONTH] == 6 || dob[MONTH] == 9
               || dob[MONTH] == 11) {
           if (dob[DAY] > 30) {
               cout << "Enter a valid day" <<endl;
               return false;
           }
       }

       if (dob[MONTH] == 2) {
           if (dob[DAY] > 29) {
               cout << "Enter a valid day (february)" <<endl;
               return false;
           }
           if (leapYear(dob[YEAR]) == false && dob[DAY] > 28) {
               cout << "NOT Leap Year! "<<dob[DAY]<<" is not a valid day of Febraury" <<endl;
               return false;
           }

       }
       return true;
   }

   void print() {

       string allMonths[]= {"January", "February", "March", "April",
                            "May", "June", "July", "August",
                            "September", "October", "November", "December"};
       string date = "";
       //cout << "Month: " << allMonths[2] <<endl;
       cout << day <<"/"<< month <<"/" << year << " (US) " <<endl;
       cout << allMonths[month-1] <<" "<< day <<"," << year << " (US expanded) " <<endl;
       cout << day <<" "<< allMonths[month-1] <<" " << year << " (US Military) " <<endl;

       cout << year <<"-"<< month <<"-" <<day << " (International) " <<endl;
   }

};


int main() {

   int day[3];
   char delim = '/';
   int i = 0;

   int exit = 0;
   char ch;
   while(exit >= 0) {
       cout << "Enter Date (MM/DD/YYYY), -1 to exit" << endl;
       for (i = 0; i < 3; i++) {
           cin >> day[i];
           if (i < 2)
               cin >> delim;
       }

       Date date = Date();
       bool valid = date.validateDay(day);
       if(valid){
           //set date
           date.setDate(day[DAY]);
           date.setMonth(day[MONTH]);
           date.setYear(day[YEAR]);
           date.print();
       }

       cout <<"Run again (y/n)" <<endl;
       cin >> ch;
       if(ch == 'n')
           break;
   }

   return 0;
}

---output---

Enter Date (MM/DD/YYYY), -1 to exit
02/29/2009
NOT Leap Year! 29 is not a valid day of Febraury
Run again (y/n)
y
Enter Date (MM/DD/YYYY), -1 to exit
02/29/2008
29/2/2008 (US)
February 29,2008 (US expanded)
29 February 2008 (US Military)
2008-2-29 (International)
Run again (y/n)
n