I could use some help with this program. Having trouble getting it to work corre
ID: 3567060 • Letter: I
Question
I could use some help with this program. Having trouble getting it to work correctly.
Problem Description:
Problem Description
Create a Date class with the following capabilities:
MM/DD/YYYY
June 14, 1992
You should only have 3 member data: integer month, integer day, and integer year. Also, you should have at least 2 constructors and 2 functions in your Date.cpp.
You also need to create 3 get functions and 3 set functions for the member data.
a) You must use a separate Date.h file for the Date class definition.
b) You must use a separate Date.cpp file for the Date class implementation.
c) You must use a separate TestDate.cpp file for the Testing program.
d) The Date class should contain only 3 member data: month, day, and year. They are all integer data.
e) Your program should run correctly with the same inputs and outputs as given in the sample run.
Sample Run:
Enter 1 for format: MM/DD/YYYY
Enter 2 for format: Month DD, YYYY
Enter 3 to exit
Choice:
1
Enter Month (1-12): 10
Enter Day of Month: 23
Enter Year: 2006
MM/DD/YYYY: 10/23/2006
Month DD, YYYY: October 23,2006
**********
Enter 1 for format: MM/DD/YYYY
Enter 2 for format: Month DD, YYYY
Enter 3 to exit
Choice:
2
Enter Month Name: May
Enter Day of Month: 9
Enter Year: 1998
month:May
MM/DD/YYYY: 5/9/1998
Month DD, YYYY: May 9,1998
**********
Enter 1 for format: MM/DD/YYYY
Enter 2 for format: Month DD, YYYY
Enter 3 to exit
Choice:
Explanation / Answer
Date.h
#ifndef DateFormat
#define DateFormat
#include <ctime>
#include <cstring>
class Date {
public:
Date();
Date( int, int );
Date( int, int, int );
Date( char *, int, int );
void setMonth( int );
void setDay( int );
void setYear( int );
void printDateSlash( void ) const;
void printDateMonth( void ) const;
void printDateDay( void ) const;
const char *monthName( void ) const;
int leapYear( void ) const;
int daysOfMonth( void ) const;
void convert1( int );
int convert2( void ) const;
void convert3( const char * const );
const char *monthList( int ) const;
int days( int ) const;
private:
int day;
int month;
int year;
};
#endif
Date.cpp
#include <iostream>
#include <cstring>
#include <ctime>
#include "Date.h"
Date::Date()
{
struct tm *ptr;
time_t t = time( 0 );
ptr = localtime( &t );
day = ptr->tm_mday;
month = 1 + ptr->tm_mon;
year = ptr->tm_year + 1900;
}
Date::Date( int ddd, int yyyy )
{
setYear( yyyy );
convert1( ddd );
}
Date::Date( int mm, int dd, int yy )
{
setYear( yy + 1900 );
setMonth( mm );
setDay( dd );
}
Date::Date( char *mPtr, int dd, int yyyy )
{
setYear( yyyy );
convert3( mPtr );
setDay( dd );
}
void Date::setDay( int d )
{ day = d >= 1 && d <= daysOfMonth() ? d : 1; }
void Date::setMonth( int m ) { month = m >= 1 && m <= 12 ? m : 1; }
void Date::setYear( int y ) { year = y >= 1900 && y <= 1999 ? y : 1900; }
void Date::printDateSlash( void ) const
{ cout << month << '/' << day << '/' << year << ' '; }
void Date::printDateMonth( void ) const
{ cout << monthName() << ' ' << day << ", " << year << ' '; }
void Date::printDateDay( void ) const
{ cout << convert2() << ' ' << year << ' '; }
const char *Date::monthName( void ) const { return monthList( month - 1 ); }
int Date::daysOfMonth( void ) const
{ return leapYear() && month == 2 ? 29 : days( month ); }
int Date::leapYear( void ) const
{
if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
return 1;
else
return 0;
}
int Date::convert2( void ) const
{
int ddd = 0;
for ( int m = 1; m < month; ++m )
ddd += days( m );
ddd += day;
return ddd;
}
void Date::convert3( const char * const mPtr )
{
int flag = 0;
for ( int subscript = 0; subscript < 12; ++subscript )
if ( !strcmp( mPtr, monthList( subscript ) ) ) {
setMonth( subscript + 1 );
flag = 1;
break;
}
if ( !flag )
setMonth( 1 );
}
const char *Date::monthList( int mm ) const
{
char *months[] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
return months[ mm ];
}
int Date::days( int m ) const
{
const int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return monthDays[ m - 1 ];
}
int main()
{
Date d1( 7, 4, 98 ), d2( 86, 1999 ),
d3, d4( "September", 1, 1998 );
d1.printDateSlash();
d2.printDateSlash();
d3.printDateSlash();
d4.printDateSlash();
cout << ' ';
d1.printDateDay();
d2.printDateDay();
d3.printDateDay();
d4.printDateDay();
cout << ' ';
d1.printDateMonth();
d2.printDateMonth();
d3.printDateMonth();
d4.printDateMonth();
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.