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...
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,...
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...
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...
Q2. Update the given code 2 to implement the following problem: Create a class triangle Use...
Q2. Update the given code 2 to implement the following problem: Create a class triangle Use dynamic memory allocation to create 10 objects of the class triangle Set default height and base of all created objects (triangle) to 5 and 10 respectively using a constructor Ask user to change only the height of the 5th triangle Print height and base of all (10) triangles to demonstrate that you did the task correctly Deallocate the 5th object Print height and base...
The Date Class This class will function similarly in spirit to the Date class in the...
The Date Class This class will function similarly in spirit to the Date class in the text, and behaves very much like a “standard” class should. It should store a month, day, and year, in addition to providing useful functions like date reporting (toString()), date setting (constructors and getters/setters), as well as some basic error detection (for example, all month values should be between 1-12, if represented as an integer). Start this by defining a new class called “Date” or...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Chapter 4 Problem 11 Problem 15, part f. in Chapter 3 asks you to construct a...
Chapter 4 Problem 11 Problem 15, part f. in Chapter 3 asks you to construct a five year financial projection for Aquatic Supplies beginning in 2015. The five year projection appears below. a. Calculate Aquatic Supplies's sustainable and actual growth rates in these years.   b. What do these numbers suggest to you? Aquatic Supplies Co. Five Year Projected Income Statements and Balance Sheets Income Statement (in $ millions) Pro Forma Forecasts 2015 - 2019 2014 Assumptions 2015 2016 2017 2018...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT