Question: I'm wanting help on my program that is from the textbook: C++ Programming From Problem Analysis and to Program Design Chapter 11 Programming Exercise #2

I'm wanting help on my program that is from the textbook: "C++ Programming From Problem Analysis and to Program Design"

Chapter 11 Programming Exercise #2 pg 812

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.

Here is my provided code with some errors I'm wanting to know how to clean up.

main.cpp/test program

//Preprocessor directives, header functions

#include #include #include "dateType.h" #include "setDate.h"

//Standard Library using namespace std;

class dateType { private: int dMonth; int dDay; int dYear;

public: //Declare the member functions void setDate(int month, int day, int year); int getDay()const; int getMonth()const; int getYear()const; bool isLeapYear(int year); dateType(int month = 0, int day = 0, int year = 0); //Constructor };

//Member function definitions void dateType::setDate(int month, int day, int year) { int noDays;

if(year <= 2008) {//Condition to check if the year is valid or not dYear = year; if(month <= 12) { //The condition for a month dMonth = month; switch(month) { //For the amount of days in each month case 1: case 3: case 5: case 7: case 8: case 10: case 12: noDays = 31; break; case 4: case 6: case 9: case 11: noDays = 31; break; case 2: if(isLeapYear(year)) noDays = 29; else noDays = 28; } if(day <= noDays) {//condition for days based on number of days in a month dDay = day; } else { cout << "Invalid Day" << endl; dDay = 0; } } else { cout << "Invalid Month" << endl; dMonth = 0; } } else { cout << "Invalid Year" << endl; dYear = 0; } }//End setDate

bool dateType::isLeapYear(int year) { //Function to conform whether the year is a leap year or not if(year % 4 == 0) return true; else return false; }

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

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

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

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

//Constructor dateType::dateType(int month, int day, int year) { dMonth = month; dDay = day; dYear = year; }

void main() {//Begin void function main int m, d, y; dateType date(0,0,0); //Constructor call

//Friendly message explaining the purpose of the program cout << "Hello There! " << endl; cout << "This program's objective is to prove a leap year is a leap year" << endl; cout << "Another purpose is for the test program to test the class of this program" << endl;

//Prompt the user to enter the values cout << "Please enter the Month"; cin >> m; cout << "Please enter the Day"; cin >> d; cout << "Please enter the Year"; cin >> y; date.setDate(m, d, y); bool check = date.isLeapYear(y); if (check) cout << "Leap year:"; date.printDate();

}//end main

dateType.h header file

using namespace std;

class dateType { public: void setDate(int month, int day, int year); //Function to set the date. //The member variables dMonth, dDay, and dYear are set //according to parameters. //PostCondition: dMonth = month; dDay = day; dYear = year;

int getDay() const; //Function to return the day. //Postcondition: The value of dDay is returned.

int getMonth() const; //Function to return the month. //Postcondition: The value of dMonth is returned.

int getYear() const; //Function to return the year. //Postcondition: The value of dYear is returned.

void printDate() const; //Function to output the date in the form mm-dd-yyy.

dateType(int month = 1, int day = 1, int year = 1900); //Constructor to set the date //The member variables dMonth, dDay, and dYear are set //according to the parameters. //Postcondition: dMonth = month; dDay = day; dYear = year; // if no values are specified, the default // values are used to initialize the member variables.

private: int dMonth; //variable to store the month int dDay; //variable to store the day int dYear; //variable to store the year };

setDate.h header file

using namespace std;

//Default Constructors 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) { dMonth = month; dDay = day; dYear = year; }

dateTypeImp.cpp Which was provided in class

#include #include #include "dateType.h" #include "setDate.h" using namespace std;

void dateType::setDate(int month, int day, int year) { if (year >= 1) dYear = year; else//Default to year 1900 dYear = 1900;

if (1 <= month && month <= 12) //validating month range of 1-12 dMonth = month; else //Default to January or month 1 dMonth = 1;

switch (dMonth) { //Months, Jan., March, May, July, Aug., Oct. and Dec. have 31 days case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (1 <= day && day <= 31) dDay = day; else dDay = 1; break; //Months April, June, Sept., and Nov. have 30 days in the month case 4: case 6: case 9: case 11: if (1 <= day && <= 30) //Date range from 1-30 dDay = day; else dDay = 1; break; //February DEPENDS case 2: if (isLeapYear()) { if ( 1 <= day && day <= 29) dDay = day; else dDay = 1; } else { if (1 <= day && day <= 28) dDay = day; else dDay = 1; }

{ return dYear; }

bool dateType::isLeapYear() { if (((dYear % 4 == 0) && (dYear % 100 != 0)) || dYear % 400 = 0) return true; // The year is a leap year and the days in February is 29 else return false; //The year is NOT a leap year and the days in February is 28 }

void dateType::printDate() const { cout << dMonth << "-" << dDay << "-" << dYear; } //constructor dateType::dateType(int month, int day, int year) { //Call the setDate function setDate(month, day, year); }

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!