Question
Hello, can someone complete parts 1 through 4 for the code. Code is already completed but just need it to be modified according to steps
Hello, can someone complete parts 1 through 4 for the code. Code is already completed but just need it to be modified according to steps 1 through 4. Please post screenshot with it compiled that should look like the Expected Program Output below. All the files are below.
Problem
Transform your Part 2 implementation of the DATE class (which should be found in your source files Date2.h and Date2.cpp) so the implementation satisfies all of the following 4 new requirements. Hint Make a copy of Date2.h (call it Date3.h) and make a copy of Date2.cpp (call it Date3.cpp). Notice Dr. H has given you a new client application, Problem-3.cpp. Now, make your changes to these new files.
Run your version of the new client application program and take 1 or more screen shots. Email AHanna@StMaryTX.edu a single Word document which contains your modified source filesall 3 of themand your screen shots.
1) Add a user-defined, explicit, 1-parameter constructor which takes apart (parses) the char date[] parameter to determine values which are used to initialize the MM, DD, and YYYY DATE class data members. Dr. H started the new constructor for you (see below). Note From my dictionary, parsing [Latin pars(orationis) part (of speech)] also called syntactic analysis, is the process of analyzing a string of symbols, expressed either in natural language or in computer language, which conforms to the rules of a grammar. In Computer Science, the term is used in the analysis of computer languages, referring to the syntactic analysis of the input code into its component parts in order to facilitate the writing of compilers and interpreters. The term may also be used to describe a split or separation.
// Prototype for class definition in Date3.h
explicit DATE(char date[]);
// Function definition for Date3.cpp
DATE::DATE(char date[])
{
// Code to "take apart" the parameter date[] belongs here!
cout Output(); cout
}
Notes The instrumentation was changed so we can tell which constructor is being referenced. You may assume the value of date[] is always a 10-character string exactly formatted as "MM/DD/YYYY". To simplify your logic, you may assume no validation need be done. Hint See Chapter #21 of our text book for help learning how to take apart a date[] then converting each date[] part into an integer using the function atoi() (or the function stoi() if your compiler supports the C++11
2) Add a 3-formal-parameter constructor, DATE::DATE(int MM,int DD,int YYYY), which uses a member-initializer list. Dr. Hanna started the new constructor for you (see below). Add the constructor function definition to the DATE class definition in Date3.h so you implicitly define the new constructor it as an inline function.
// Function definition for Date3.h
DATE(int MM,int DD,int YYYY) // Your member-initializer list belongs here
{
cout
}
Notes Dr. H changed the instrumentation so we can tell which constructor is being referenced. Again, to simplify your logic, you may assume no validation of parameter values need be done.
3a) Modify the DATE class definition so it contains the implicit inline function definition (the body) for the set mutator function SetMM(). Note Dont forget to delete the same function definition from the member function definition file Date3.cpp.
3b) Modify the DATE class definition so it contains the explicit inline function definition for the get accessor functions GetMM(), GetDD(), and GetYYYY(). Note Dont forget to delete the same function definitions from the member function definition file Date3.cpp.
4) Add another non-trivial member function to the DATE class and modify the client application program Problem-3.cpp to test your newest implementation of the DATE class. See Part 2 for suggestions!Add the utility function bool IsValid(); as a private member function to provide validation of a date, then make use of the utility function to validate the dates passed to both of the constructors added to the DATE class by Requirements 1) and 2). Note You must decide how to deal with the exceptional condition which occurs when an invalid date is discovered by IsValid(): What ways have you learned to use to handle exceptional conditions? Hint We havent learned how to use C++ exception handling yet (see Chapter #17 of our text book)! Note From my dictionary, validate means to make valid (to make sound or well-founded and capable of producing the desired result; effective); to confirm.
# | Name | Days |
1 | January | 31 |
2 | February | 28 or 29 (see Note) |
3 | March | 31 |
4 | April | 30 |
5 | May | 31 |
6 | June | 30 |
7 | July | 31 |
8 | August | 31 |
9 | September | 30 |
10 | October | 31 |
11 | November | 30 |
12 | December | 31 |
Note Every year which is exactly divisible by 4 is a leap year, except for years which are exactly divisible by 100, but these centurial years are leap years when they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the year 2000 is. The Gregorian calendar is valid for (YYYY 1582).
Expected Program Output
//-------------------------------------------------
// Dr. H
// Chapter #3 Problem, Part 3
// Problem-3.cpp
//-------------------------------------------------
#include
#include
#include
#include ".\Date3.h"
using namespace std;
//-------------------------------------------------
int main()
//-------------------------------------------------
{
void MyMain();
MyMain();
system("PAUSE");
return( 0 );
}
//-------------------------------------------------
void MyMain()
//-------------------------------------------------
{
DATE date1;
DATE date2("01/05/1953");
DATE date3(2,3,1954);
cout
cout
cout
// Causes compiler error message shown below when DATE(char date[]) is an explicit constructor
// no match for 'operator=' in 'date4 = "09/03/2013"'
// date4 = "09/03/2013";
// cout
DATE date4 = DATE("09/03/2013");
cout
date4 = (DATE) "09/03/2014";
cout
date4 = static_cast
cout
}
Date.h
#ifndef DATE_H
#define DATE_H
using namespace std;
enum DAYOFWEEK { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
enum LTEQORGT { LT, EQ, GT };
class DATE {
public:
int MM, DD, YYYY;
int getDaysOfMonth(int m, bool leapYear) const;
public:
void ConstructDate(DATE *date);
void DestructDate(DATE *date);
void InputDate(DATE *date);
void OutputDate(const DATE *date);
void SetDateMM(DATE *date, int MM);
void SetDateDD(DATE *date, int DD);
void SetDateYYYY(DATE *date, int YYYY);
int GetDateMM(const DATE *date);
int GetDateDD(const DATE *date);
int GetDateYYYY(const DATE *date);
friend ostream& operator
friend istream& operator >> (istream &in, DATE &d);
void increment();
void decrement();
void AddDays(unsigned int days);
void AddDays(int days);
DAYOFWEEK DayOfWeek() const;
bool IsLeapYear() const;
unsigned int DaySequenceNumber() const;
bool isLT(const DATE RHS) const;
LTEQORGT ChronologicalRelationShip(const DATE d) const;
unsigned int IntervalInDays(const DATE RHS) const;
DATE DateForDaySequenceNumberAndYear(int sequenceNumber, int year);
};
#endif
Problem.cpp
#include
#include
#include
#include "Date.h"
using namespace std;
//-------------------------------------------------
int main()
//-------------------------------------------------
{
// create objects of class DATE
DATE date1, date2;
DATE d;
// create date 1.
d.ConstructDate(&date1);
// create date 2.
d.ConstructDate(&date2);
// set the month, day, year of the date1.
d.SetDateMM(&date1, 1);
d.SetDateDD(&date1, 30);
d.SetDateYYYY(&date1, 1979);
// print the date 1 using OutputDate() function.
cout
d.OutputDate(&date1); cout
// read the date 2 using the function InputDate();
cout
// print the date2.
cout
cout
cout
// increament the date 1 to next. using increment() function.
date1.increment();
// print date 1 after incrementation.
cout
cout
cout
// increament the date 2 to next. using increment() function.
date2.increment();
// print date 2 after incrementation.
cout
cout
cout
// add 7 days to date 2 using AddDays() functions.
date2.AddDays(7);
// print date 2 after adding days.
cout
cout
cout
// deteremine the day of the date 2.
DAYOFWEEK day =date2.DayOfWeek();
// printthe day of the date 2
cout
// check whether date is leap year or not using IsLeapYear()
bool leap = date2.IsLeapYear();
cout
// calculate the DaySequenceNumber() of date2.
int seq = date2.DaySequenceNumber();
cout
// check of the LT of date2 using isLT() function
bool islt = d.isLT(date2);
cout
// check for the Chronological RelationShip of date 2 using function
// ChronologicalRelationShip().
LTEQORGT lt=d.ChronologicalRelationShip(date2);
cout
// destruct the date1 and date2.
d.DestructDate(&date1);
d.DestructDate(&date2);
system("pause");
return(0);
}
Date.cpp
#include
#include
#include
#include
#include "Date.h"
using namespace std;
void DATE:: ConstructDate(DATE *date)
//-------------------------------------------------
{
date->DD = 12; date->MM = 1; date->YYYY = 1993;
cout
}
//-------------------------------------------------
void DATE::DestructDate(DATE *date)
//-------------------------------------------------
{
cout
}
//-------------------------------------------------
void DATE::InputDate(DATE *date)
//-------------------------------------------------
{
cout > date->MM;
cout > date->DD;
cout > date->YYYY;
}
//-------------------------------------------------
void DATE::OutputDate(const DATE *date)
//-------------------------------------------------
{
// Use the format MM-DD-YYYY
cout MM DD YYYY;
}
//-------------------------------------------------
void DATE::SetDateMM(DATE *date, int MM)
//-------------------------------------------------
{
date->MM = MM;
}
//-------------------------------------------------
void DATE::SetDateDD(DATE *date, int DD)
//-------------------------------------------------
{
date->DD = DD;
}
//-------------------------------------------------
void DATE::SetDateYYYY(DATE *date, int YYYY)
//-------------------------------------------------
{
date->YYYY = YYYY;
}
//-------------------------------------------------
int DATE::GetDateMM(const DATE *date)
//-------------------------------------------------
{
return(date->MM);
}
//-------------------------------------------------
int DATE::GetDateDD(const DATE *date)
//-------------------------------------------------
{
return(date->DD);
}
//-------------------------------------------------
int DATE::GetDateYYYY(const DATE *date)
//-------------------------------------------------
{
return(date->YYYY);
}
ostream& operator
out
out
return out;
}
istream& operator >> (istream &in, DATE &d) {
cout > d.MM;
cout > d.DD;
cout > d.YYYY;
return in;
}
bool DATE::IsLeapYear() const {
int year = this->YYYY;
if (year % 4 != 0)
return false;
if (year % 100 != 0)
return true;
if (year % 400 == 0)
return true;
else
return false;
}
int DATE::getDaysOfMonth(int m, bool leapYear) const {
if ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12)) {
return 31;
}
if ((m == 4) || (m == 6) || (m == 11) || (m == 9)) {
return 30;
}
if (m == 2 && leapYear) {
return 29;
}
else {
return 28;
}
}
void DATE::increment() {
int lastDay = getDaysOfMonth(this->MM, IsLeapYear());
if (this->DD == lastDay)
{
this->DD = 1;
this->MM += 1;
if (this->MM == 13)
{
this->MM = 1;
this->YYYY += 1;
}
}
else
{
this->DD += 1;
}
}
void DATE::decrement() {
if (this->DD == 1) {
this->MM -= 1;
if (this->MM == 0) {
this->MM = 12;
this->YYYY -= 1;
}
this->DD = 1;
int lastDay = getDaysOfMonth(this->MM, IsLeapYear());
this->DD = lastDay;
}
else {
this->DD -= 1;
}
}
void DATE::AddDays(unsigned int days) {
for (int i = 0; i this->increment(); } } void DATE::AddDays(int days) { bool decrem = false; if (days decrem = true; days = days * -1; } for (int i = 0; i if (decrem) { this->decrement(); } else { this->increment(); } } } int dayIndex(int d, int m, int y) { static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; y -= m return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7; } DAYOFWEEK DATE::DayOfWeek() const { int d = dayIndex(this->DD, this->MM, this->YYYY); switch (d) { case 0: return Sunday; case 1: return Monday; case 2: return Tuesday; case 3: return Wednesday; case 4: return Thursday; case 5: return Friday; case 6: return Saturday; } } unsigned int DATE::DaySequenceNumber() const { unsigned int index = 0; for (int i = 1; i MM; i++) { index += this->getDaysOfMonth(i, IsLeapYear()); } index += this->DD; return index; } bool DATE::isLT(const DATE RHS) const { DATE d; if (d.GetDateYYYY(&RHS) YYYY ) { return false; } else if (d.GetDateYYYY(&RHS) > this->YYYY ) { return true; } else { if (d.GetDateMM(&RHS) { return false; } else if (d.GetDateMM(&RHS) > this->MM ) { return true; } else { if (d.GetDateDD(&RHS) > this->DD) { return false; } else { return true; } } } } LTEQORGT DATE::ChronologicalRelationShip(const DATE d) const { DATE date; if (isLT(d)) { return LT; } if ((date.GetDateMM(&d) == this->MM) || (date.GetDateDD(&d) == this->DD) || (date.GetDateYYYY(&d) == this->YYYY)) { return EQ; } return GT; } unsigned int DATE::IntervalInDays(const DATE RHS) const { return 0; } DATE DATE::DateForDaySequenceNumberAndYear(int sequenceNumber, int year) { return DATE(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started