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

C++ Programming - Data Structures Hello I need help completing this assignment.

ID: 3729474 • Letter: C

Question

C++ Programming - Data Structures Hello I need help completing this assignment. All help is appreciated! Here is the prompt:

In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap year. Moreover, write a test program to test your class. (Book: Data Structures Using C++, 2nd ed, D.S. Malik; Ch. 2; pg. 124 PE 2)

(This part was added by my professor) The class dateType was designed to implement the date in a program, but the member function setDate and the constuctor do not check whether the date is valid before storing the date in the date members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap year.

1. Make sure you name your class dateType

2. Make sure the mutator's signature is void setDate(const dateType&)

3. Make sure the accessor's signature is string getDate() const

4. Make sure is leapYear is a private function and its signature is bool isLeapYear() const;

Here is dateType.cpp: we were give this to complete the program.

//Implementation file date

#include <iostream>

#include "dateType.h"

using namespace std;

void dateType::setDate(int month, int day, int year)

{

dMonth = month;

dDay = day;

dYear = year;

}

int dateType::getDay() const

{

return dDay;

}

int dateType::getMonth() const

{

return dMonth;

}

int dateType::getYear() const

{

return dYear;

}

void dateType::printDate() const

{

cout << dMonth << "-" << dDay << "-" << dYear;

}

//Constructor with parameters

dateType::dateType(int month, int day, int year)

{

setDate(month, day, year);

}

Explanation / Answer

Code

dateType.h

#ifndef dateType_H
#define dateType_H

class dateType
{
public:
    void setDate(int month, int day, int year);

int getDay() const;

int getMonth() const;

int getYear() const;

void printDate() const;

void isLeapYear() const;

    dateType(int month = 1, int day = 1, int year = 1900);

private:

    int dMonth;

int dDay;  

int dYear;

};

#endif

dateType.cpp

#include <iostream>
#include "dateType.h"

using namespace std;

void dateType::setDate(int month, int day, int year)
{
while(month<1 || month>12)
   {
       cout << "Enterd month "<<month<< " is wrong"<<endl;
       cout << "Enter correct month"<<endl;
       cin>>month;
   }
   dMonth = month;
  
while(day<1 || day>31)
   {
       cout << "Enterd date "<<day<<" is wrong"<<endl;
       cout<<"Enter correct date"<<endl;
       cin>>day;
   }
   dDay = day;
   int count_digits = 0;
   int flag=0;
   int year1;
while(flag==0)
   {
       year1=year;
       count_digits=0;
       while (year) {
           year /= 10;
           count_digits++;
       }
       if(count_digits != 4)
       {
           cout << "Enterd year "<<year1<<" is wrong"<<endl;
           cout<<"Enter correct year"<<endl;
           cin>>year;
           flag=0;
       }
       else
           flag=1;
   }
   dYear = year1;
}
  

int dateType::getDay() const
{
    return dDay;
}

int dateType::getMonth() const
{
    return dMonth;
}

int dateType::getYear() const
{
    return dYear;
}

void dateType::printDate() const
{
    cout << dMonth << "-" << dDay<< "-" << dYear;
}

void dateType::isLeapYear() const
{
  
if ( dYear%400 == 0)
    cout<<endl<<dYear<< " is leap year. ";
else if ( dYear%100 == 0)
    cout<<endl<<dYear<< " is leap year. ";
else if ( dYear%4 == 0 )
    cout<<endl<<dYear<< " is leap year. ";
else
    cout<<endl<<dYear<< " is not leap year. ";

}

dateType::dateType(int month, int day, int year)
{
while(month<1 || month>12)
   {
       cout << "Enterd month "<<month<< " is wrong"<<endl;
       cout << "Enter correct month"<<endl;
       cin>>month;
   }
   dMonth = month;
  
while(day<1 || day>31)
   {
       cout << "Enterd date "<<day<<" is wrong"<<endl;
       cout<<"Enter correct date"<<endl;
       cin>>day;
   }
   dDay = day;
   int count_digits = 0;
   int flag=0;
   int year1;
while(flag==0)
   {
       year1=year;
       count_digits=0;
       while (year) {
           year /= 10;
           count_digits++;
       }
       if(count_digits != 4)
       {
           cout << "Enterd year "<<year1<<" is wrong"<<endl;
           cout<<"Enter correct year"<<endl;
           cin>>year;
           flag=0;
       }
       else
           flag=1;
   }
   dYear = year1;
}

main.cpp

#include<iostream>
#include "dateType.h"
using namespace std;
int main()
{
   dateType *dt1=new dateType();
   cout<<"Date is "<<endl;
   dt1->printDate();
   cout<<endl;
   dt1->isLeapYear();
   cout<<endl;
   dateType *dt2=new dateType(11,14,2019);
   cout<<"Date is "<<endl;
   dt2->printDate();
   cout<<endl;
   dt2->isLeapYear();
   cout<<endl;
   dt2->setDate(13,32,2016);
   cout<<"Date is "<<endl;
   dt2->printDate();
   cout<<endl;
   dt2->isLeapYear();
   cout<<endl;
   dt1->setDate(10,10,198);
   cout<<"Date is "<<endl;
   dt1->printDate();
   cout<<endl;
   dt1->isLeapYear();
   cout<<endl;
   system("pause");
   return 0;
}

If any queries regarding code and execution please get back to me

Thank You

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