In this chapter, the class dateType was designed to implement the date in a prog
ID: 3768061 • Letter: I
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 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. Here is what was given: #include 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 the 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-yyyy. 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 }; 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
Here is the code given by you
#include
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 the 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-yyyy. 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 }; 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); }
Here is 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.
#include <iostream>
using namespace std;
class dateType
{
public:
void setDate(int month, int day, int year);
void setMonth(int month);
void setDay(int day);
void setYear(int year);
void calculateNewDate(int dayAmount);
int getDaysInMonth() const;
int getDaysPassed() const;
int getDaysRemaining() const;
int getDay() const;
int getMonth() const;
int getYear() const;
bool isLeapYear() const;
void printDate() const;
dateType(int = 1, int = 1, int = 1900);
private:
int dMonth;
int dDay;
int dYear;
};
int main()
{
dateType myDate(12, 31, 2008);
myDate.printDate();
cout << "Total days passed in year " << myDate.getDaysPassed() << endl;
cout << "Total days remaining in year" << myDate.getDaysRemaining() << endl;
myDate.printDate();
return 0;
}
void dateType::setDate(int month, int day, int year)
{
if(1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
if(year > 0)
dYear = year;
else
dYear = 1;
if(dMonth <= 7)
{
if(dMonth % 2 == 1 && (1 <= day && day <= 31))
dDay = day;
else if((dMonth % 2 == 0 && dMonth != 2) && (1 <= day && day <= 30))
dDay = day;
else if((dMonth == 2 && isLeapYear()) && (1 <= day && day <= 29))
dDay = day;
else if((dMonth == 2 && !isLeapYear()) && (1 <= day && day <=28))
dDay = day;
else
dDay = 1;
}
else
{
if(dMonth % 2 == 1 && (1 <= day && day <= 30))
dDay = day;
else if(dMonth % 2 == 0 && (1 <= day && day <= 31))
dDay = day;
else
dDay = 1;
}
}
void dateType::setMonth(int month)
{
if(1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
}
void dateType::setDay(int day)
{
if(dMonth <= 7)
{
if(dMonth % 2 == 1 && (1 <= day && day <= 31))
dDay = day;
else if((dMonth % 2 == 0 && dMonth != 2) && (1 <= day && day <= 30))
dDay = day;
else if((dMonth == 2 && isLeapYear()) && (1 <= day && day <= 29))
dDay = day;
else if((dMonth == 2 && !isLeapYear()) && (1 <= day && day <=28))
dDay = day;
else
dDay = 1;
}
else
{
if(dMonth % 2 == 1 && (1 <= day && day <= 30))
dDay = day;
else if(dMonth % 2 == 0 && (1 <= day && day <= 31))
dDay = day;
else
dDay = 1;
}
}
void dateType::setYear(int year)
{
if(year > 0)
dYear = year;
else
dYear = 1;
}
void dateType::calculateNewDate(int dayAmount)
{
}
int dateType::getDaysInMonth() const
{
if(dMonth <= 7)
{
switch(dMonth % 2)
{
case 0:
if(dMonth == 2 && isLeapYear())
return 29;
else if(dMonth == 2 && !isLeapYear())
return 28;
else
return 30;
case 1:
return 31;
}//end switch
} //end if
else
{
switch(dMonth % 2)
{
case 0:
return 31;
case 1:
return 30;
}
}
}
int dateType::getDaysPassed() const
{
switch(dMonth)
{
case 1:
return dDay;
case 2:
return (31 + dDay);
case 3:
if(isLeapYear())
return (60 + dDay);
else
return (59 + dDay);
case 4:
if(isLeapYear())
return (91 + dDay);
else
return (90 + dDay);
case 5:
if(isLeapYear())
return (121 + dDay);
else
return (120 + dDay);
case 6:
if(isLeapYear())
return (152 + dDay);
else
return (151 + dDay);
case 7:
if(isLeapYear())
return (182 + dDay);
else
return (181 + dDay);
case 8:
if(isLeapYear())
return (213 + dDay);
else
return (212 + dDay);
case 9:
if(isLeapYear())
return (244 + dDay);
else
return (243 + dDay);
case 10:
if(isLeapYear())
return (274 + dDay);
else
return (273 + dDay);
case 11:
if(isLeapYear())
return (305 + dDay);
else
return (304 + dDay);
case 12:
if(isLeapYear())
return (335 + dDay);
else
return (334 + dDay);
}
}
int dateType::getDaysRemaining() const
{
if(isLeapYear())
return (366 - getDaysPassed());
else
return (365 - getDaysPassed());
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
bool dateType::isLeapYear() const
{
return(dYear % 4 == 0 && (dYear % 100 != 0 || dYear % 400 == 0));
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear << endl;
}
dateType::dateType(int month, int day, int year)
{
if(1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
if(year > 0)
dYear = year;
else
dYear = 1;
if(dMonth <= 7)
{
if(dMonth % 2 == 1 && (1 <= day && day <= 31))
dDay = day;
else if((dMonth % 2 == 0 && dMonth != 2) && (1 <= day && day <= 30))
dDay = day;
else if((dMonth == 2 && isLeapYear()) && (1 <= day && day <= 29))
dDay = day;
else if((dMonth == 2 && !isLeapYear()) && (1 <= day && day <=28))
dDay = day;
else
dDay = 1;
}
else
{
if(dMonth % 2 == 1 && (1 <= day && day <= 30))
dDay = day;
else if(dMonth % 2 == 0 && (1 <= day && day <= 31))
dDay = day;
else
dDay = 1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.