SlideShare a Scribd company logo
Problem Definition
Build a Date class and a main function to test it.
Specifications
Below is the interface for the Date class: it is our "contract" with you: you have to implement
everything it describes, and show us that it works with a test harness that puts it through its
paces. The comments in the interface below should be sufficient for you to understand the
project (use these comments in your Date declaration), without the need of any further
documentation. But of course, as always, you can ask us any questions you may have on Piazza.
Note: Placing the error messages into the constructors like is not necessarily a good way to
handle constructor errors, but until you learn about exceptions in CS 14, it's the best we can do.
Private Member Functions
The functions declared private above, isLeap, daysPerMonth, name, number, are helper functions
- member functions that will never be needed by a user of the class, and so do not belong to the
public interface (which is why they are "private"). They are, however, needed by the interface
functions (public member functions), which use them to test the validity of arguments and
construct valid dates. For example, the constructor that passes in the month as a string will call
the number function to assign a value to the unsigned member variable month.
isLeap: The rule for whether a year is a leap year is:
(year % 4 == 0) implies leap year
except (year % 100 == 0) implies NOT leap year
except (year % 400 == 0) implies leap year
So, for instance, year 2000 is a leap year, but 1900 is NOT a leap year. Years 2004, 2008, 2012,
2016, etc. are all leap years. Years 2005, 2006, 2007, 2009, 2010, etc. are NOT leap years.
Output Specifications
Read the specifications for the print function carefully. The only cout statements within your
Date member functions should be:
the "Invalid Date" warnings in the constructors
in your two print functions
Required Main Function
You must use this main function and global function getDate as they are here. You may not
change these functions at all. Copy-and-paste these into your main.cpp file and then add the Date
class.
Solution
// Date.h
#include
#include
#include
#include
using namespace std;
class Date
{
private:
unsigned day;
unsigned month;
string monthName;
unsigned year;
public:
Date();
Date(unsigned m, unsigned d, unsigned y);
Date(const string &mn, unsigned d, unsigned y);
void printNumeric() const;
void printAlpha() const;
private:
bool isLeap(unsigned y) const;
unsigned daysPerMonth(unsigned m, unsigned y) const;
string name(unsigned m) const;
unsigned number(const string &mn) const;
};
//Date.cpp
#include
#include
#include
#include
using namespace std;
#include "Date.h"
// creates the date January 1st, 2000.
Date::Date()
{
day = 1;
month = 1;
monthName = "January";
year = 2000;
}
/* parameterized constructor: month number, day, year
- e.g. (3, 1, 2010) will construct the date March 1st, 2010
If any of the arguments are invalid (e.g. 15 for month or 32 for day)
then the constructor will construct instead a valid Date as close
as possible to the arguments provided - e.g. in above example,
Date(15, 32, 2010), the Date would be corrected to Dec 31st, 2010.
In case of such invalid input, the constructor will issue a console error message:
Invalid date values: Date corrected to 12/31/2010.
(with a newline at the end).
*/
Date::Date (unsigned m, unsigned d, unsigned y)
{
bool invalid = false;
//if invalid month input, change to closest month
if (m < 1)
{
m = 1;
invalid = true;
}
else if (m > 12)
{
m = 12;
invalid = true;
}
//invalid day
if (d > daysPerMonth(m, y))
{
d = daysPerMonth(m,y);
invalid = true;
}
day = d;
month = m;
monthName = name(m);
year = y;
if (invalid)
{
cout << "Invalid date values: Date corrected to ";
cout << month << "/" << day << "/" << year << "." << endl;
}
}
/* parameterized constructor: month name, day, year
- - e.g. (December, 15, 2012) will construct the date December 15th, 2012
If the constructor is unable to recognize the string argument as a valid month name,
then it will issue a console error message:
Invalid month name: the Date was set to 1/1/2000.
(with a newline at the end).
If the day argument is invalid for the given month (but the month name was valid),
then the constructor will handle this error in the same manner as the other
parameterized constructor.
This constructor will recognize both "december" and "December"
as month name.
*/
Date::Date (const string &mName, unsigned d, unsigned y)
{
//will change to true if invalid day
bool invalidDay = false;
//will change to true if invalid month
bool invalidMonth = false;
if (mName == "January" || mName == "january")
{
month = number("January");
monthName = "January";
}
else if (mName == "February" || mName == "february")
{
month = number("February");
monthName = "February";
}
else if (mName == "March" || mName == "march")
{
month = number("March");
monthName = "March";
}
else if (mName == "April" || mName == "april")
{
month = number("April");
monthName = "April";
}
else if (mName == "May" || mName == "may")
{
month = number("May");
monthName = "May";
}
else if (mName == "June" || mName == "june")
{
month = number("June");
monthName = "June";
}
else if (mName == "July" || mName == "july")
{
month = number("July");
monthName = "July";
}
else if (mName == "August" || mName == "august")
{
month = number("August");
monthName = "August";
}
else if (mName == "September" || mName == "september")
{
month = number("September");
monthName = "September";
}
else if (mName == "October" || mName == "october")
{
month = number("October");
monthName = "October";
}
else if (mName == "November" || mName == "november")
{
month = number("November");
monthName = "November";
}
else if (mName == "December" || mName == "december")
{
month = number("December");
monthName = "December";
}
//If invalid month name, change date to 1/1/2000
else
{
day = 1;
month = number("January");
monthName = "January";
year = 2000;
invalidMonth = true;
}
//if monthName is correct but day number is invalid, change to closest day
if ((!invalidMonth) && (d > daysPerMonth(month, y)))
{
day = daysPerMonth(month,y);
invalidDay = true;
}
//outputs message if input was invalid
if (invalidDay || invalidMonth)
{
cout << "Invalid date values: Date corrected to ";
if (invalidDay)
{
year = y;
}
cout << month << "/" << day << "/" << year << "." << endl;
}
else
{
day = d;
year = y;
}
}
/* Outputs to the console (cout) a Date exactly in the format "3/1/2012".
Does not output a newline at the end.
*/
void Date::printNumeric () const
{
cout << month << "/" << day << "/" << year;
}
/* Outputs to the console (cout) a Date exactly in the format "March 1, 2012".
The first letter of the month name is upper case, and the month name is
printed in full - January, not Jan, jan, or january.
Does not output a newline at the end.
*/
void Date::printAlpha () const
{
cout << monthName << " " << day << ", " << year;
}
/* Returns true if the year passed in is a leap year, otherwise returns false.
*/
bool Date::isLeap(unsigned y) const
{
//implies leap year
if (y % 4 == 0)
{
//does not imply leap year
if (y % 100 == 0)
{
//unless its a multiple of 400
if (y % 400 == 0)
{
return true;
}
return false;
}
return true;
}
return false;
}
/* Returns number of days allowed in a given month
- e.g. daysPerMonth(9, 2000) returns 30.
Calculates February's days for leap and non--leap years,
thus, the reason year is also a parameter.
*/
unsigned Date::daysPerMonth(unsigned m, unsigned y) const
{
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
return 31;
}
else if (m == 4 || m == 6 || m == 9 || m == 11)
{
return 30;
}
else if (isLeap(y))
{
return 29;
}
return 28;
}
/* Returns the name of a given month
- e.g. name(12) returns the string "December"
*/
string Date::name(unsigned m) const
{
if (m <= 1)
{
return "January";
}
else if (m >= 12)
{
return "December";
}
if (m == 2)
{
return "February";
}
else if (m == 3)
{
return "March";
}
else if (m == 4)
{
return "April";
}
else if (m == 5)
{
return "May";
}
else if (m == 6)
{
return "June";
}
else if (m == 7)
{
return "July";
}
else if (m == 8)
{
return "August";
}
else if (m == 9)
{
return "September";
}
else if (m == 10)
{
return "October";
}
else if (m == 11)
{
return "November";
}
}
/* Returns the number of a given named month
- e.g. number("March") returns 3
*/
unsigned Date::number(const string &mName) const
{
if (mName == "January")
{
return 1;
}
else if (mName == "February")
{
return 2;
}
else if (mName == "March")
{
return 3;
}
else if (mName == "April")
{
return 4;
}
else if (mName == "May")
{
return 5;
}
else if (mName == "June")
{
return 6;
}
else if (mName == "July")
{
return 7;
}
else if (mName == "August")
{
return 8;
}
else if (mName == "September")
{
return 9;
}
else if (mName == "October")
{
return 10;
}
else if (mName == "November")
{
return 11;
}
else if (mName == "December")
{
return 12;
}
}
// main.cpp
#include
#include
#include
#include
#include "Date.h"
using namespace std;
Date getDate();
int main() {
Date testDate;
testDate = getDate();
cout << endl;
cout << "Numeric: ";
testDate.printNumeric();
cout << endl;
cout << "Alpha: ";
testDate.printAlpha();
cout << endl;
return 0;
}
Date getDate() {
int choice;
unsigned monthNumber, day, year;
string monthName;
cout << "Which Date constructor? (Enter 1, 2, or 3)" << endl
<< "1 - Month Number" << endl
<< "2 - Month Name" << endl
<< "3 - default" << endl;
cin >> choice;
cout << endl;
if (choice == 1) {
cout << "month number? ";
cin >> monthNumber;
cout << endl;
cout << "day? ";
cin >> day;
cout << endl;
cout << "year? ";
cin >> year;
cout << endl;
return Date(monthNumber, day, year);
} else if (choice == 2) {
cout << "month name? ";
cin >> monthName;
cout << endl;
cout << "day? ";
cin >> day;
cout << endl;
cout << "year? ";
cin >> year;
cout << endl;
return Date(monthName, day, year);
} else {
return Date();
}
}
/*
output:
Which Date constructor? (Enter 1, 2, or 3)
1 - Month Number
2 - Month Name
3 - default
3
Numeric: 1/1/2000
Alpha: January 1, 2000
Which Date constructor? (Enter 1, 2, or 3)
1 - Month Number
2 - Month Name
3 - default
2
month name? march
day? 32
year? 1994
Invalid date values: Date corrected to 3/31/1994.
Numeric: 3/31/1994
Alpha: March 31, 1994
*/

More Related Content

Similar to Problem DefinitionBuild a Date class and a main function to test i.pdf

Write a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdfWrite a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdf
arihantmobileselepun
 
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdfpublicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
mukhtaransarcloth
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)
Achmad Solichin
 
Hello I need some help on this question this is for C++ PEARSON REVE.pdf
Hello I need some help on this question this is for C++ PEARSON REVE.pdfHello I need some help on this question this is for C++ PEARSON REVE.pdf
Hello I need some help on this question this is for C++ PEARSON REVE.pdf
almonardfans
 
I am having some trouble getting this to compile, I have made seve.docx
I am having some trouble getting this to compile, I have made seve.docxI am having some trouble getting this to compile, I have made seve.docx
I am having some trouble getting this to compile, I have made seve.docx
adampcarr67227
 
I got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdfI got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdf
shreeaadithyaacellso
 
Les03
Les03Les03
I Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdfI Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdf
allystraders
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
MomenMostafa
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Dustin Chase
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
anonaeon
 
MS_Access_C_-_Calculated__Fields in IT.pptx
MS_Access_C_-_Calculated__Fields in IT.pptxMS_Access_C_-_Calculated__Fields in IT.pptx
MS_Access_C_-_Calculated__Fields in IT.pptx
ShavaneDavis
 
SessionSeven_WorkingWithDatesandTime
SessionSeven_WorkingWithDatesandTimeSessionSeven_WorkingWithDatesandTime
SessionSeven_WorkingWithDatesandTimeHellen Gakuruh
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]MomenMostafa
 
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
BANSALANKIT1077
 

Similar to Problem DefinitionBuild a Date class and a main function to test i.pdf (16)

Write a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdfWrite a program to generate the entire calendar for one year. The pr.pdf
Write a program to generate the entire calendar for one year. The pr.pdf
 
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdfpublicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)
 
Hello I need some help on this question this is for C++ PEARSON REVE.pdf
Hello I need some help on this question this is for C++ PEARSON REVE.pdfHello I need some help on this question this is for C++ PEARSON REVE.pdf
Hello I need some help on this question this is for C++ PEARSON REVE.pdf
 
I am having some trouble getting this to compile, I have made seve.docx
I am having some trouble getting this to compile, I have made seve.docxI am having some trouble getting this to compile, I have made seve.docx
I am having some trouble getting this to compile, I have made seve.docx
 
I got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdfI got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdf
 
Les03
Les03Les03
Les03
 
I Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdfI Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdf
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
Les03
Les03Les03
Les03
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
 
MS_Access_C_-_Calculated__Fields in IT.pptx
MS_Access_C_-_Calculated__Fields in IT.pptxMS_Access_C_-_Calculated__Fields in IT.pptx
MS_Access_C_-_Calculated__Fields in IT.pptx
 
SessionSeven_WorkingWithDatesandTime
SessionSeven_WorkingWithDatesandTimeSessionSeven_WorkingWithDatesandTime
SessionSeven_WorkingWithDatesandTime
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
(----IN C) Incorrect output on some test cases- I'm using a linked lis.pdf
 

More from smitaguptabootique

A husband and wife, Ted and Suzanne, share a digital music player th.pdf
A husband and wife, Ted and Suzanne, share a digital music player th.pdfA husband and wife, Ted and Suzanne, share a digital music player th.pdf
A husband and wife, Ted and Suzanne, share a digital music player th.pdf
smitaguptabootique
 
Why the average of two extreme observations is the best predictor of.pdf
Why the average of two extreme observations is the best predictor of.pdfWhy the average of two extreme observations is the best predictor of.pdf
Why the average of two extreme observations is the best predictor of.pdf
smitaguptabootique
 
Why is a constant in matrices called a scalar Dont assume I know .pdf
Why is a constant in matrices called a scalar Dont assume I know .pdfWhy is a constant in matrices called a scalar Dont assume I know .pdf
Why is a constant in matrices called a scalar Dont assume I know .pdf
smitaguptabootique
 
What is the mission andor vision of BMO Harriis bankSolution.pdf
What is the mission andor vision of BMO Harriis bankSolution.pdfWhat is the mission andor vision of BMO Harriis bankSolution.pdf
What is the mission andor vision of BMO Harriis bankSolution.pdf
smitaguptabootique
 
Using the Thin-lens equation A 1f = 1p + 1q. with equation B L =.pdf
Using the Thin-lens equation A 1f = 1p + 1q. with equation B L =.pdfUsing the Thin-lens equation A 1f = 1p + 1q. with equation B L =.pdf
Using the Thin-lens equation A 1f = 1p + 1q. with equation B L =.pdf
smitaguptabootique
 
Using C language to write an algorithm that accepts a list implement.pdf
Using C language to write an algorithm that accepts a list implement.pdfUsing C language to write an algorithm that accepts a list implement.pdf
Using C language to write an algorithm that accepts a list implement.pdf
smitaguptabootique
 
type 1-2 page (typewritten) research paper on the issue of voting.pdf
type 1-2 page (typewritten) research paper on the issue of voting.pdftype 1-2 page (typewritten) research paper on the issue of voting.pdf
type 1-2 page (typewritten) research paper on the issue of voting.pdf
smitaguptabootique
 
Two figures have a ration if similarity of 34. If the are of the la.pdf
Two figures have a ration if similarity of 34. If the are of the la.pdfTwo figures have a ration if similarity of 34. If the are of the la.pdf
Two figures have a ration if similarity of 34. If the are of the la.pdf
smitaguptabootique
 
There is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdfThere is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdf
smitaguptabootique
 
The game of Pig is a simple two-player dice game in which the first .pdf
The game of Pig is a simple two-player dice game in which the first .pdfThe game of Pig is a simple two-player dice game in which the first .pdf
The game of Pig is a simple two-player dice game in which the first .pdf
smitaguptabootique
 
Problem 2 DSB-SC, Matched Filtering Consider the Double-Sideband Sup.pdf
Problem 2 DSB-SC, Matched Filtering Consider the Double-Sideband Sup.pdfProblem 2 DSB-SC, Matched Filtering Consider the Double-Sideband Sup.pdf
Problem 2 DSB-SC, Matched Filtering Consider the Double-Sideband Sup.pdf
smitaguptabootique
 
One of the popular tourist attractions in Alaska is watching black b.pdf
One of the popular tourist attractions in Alaska is watching black b.pdfOne of the popular tourist attractions in Alaska is watching black b.pdf
One of the popular tourist attractions in Alaska is watching black b.pdf
smitaguptabootique
 
Im doing a marketing plan on how Apple can improve their products .pdf
Im doing a marketing plan on how Apple can improve their products .pdfIm doing a marketing plan on how Apple can improve their products .pdf
Im doing a marketing plan on how Apple can improve their products .pdf
smitaguptabootique
 
If we were to implement a better budgeting process in which lower-le.pdf
If we were to implement a better budgeting process in which lower-le.pdfIf we were to implement a better budgeting process in which lower-le.pdf
If we were to implement a better budgeting process in which lower-le.pdf
smitaguptabootique
 
I can change a link layer protocol and the application layer protoco.pdf
I can change a link layer protocol and the application layer protoco.pdfI can change a link layer protocol and the application layer protoco.pdf
I can change a link layer protocol and the application layer protoco.pdf
smitaguptabootique
 
Could a poly-Ala sequence form a beta sheet Why or why notSolu.pdf
Could a poly-Ala sequence form a beta sheet Why or why notSolu.pdfCould a poly-Ala sequence form a beta sheet Why or why notSolu.pdf
Could a poly-Ala sequence form a beta sheet Why or why notSolu.pdf
smitaguptabootique
 
Cell Physiology Discuss the ramifications of the Human Genome Proje.pdf
Cell Physiology Discuss the ramifications of the Human Genome Proje.pdfCell Physiology Discuss the ramifications of the Human Genome Proje.pdf
Cell Physiology Discuss the ramifications of the Human Genome Proje.pdf
smitaguptabootique
 
Early on the morning of April 15, 1912, the RMS Titanic collided wit.pdf
Early on the morning of April 15, 1912, the RMS Titanic collided wit.pdfEarly on the morning of April 15, 1912, the RMS Titanic collided wit.pdf
Early on the morning of April 15, 1912, the RMS Titanic collided wit.pdf
smitaguptabootique
 
Discuss why ethnoarchaeology is so important in helping us understan.pdf
Discuss why ethnoarchaeology is so important in helping us understan.pdfDiscuss why ethnoarchaeology is so important in helping us understan.pdf
Discuss why ethnoarchaeology is so important in helping us understan.pdf
smitaguptabootique
 
Briefly discuss five issues which crawling insects face and how they .pdf
Briefly discuss five issues which crawling insects face and how they .pdfBriefly discuss five issues which crawling insects face and how they .pdf
Briefly discuss five issues which crawling insects face and how they .pdf
smitaguptabootique
 

More from smitaguptabootique (20)

A husband and wife, Ted and Suzanne, share a digital music player th.pdf
A husband and wife, Ted and Suzanne, share a digital music player th.pdfA husband and wife, Ted and Suzanne, share a digital music player th.pdf
A husband and wife, Ted and Suzanne, share a digital music player th.pdf
 
Why the average of two extreme observations is the best predictor of.pdf
Why the average of two extreme observations is the best predictor of.pdfWhy the average of two extreme observations is the best predictor of.pdf
Why the average of two extreme observations is the best predictor of.pdf
 
Why is a constant in matrices called a scalar Dont assume I know .pdf
Why is a constant in matrices called a scalar Dont assume I know .pdfWhy is a constant in matrices called a scalar Dont assume I know .pdf
Why is a constant in matrices called a scalar Dont assume I know .pdf
 
What is the mission andor vision of BMO Harriis bankSolution.pdf
What is the mission andor vision of BMO Harriis bankSolution.pdfWhat is the mission andor vision of BMO Harriis bankSolution.pdf
What is the mission andor vision of BMO Harriis bankSolution.pdf
 
Using the Thin-lens equation A 1f = 1p + 1q. with equation B L =.pdf
Using the Thin-lens equation A 1f = 1p + 1q. with equation B L =.pdfUsing the Thin-lens equation A 1f = 1p + 1q. with equation B L =.pdf
Using the Thin-lens equation A 1f = 1p + 1q. with equation B L =.pdf
 
Using C language to write an algorithm that accepts a list implement.pdf
Using C language to write an algorithm that accepts a list implement.pdfUsing C language to write an algorithm that accepts a list implement.pdf
Using C language to write an algorithm that accepts a list implement.pdf
 
type 1-2 page (typewritten) research paper on the issue of voting.pdf
type 1-2 page (typewritten) research paper on the issue of voting.pdftype 1-2 page (typewritten) research paper on the issue of voting.pdf
type 1-2 page (typewritten) research paper on the issue of voting.pdf
 
Two figures have a ration if similarity of 34. If the are of the la.pdf
Two figures have a ration if similarity of 34. If the are of the la.pdfTwo figures have a ration if similarity of 34. If the are of the la.pdf
Two figures have a ration if similarity of 34. If the are of the la.pdf
 
There is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdfThere is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdf
 
The game of Pig is a simple two-player dice game in which the first .pdf
The game of Pig is a simple two-player dice game in which the first .pdfThe game of Pig is a simple two-player dice game in which the first .pdf
The game of Pig is a simple two-player dice game in which the first .pdf
 
Problem 2 DSB-SC, Matched Filtering Consider the Double-Sideband Sup.pdf
Problem 2 DSB-SC, Matched Filtering Consider the Double-Sideband Sup.pdfProblem 2 DSB-SC, Matched Filtering Consider the Double-Sideband Sup.pdf
Problem 2 DSB-SC, Matched Filtering Consider the Double-Sideband Sup.pdf
 
One of the popular tourist attractions in Alaska is watching black b.pdf
One of the popular tourist attractions in Alaska is watching black b.pdfOne of the popular tourist attractions in Alaska is watching black b.pdf
One of the popular tourist attractions in Alaska is watching black b.pdf
 
Im doing a marketing plan on how Apple can improve their products .pdf
Im doing a marketing plan on how Apple can improve their products .pdfIm doing a marketing plan on how Apple can improve their products .pdf
Im doing a marketing plan on how Apple can improve their products .pdf
 
If we were to implement a better budgeting process in which lower-le.pdf
If we were to implement a better budgeting process in which lower-le.pdfIf we were to implement a better budgeting process in which lower-le.pdf
If we were to implement a better budgeting process in which lower-le.pdf
 
I can change a link layer protocol and the application layer protoco.pdf
I can change a link layer protocol and the application layer protoco.pdfI can change a link layer protocol and the application layer protoco.pdf
I can change a link layer protocol and the application layer protoco.pdf
 
Could a poly-Ala sequence form a beta sheet Why or why notSolu.pdf
Could a poly-Ala sequence form a beta sheet Why or why notSolu.pdfCould a poly-Ala sequence form a beta sheet Why or why notSolu.pdf
Could a poly-Ala sequence form a beta sheet Why or why notSolu.pdf
 
Cell Physiology Discuss the ramifications of the Human Genome Proje.pdf
Cell Physiology Discuss the ramifications of the Human Genome Proje.pdfCell Physiology Discuss the ramifications of the Human Genome Proje.pdf
Cell Physiology Discuss the ramifications of the Human Genome Proje.pdf
 
Early on the morning of April 15, 1912, the RMS Titanic collided wit.pdf
Early on the morning of April 15, 1912, the RMS Titanic collided wit.pdfEarly on the morning of April 15, 1912, the RMS Titanic collided wit.pdf
Early on the morning of April 15, 1912, the RMS Titanic collided wit.pdf
 
Discuss why ethnoarchaeology is so important in helping us understan.pdf
Discuss why ethnoarchaeology is so important in helping us understan.pdfDiscuss why ethnoarchaeology is so important in helping us understan.pdf
Discuss why ethnoarchaeology is so important in helping us understan.pdf
 
Briefly discuss five issues which crawling insects face and how they .pdf
Briefly discuss five issues which crawling insects face and how they .pdfBriefly discuss five issues which crawling insects face and how they .pdf
Briefly discuss five issues which crawling insects face and how they .pdf
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 

Problem DefinitionBuild a Date class and a main function to test i.pdf

  • 1. Problem Definition Build a Date class and a main function to test it. Specifications Below is the interface for the Date class: it is our "contract" with you: you have to implement everything it describes, and show us that it works with a test harness that puts it through its paces. The comments in the interface below should be sufficient for you to understand the project (use these comments in your Date declaration), without the need of any further documentation. But of course, as always, you can ask us any questions you may have on Piazza. Note: Placing the error messages into the constructors like is not necessarily a good way to handle constructor errors, but until you learn about exceptions in CS 14, it's the best we can do. Private Member Functions The functions declared private above, isLeap, daysPerMonth, name, number, are helper functions - member functions that will never be needed by a user of the class, and so do not belong to the public interface (which is why they are "private"). They are, however, needed by the interface functions (public member functions), which use them to test the validity of arguments and construct valid dates. For example, the constructor that passes in the month as a string will call the number function to assign a value to the unsigned member variable month. isLeap: The rule for whether a year is a leap year is: (year % 4 == 0) implies leap year except (year % 100 == 0) implies NOT leap year except (year % 400 == 0) implies leap year So, for instance, year 2000 is a leap year, but 1900 is NOT a leap year. Years 2004, 2008, 2012, 2016, etc. are all leap years. Years 2005, 2006, 2007, 2009, 2010, etc. are NOT leap years. Output Specifications Read the specifications for the print function carefully. The only cout statements within your Date member functions should be: the "Invalid Date" warnings in the constructors in your two print functions Required Main Function You must use this main function and global function getDate as they are here. You may not change these functions at all. Copy-and-paste these into your main.cpp file and then add the Date class. Solution
  • 2. // Date.h #include #include #include #include using namespace std; class Date { private: unsigned day; unsigned month; string monthName; unsigned year; public: Date(); Date(unsigned m, unsigned d, unsigned y); Date(const string &mn, unsigned d, unsigned y); void printNumeric() const; void printAlpha() const; private: bool isLeap(unsigned y) const; unsigned daysPerMonth(unsigned m, unsigned y) const; string name(unsigned m) const; unsigned number(const string &mn) const; }; //Date.cpp #include #include #include #include using namespace std; #include "Date.h" // creates the date January 1st, 2000. Date::Date()
  • 3. { day = 1; month = 1; monthName = "January"; year = 2000; } /* parameterized constructor: month number, day, year - e.g. (3, 1, 2010) will construct the date March 1st, 2010 If any of the arguments are invalid (e.g. 15 for month or 32 for day) then the constructor will construct instead a valid Date as close as possible to the arguments provided - e.g. in above example, Date(15, 32, 2010), the Date would be corrected to Dec 31st, 2010. In case of such invalid input, the constructor will issue a console error message: Invalid date values: Date corrected to 12/31/2010. (with a newline at the end). */ Date::Date (unsigned m, unsigned d, unsigned y) { bool invalid = false; //if invalid month input, change to closest month if (m < 1) { m = 1; invalid = true; } else if (m > 12) { m = 12; invalid = true; } //invalid day if (d > daysPerMonth(m, y)) { d = daysPerMonth(m,y);
  • 4. invalid = true; } day = d; month = m; monthName = name(m); year = y; if (invalid) { cout << "Invalid date values: Date corrected to "; cout << month << "/" << day << "/" << year << "." << endl; } } /* parameterized constructor: month name, day, year - - e.g. (December, 15, 2012) will construct the date December 15th, 2012 If the constructor is unable to recognize the string argument as a valid month name, then it will issue a console error message: Invalid month name: the Date was set to 1/1/2000. (with a newline at the end). If the day argument is invalid for the given month (but the month name was valid), then the constructor will handle this error in the same manner as the other parameterized constructor. This constructor will recognize both "december" and "December" as month name. */ Date::Date (const string &mName, unsigned d, unsigned y) { //will change to true if invalid day bool invalidDay = false; //will change to true if invalid month bool invalidMonth = false; if (mName == "January" || mName == "january") { month = number("January");
  • 5. monthName = "January"; } else if (mName == "February" || mName == "february") { month = number("February"); monthName = "February"; } else if (mName == "March" || mName == "march") { month = number("March"); monthName = "March"; } else if (mName == "April" || mName == "april") { month = number("April"); monthName = "April"; } else if (mName == "May" || mName == "may") { month = number("May"); monthName = "May"; } else if (mName == "June" || mName == "june") { month = number("June"); monthName = "June"; } else if (mName == "July" || mName == "july") { month = number("July"); monthName = "July"; } else if (mName == "August" || mName == "august") { month = number("August"); monthName = "August";
  • 6. } else if (mName == "September" || mName == "september") { month = number("September"); monthName = "September"; } else if (mName == "October" || mName == "october") { month = number("October"); monthName = "October"; } else if (mName == "November" || mName == "november") { month = number("November"); monthName = "November"; } else if (mName == "December" || mName == "december") { month = number("December"); monthName = "December"; } //If invalid month name, change date to 1/1/2000 else { day = 1; month = number("January"); monthName = "January"; year = 2000; invalidMonth = true; } //if monthName is correct but day number is invalid, change to closest day if ((!invalidMonth) && (d > daysPerMonth(month, y))) { day = daysPerMonth(month,y); invalidDay = true;
  • 7. } //outputs message if input was invalid if (invalidDay || invalidMonth) { cout << "Invalid date values: Date corrected to "; if (invalidDay) { year = y; } cout << month << "/" << day << "/" << year << "." << endl; } else { day = d; year = y; } } /* Outputs to the console (cout) a Date exactly in the format "3/1/2012". Does not output a newline at the end. */ void Date::printNumeric () const { cout << month << "/" << day << "/" << year; } /* Outputs to the console (cout) a Date exactly in the format "March 1, 2012". The first letter of the month name is upper case, and the month name is printed in full - January, not Jan, jan, or january. Does not output a newline at the end. */ void Date::printAlpha () const { cout << monthName << " " << day << ", " << year; } /* Returns true if the year passed in is a leap year, otherwise returns false.
  • 8. */ bool Date::isLeap(unsigned y) const { //implies leap year if (y % 4 == 0) { //does not imply leap year if (y % 100 == 0) { //unless its a multiple of 400 if (y % 400 == 0) { return true; } return false; } return true; } return false; } /* Returns number of days allowed in a given month - e.g. daysPerMonth(9, 2000) returns 30. Calculates February's days for leap and non--leap years, thus, the reason year is also a parameter. */ unsigned Date::daysPerMonth(unsigned m, unsigned y) const { if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { return 31; } else if (m == 4 || m == 6 || m == 9 || m == 11) { return 30; } else if (isLeap(y))
  • 9. { return 29; } return 28; } /* Returns the name of a given month - e.g. name(12) returns the string "December" */ string Date::name(unsigned m) const { if (m <= 1) { return "January"; } else if (m >= 12) { return "December"; } if (m == 2) { return "February"; } else if (m == 3) { return "March"; } else if (m == 4) { return "April"; } else if (m == 5) { return "May"; } else if (m == 6)
  • 10. { return "June"; } else if (m == 7) { return "July"; } else if (m == 8) { return "August"; } else if (m == 9) { return "September"; } else if (m == 10) { return "October"; } else if (m == 11) { return "November"; } } /* Returns the number of a given named month - e.g. number("March") returns 3 */ unsigned Date::number(const string &mName) const { if (mName == "January") { return 1; } else if (mName == "February") {
  • 11. return 2; } else if (mName == "March") { return 3; } else if (mName == "April") { return 4; } else if (mName == "May") { return 5; } else if (mName == "June") { return 6; } else if (mName == "July") { return 7; } else if (mName == "August") { return 8; } else if (mName == "September") { return 9; } else if (mName == "October") { return 10; } else if (mName == "November") {
  • 12. return 11; } else if (mName == "December") { return 12; } } // main.cpp #include #include #include #include #include "Date.h" using namespace std; Date getDate(); int main() { Date testDate; testDate = getDate(); cout << endl; cout << "Numeric: "; testDate.printNumeric(); cout << endl; cout << "Alpha: "; testDate.printAlpha(); cout << endl; return 0; } Date getDate() { int choice; unsigned monthNumber, day, year; string monthName; cout << "Which Date constructor? (Enter 1, 2, or 3)" << endl
  • 13. << "1 - Month Number" << endl << "2 - Month Name" << endl << "3 - default" << endl; cin >> choice; cout << endl; if (choice == 1) { cout << "month number? "; cin >> monthNumber; cout << endl; cout << "day? "; cin >> day; cout << endl; cout << "year? "; cin >> year; cout << endl; return Date(monthNumber, day, year); } else if (choice == 2) { cout << "month name? "; cin >> monthName; cout << endl; cout << "day? "; cin >> day; cout << endl; cout << "year? "; cin >> year; cout << endl; return Date(monthName, day, year); } else { return Date(); } } /* output: Which Date constructor? (Enter 1, 2, or 3) 1 - Month Number
  • 14. 2 - Month Name 3 - default 3 Numeric: 1/1/2000 Alpha: January 1, 2000 Which Date constructor? (Enter 1, 2, or 3) 1 - Month Number 2 - Month Name 3 - default 2 month name? march day? 32 year? 1994 Invalid date values: Date corrected to 3/31/1994. Numeric: 3/31/1994 Alpha: March 31, 1994 */