Question

In: Computer Science

Create a "Date" class that contains: three private data members: month day year (I leave it...

Create a "Date" class that contains:

three private data members:
month
day
year
(I leave it to you to decide the type)
"setters" and "getters" for each of the data (6 functions in total)
One advantage of a "setter" is that it can provide error checking. Add assert statements to the setter to enforce reasonable conditions. For example, day might be restricted to between 1 and 31 inclusive.
one default constructor (no arguments)
one constructor with three arguments: month, day, and year
Add assert statements to enforce reasonable conditions.
a printDate function. This function will have no arguments and return void
a sameMonth function. This function will have one Date argument and a boolean return type
In main (in the following order):

instantiate one date object (date1) using the default constructor
use the getters to display the month, day, and year of date1 (should print the default values)
read keyboard input from the user for a month, day and year
use the setters to set the values of date1 to the values that came from the user
read keyboard input from the user for a second date
use the constructor with three arguments to instantiate date2 to the second date input from the user
print both objects using printDate
print a message to say if the two months are the same (testing the sameMonth function)
Your code should be in three files:

Date.h
contains the class definition
Date.cpp
includes "Date.h"
contains the functions for the class
main.cpp
includes "Date.h"
tests the class
Sample Output (Two Runs)

>./main
Testing the default constructor and the getters
The initialized date is (M-D-Y):1-1-1

Please enter a date:(Month Day Year): 11 03 1976
Please enter a second date:(Month Day Year): 03 03 1999

Printing the two days:
The date is (M-D-Y): 11-3-1976
The date is (M-D-Y): 3-3-1999
The months are different
>./main
Testing the default constructor and the getters
The initialized date is (M-D-Y): 1-1-1

Please enter a date:(Month Day Year): 12 15 2009
Please enter a second date:(Month Day Year): 12 25 2020

Printing the two days:
The date is (M-D-Y): 12-15-2009
The date is (M-D-Y): 12-25-2020
The months are the same

Solutions

Expert Solution

// Date.h

#ifndef DATE_H
#define DATE_H

class Date
{
public:
// Function declarations
Date();
Date(int month, int day, int year);

int getMonth();
void setMonth(int month);
int getDay();
void setDay(int day);
int getYear();
void setYear(int year);
void printDate();
bool sameMonth(Date d);

private:
// Declaring variables
int month;
int day;
int year;
};
#endif

/****************************************************/

/****************************************************/

// Date.cpp

#include <assert.h>
#include <iostream>
using namespace std;
#include "Date.h"

Date::Date()
{
this->day = 1;
this->month = 1;
this->year = 1;
}
Date::Date(int month, int day, int year)
{
setMonth(month);
setDay(day);
this->year = year;
}

int Date::getMonth()
{
return month;
}
void Date::setMonth(int month)
{
assert(month >= 1 && month <= 12);
this->month = month;
}
int Date::getDay()
{
return day;
}
void Date::setDay(int day)
{
assert(day >= 1 && day <= 31);
this->day = day;
}
int Date::getYear()
{
return year;
}
void Date::setYear(int year)
{
this->year = year;
}
void Date::printDate()
{
cout << "he date is (M-D-Y): " << month << "-" << day << "-" << year << endl;
}
bool Date::sameMonth(Date d)
{
if (this->month == d.getMonth())
return true;
else
return false;
}

/****************************************************/

/****************************************************/

// main.cpp

#include <iostream>
using namespace std;
#include "Date.h"

int main()
{
int month,day,year;
Date date1;
cout<<"The initialized date is (M-D-Y):"<<date1.getMonth()<<"-"<<date1.getDay()<<"-"<<date1.getYear()<<endl;
cout<<"Please enter a date : (Month Day Year):";
cin>>month>>day>>year;
date1.setDay(day);
date1.setMonth(month);
date1.setYear(year);
cout<<"Please enter a second date : (Month Day Year):";
cin>>month>>day>>year;
Date date2(month,day,year);

cout<<"\nPrinting the two days:"<<endl;
date1.printDate();
date2.printDate();
bool b=date1.sameMonth(date2);
if(b)
{
cout<<"The months are the same"<<endl;
}
else{
cout<<"The months are not same"<<endl;
}

return 0;
}


/***********************************************/

/***********************************************/

Output:

/***********************************************/


Related Solutions

Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class in the person class.    First, Create a date class.    Which has integer month, day and year    Each with getters and setters. Be sure that you validate the getter function inputs:     2 digit months - validate 1-12 for month     2 digit day - 1-3? max for day - be sure the min-max number of days is validate for specific month...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
I. Create the class Item with the following members: 1. id, a protected variable of type...
I. Create the class Item with the following members: 1. id, a protected variable of type int 2. name, a protected variable of type string 3. price, a protected variable of type double 4. a public non-default constructor which accepts three parameters to initialize the variables above 5. a copy constructor 6. overload the = operator such that both operands are of type Item 7. overload the = operator such that the right operand is of type double. Assign the...
/////////////////JAVA PLEASE///////////////////////////////// Create a class called GVdate to keep track of a calendar date including month,...
/////////////////JAVA PLEASE///////////////////////////////// Create a class called GVdate to keep track of a calendar date including month, day and year.  You can do simple things like checking if it is your birthday, advancing to the next day, checking if a given date is valid and checking if it is a leap year. Class Fields/Instance Variables Provide appropriate names and data types for each of the private instance variables: the month, day and year (int) two final integers that represent YOUR birthday (month...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT