Question

In: Computer Science

C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in...

C++ problem 11-2

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 member variables. 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 member variables. Add a member function, isLeapYear, to check whether a year is a leap year. Moreover, write a test program to test your class.

Input should be format month day year with each separated by a space.

for dateType.h

//dateType.h

#ifndef date_H

#define date_H

class dateType

{

public:

    void setDate(int month, int day, int year);

      int getDay() const;

      int getMonth() const;

    int getYear() const;

    void printDate() const;

    bool isLeapYear();

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

private:

int dMonth;      

    int dDay;        

    int dYear;       

};

#endif

---------------------------------------

For dateTypeImp.cpp

#include"dateType.h"

class dateType{

   private:

   int month;

   int day;

   int year;

  

    public:

   

   dateType(){

   }

   

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

       int maxNumberOfDays[]={31,28,31,30,31,30,31,31,30,31,30,31};

       if(month < 1 || month > 12){

           cout<<"Date :"<<month<<"-"<<day<<"-"<<year;

           cout<<" Month is not valid"<<endl;

       }

       if(day >= 1 && day <= maxNumberOfDays[month]){

       }

       

       else if(month == 2 && day == 29){

           if(isLeapYear() == 1){

              

           }

           else{

               cout<<"Date :"<<month<<"-"<<day<<"-"<<year;

               cout<<" Day is not valid for this month"<<endl;

           }

       }

       else{

           cout<<"Date :"<<month<<"-"<<day<<"-"<<year;

           cout<<" Day is not valid for this month"<<endl;

       }

       if(year < 0){

           cout<<"Date :"<<month<<"-"<<day<<"-"<<year;

           cout<<" year is not valid"<<endl;

       }

       this->month = month;

       this->day = day;

       this->year = year;

   }

   int isLeapYear(){

       int year = this->year;

       

       if((year % 400) == 0){

           return 1;

       }

      

      

       else if((year % 100) == 0){

           return 0;

       }

       else if((year%4) == 0)

           return 1;

       // else

       return 0;

   }

   void setDate(int month,int day,int year){

       

       int maxNumberOfDays[]={31,28,31,30,31,30,31,31,30,31,30,31};

      

       

       if(month < 1 || month > 12){

           cout<<"Date :"<<month<<"-"<<day<<"-"<<year;

           cout<<" Month is not valid"<<endl;

       }

       

       if(day >= 1 && day <= maxNumberOfDays[month]){

       }

       

       else if(month == 2 && day == 29){

           if(isLeapYear() == 1){

               

           }

           else{

               cout<<"Date :"<<month<<"-"<<day<<"-"<<year;

               cout<<" Day is not valid for this month"<<endl;

           }

       }

       else{

           cout<<"Date :"<<month<<"-"<<day<<"-"<<year;

           cout<<" Day is not valid for month="<<month<<endl;

       }

       if(year < 0){

           cout<<"Date :"<<month<<"-"<<day<<"-"<<year;

           cout<<" year is not valid"<<endl;

       }

       this->month = month;

       this->day = day;

       this->year = year;  

   }

   string getDate(){

       return (to_string(this->month)+"-"+to_string(this->day)+"-"+to_string(this->year));

   }

};

--------------------------------------------------------

for main.cpp

#include"dateTypeImp.cpp"

int main(){

   int month;

   int day;

   int year;

   cin>>month>>day>>year;

   dateType Date1(month,day,year);

  

   cout<<"Date 1:"<<Date1.getDate();

   int isLeap = Date1.isLeapYear();

   if(isLeap == 1){

       cout<<" this is a Leap Year"<<endl;

   }

   else{

       cout<<" this is not a Leap Year"<<endl;

   }

   cin>>month>>day>>year;

   dateType Date2;

   Date2.setDate(month,day,year);

   cout<<"Date 2:"<<Date2.getDate();

   isLeap = Date2.isLeapYear();

   if(isLeap == 1){

       cout<<" this is a Leap Year"<<endl;

   }

   else{

       cout<<" this is not a Leap Year"<<endl;

   }

   cin>>month>>day>>year;

   dateType Date3(month,day,year);

   cin>>month>>day>>year;

   dateType Date4;

   Date4.setDate(month,day,year);

  

   cin>>month>>day>>year;

   dateType Date5(month,day,year);

  

   return 0;

}

Solutions

Expert Solution

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

#include <bits/stdc++.h>

using namespace std;

  

bool checkYear(int year)

{

    // If a year is multiple of 400,

    // then it is a leap year

    if (year % 400 == 0)

        return true;

  

    // Else If a year is muliplt of 100,

    // then it is not a leap year

    if (year % 100 == 0)

        return false;

  

    // Else If a year is muliplt of 4,

    // then it is a leap year

    if (year % 4 == 0)

        return true;

    return false;

}

  

// Driver code

int main()

{

    int year = 2000;

  

    checkYear(year) ? cout << "Leap Year":

                      cout << "Not a Leap Year";

    return 0;

}

Kindly revert for any queries

Thanks.


Related Solutions

Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date...
Programming Exercise 11-2 QUESTION: 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 member variables. 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 member variables. Add a member function, isLeapYear, to check...
Review the General Programming Assignment instructions. In this chapter, the class dateType was designed to implement...
Review the General Programming Assignment instructions. 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 member variables. 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 member variables. Add a member function, isLeapYear,...
(Problem is from the textbook Data Structures using C++) Add a function to the dateType class....
(Problem is from the textbook Data Structures using C++) Add a function to the dateType class. The function's name is compareDates. Its prototype is int compareDates(const dateType& otherDate; The function returns -1 if otherDate is greater than than this date The function returns 0 if otherDate equals this date The function returns 1 if otherDate is less than this date Examples dateType d1(1, 1, 2019); dateType d2(11,1, 2019) d1.compareDates(d2) returns -1 d2.compareDates(d1) returns 1 d2.compareDates(d2) returns 0 <<<<< dateType.h >>>>>>...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class in the person class.    First, Create a date class.    Which has integer month, day and year    Each with getters and setters. Be sure that you validate the getter function inputs:     2 digit months - validate 1-12 for month     2 digit day - 1-3? max for day - be sure the min-max number of days is validate for specific month...
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex...
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex numbers. A number of the form a +ib, in which i2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib. Complex numbers can also be represented as ordered pairs (a, b). The class you will design should have the following features. Constructor Your class...
2. Problem 2 is adapted from the Problem 39 at the end of Chapter 11. Please...
2. Problem 2 is adapted from the Problem 39 at the end of Chapter 11. Please solve this problem in Excel and submit your Excel spreadsheet. The problem is as follows: The state of Virginia has implemented a Standard of Learning (SOL) test that all public school students must pass before they can graduate from high school. A passing grade is 75. Montgomery County High School administrators want to gauge how well their students might do on the SOL test,...
Instructions Write a program to implement the algorithm that you designed in Exercise 19 of Chapter...
Instructions Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s standard library, this class uses C-strings as the underlying data structure. Recall that a C-string is a null-terminated array of type char. See section 8.1 in Savitch for more information about C-strings; in particular, you will find it helpful to take advantage of several of the C-string functions mentioned in that section. What To Do. In the hw8 directory that is created, you will...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at Merks Sales are paid based on an annual salary rather than an hourly wage. However, some employees are paid weekly while others are paid every other week (biweekly). Weekly employees receive 52 paychecks; biweekly employees receive 26 paychecks. The payroll manager wants a program that displays two amounts: an employee’s weekly gross pay and his or her biweekly gross pay. Complete an IPO chart...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT