Coding Language C++ Note: Do not make use of coding techniques that are too advanced such as linked lists or recursive functions Must be done using classes Private Member Functions The member...


Coding Language C++


Note: Do not make use of coding techniques that are too advanced such as linked lists or recursive functions



Must be done using classes




Private Member Functions


The member functions declared private, isLeap, daysPerMonth, name, number, are
helperfunctions - 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:



  1. the "Invalid Date" warnings in the constructors

  2. in your two print functions


Required Main Function


You must use the provided main function and global function getDate as they are here. You may not change these functions at all to pass the tests in submit mode.



Transcript of code:


#include
#include


using namespace std;


class Date {
private:
unsigned day;
unsigned month;
string monthName;
unsigned year;


public:
// creates the date January 1st, 2000.
Date();




/* 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(unsigned m, unsigned d, unsigned y);




/* 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(const string &mn, unsigned d, unsigned y);


/* Outputs to the console (cout) a Date exactly in the format "3/15/2012".

Does not output a newline at the end.
*/
void printNumeric() const;




/* Outputs to the console (cout) a Date exactly in the format "March 15, 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 printAlpha() const;


private:


/* Returns true if the year passed in is a leap year, otherwise returns false.
*/
bool isLeap(unsigned y) const;




/* 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 daysPerMonth(unsigned m, unsigned y) const;


/* Returns the name of a given month
- e.g. name(12) returns the string "December"
*/
string name(unsigned m) const;


/* Returns the number of a given named month
- e.g. number("March") returns 3
*/
unsigned number(const string &mn) const;
};




// Implement the Date member functions here






// Don't change the code below this line.
// You may comment them out if you want to build your own test harness
// while in develope mode, but you will need these to pass tets in submit mode.


Date getDate();


int main() {


Date testDate;
testDate = getDate();
cout <>
cout < "numeric:="">
testDate.printNumeric();
cout <>
cout < "alpha:="">
testDate.printAlpha();
cout <>


return 0;
}


Date getDate() {
int choice;
unsigned monthNumber, day, year;
string monthName;


cout < "which="" date="" constructor?="" (enter="" 1,="" 2,="" or="" 3)"=""><>
< "1="" -="" month="" number"=""><>
< "2="" -="" month="" name"=""><>
< "3="" -="" default"=""><>
cin >> choice;
cout <>


if (choice == 1) {
cout < "month="" number?="">
cin >> monthNumber;
cout <>
cout < "day?="">
cin >> day;
cout <>
cout < "year?="">
cin >> year;
cout <>
return Date(monthNumber, day, year);
} else if (choice == 2) {
cout < "month="" name?="">
cin >> monthName;
cout <>
cout < "day?="">
cin >> day;
cout <>
cout < "year?="">
cin >> year;
cout <>
return Date(monthName, day, year);
} else {
return Date();
}
}








#include <iostream><br>#include <string><br>using namespace std;<br>class Date {<br>private:<br>unsigned day;<br>unsigned month;<br>string monthName;<br>unsigned year;<br>public:<br>// creates the date January 1st, 2000.<br>Date();<br>/* parameterized constructor: month number, day, year<br>- e.g. (3, 1, 2010) will construct the date March 1st, 2010<br>If any of the arguments are invalid (e.g. 15 for month or 32 for day)<br>then the constructor will construct instead a valid Date as close<br>as possible to the arguments provided - e.g. in above example,<br>Date(15, 32, 2010), the Date would be corrected to Dec 31st, 2010.<br>In case of such invalid input, the constructor will issue a console error message:<br>Invalid date values: Date corrected to 12/31/2010.<br>(with a newline at the end).<br>* /<br>Date(unsigned m, unsigned d, unsigned y);<br>/* parameterized constructor: month name, day, year<br>- e.g. (December, 15, 2012) will construct the date December 15th, 2012<br>If the constructor is unable to recognize the string argument as a valid month nam<br>then it will issue a console error message:<br>Invalid month name: the Date was set to 1/1/200e.<br>(with a newline at the end).<br>If the day argument is invalid for the given month (but the month name was valid),<br>then the constructor will handle this error in the same manner as the other<br>parameterized constructor.<br>This constructor will recognize both

Extracted text: #include #include using namespace std; class Date { private: unsigned day; unsigned month; string monthName; unsigned year; public: // creates the date January 1st, 2000. Date(); /* 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(unsigned m, unsigned d, unsigned y); /* 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 nam then it will issue a console error message: Invalid month name: the Date was set to 1/1/200e. (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(const string &mn, unsigned d, unsigned y); /* Outputs to the console (cout) a Date exactly in the format "3/15/2012". Does not output a newline at the end. * / void printNumeric() const; /* Outputs to the console (cout) a Date exactly in the format "March 15, 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 printAlpha() const; private: /* Returns true if the year passed in is a leap year, otherwise returns false. * / bool isLeap(unsigned y) const; /* 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 daysPerMonth(unsigned m, unsigned y) const; /* Returns the name of a given month - e.g. name (12) returns the string "December" */ string name(unsigned m) const; /* Returns the number of a given named month - e.g. number("March") returns 3 */ unsigned number(const string &mn) const; }; // Implement the Date member functions here
/* Returns true if the year passed in is a leap year, otherwise returns false.<br>*/<br>bool isleap(unsigned y) const;<br>/* Returns number of days allowed in a given month<br>e.g. daysPerMonth(9, 2000) returns 30.<br>Calculates February's days for leap and non-leap years,<br>thus, the reason year is also a parameter.<br>-<br>unsigned daysPerMonth(unsigned m, unsigned y) const;<br>/* Returns the name of a given month<br>- e.g. name(12) returns the string > 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();="" }="" }="" "/="">
Extracted text: /* Returns true if the year passed in is a leap year, otherwise returns false. */ bool isleap(unsigned y) const; /* 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 daysPerMonth(unsigned m, unsigned y) const; /* Returns the name of a given month - e.g. name(12) returns the string "December" */ string name(unsigned m) const; /* Returns the number of a given named month - e.g. number("March") returns 3 * / unsigned number(const string &mn) const; }; // Implement the Date member functions here // Don't change the code below this line. // You may comment them out if you want to build your own test harness // while in develope mode, but you will need these to pass tets in submit mode. 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(); } } endl;="" return="" date(monthname,="" day,="" year);="" }="" else="" {="" return="" date();="" }="">

Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here