Question

Please write the program in C++ Problem Description: Design a class called Date. The class should...

Please write the program in C++

Problem Description:

  • Design a class called Date. The class should store a date in three integers: month, day, and year.    
  • Create the necessary set and get methods.      
  • Design member functions to return the date as a string in the following formats:

                                    12/25/2012

                                    December 25, 2012

                                    25 December 2012

Input Validation:

  • Do not accept values for day greater than 30 or less than 1.
  • Do not accept values for month greater than 12 or less than 1.
  • Do not accept values for year greater than 2025 or less than 1900.

Include the following methods:

Default Constructor: Initializes Date object to Jan 1, 1900

Parameterized Constructor

Copy Constructor

Destructor

static isValidYear predicate method: valid range: 1900 to current year.

static isValidMonth predicate method

static isValidDay predicate method for a given month

static isLeapYear predicate method

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of the code:

Sample Output:

Code to copy:

//Include required header files.

#include <iostream>

//Use the standard naming convention.

using namespace std;

//Define the class Date.

class Date

{

//Declare the required private member variables.

private:

    int day, month, year;

//Declare the prototype of required public

//member functions.

public:

    Date();

    Date(int d, int m, int y);

    Date(Date &dateObj);

    ~Date();

    void setDay(int d);

    void setMonth(int m);

    void setYear(int y);

    int getDay();

    int getMonth();

    int getYear();

    void dateFromatOne();

    void dateFormatTwo();

    void dateFormatThree();

    string getMonthName(int month);

    static bool isValidDay(int d);

    static bool isValidMonth(int m);

    static bool isValidYear(int y);

    static bool isLeapYear(int y);

};

//Define the default constructor of the class.

Date::Date()

{

//Initialize the member variables of the class with

//default values.

day = 1;

month = 1;

year = 1900;

}

//Define the parameterized constructor of the class.

Date::Date(int d, int m, int y)

{

//Set the required values of the member variables of

//the class by calling required setter functions.

setDay(d);

setMonth(m);

setYear(y);

}

//Define the copy constructor if the class.

Date::Date(Date &dateObj)

{

//Call the setter functions to set the values of

//member variables of the class with the values of

//the date class object given in the parameter.

setDay(dateObj.getDay());

setMonth(dateObj.getMonth());

setYear(dateObj.getYear());

}

//Define the destructor of the class.

Date::~Date()

{}

//Define the required setter functions for the member

//variables of the class.

void Date::setDay(int d)

{

//Start a while loop till the day value is not valid.

while(!isValidDay(d))

{

    //Keep prompting the user to enter the valid day

    //value.

    cout << "\nInvalid Day! Please enter a valid day ";

    cout << "between 1 and 30: ";

    cin >> d;

}

//Set the day value of the class.

day = d;

}

//Define the setMonth() function.

void Date::setMonth(int m)

{

//Start a while loop till the month value is not

//valid.

while(!isValidMonth(m))

{

    //Keep prompting the user to enter the valid

    //month value.   

    cout << "\nInvalid Month! Please enter a valid day between 1 and 12: ";

    cin >> m;

}

//Set the month value of the class.

month = m;

}

//Define the setYear() function.

void Date::setYear(int y)

{

//Start a while loop till the year value is not valid.

while(!isValidYear(y))

{

    //Keep prompting the user to enter the valid year

    //value.  

    cout << "\nInvalid Year! Please enter a valid day between 1900 and 2025: ";

    cin >> y;

}

  //Set the year value of the class.

year = y;

}

//Define the required getter functions of the class.

int Date::getDay()

{

return day;

}

int Date::getMonth()

{

return month;

}

int Date::getYear()

{

return year;

}

//Define the function dateFromatOne().

void Date::dateFromatOne()

{

//If the month value is less than 10, then display the

//month value a 0.

if(month < 10)

{

    cout << "0" << month << "/";

}

else

{

    cout << month << "/";

}

//If the day value is less than 10, then display the

//day value with a 0.

if(day < 10)

{

    cout << "0" << day << "/";

}

else

{

    cout << day << "/";

}

cout << year << endl;

}

//Define the function dateFormatTwo().

void Date::dateFormatTwo()

{

//Display the month name by calling the function

//getMonthName().

cout << getMonthName(month) << " ";

//If the day value is less than 10, then display the

//day value with a 0.

if(day < 10)

{

    cout << "0" << day << ", ";

}

else

{

    cout << day << ", ";

}

cout << year << endl;

}

//Define the function dateFormatThree().

void Date::dateFormatThree()

{

//If the day value is less than 10, then display the

//day value with a 0.

if(day < 10)

{

    cout << "0" << day << " ";

}

else

{

    cout << day << " ";

}

//Display the month name by calling the function

//getMonthName().

cout << getMonthName(month) << " ";

cout << year << endl;

}

//Define the function getMonthName().

string Date::getMonthName(int month)

{

//Declare and initialize the required variable to

//store the month name.

string monthName = "";

//Store the required month names to the variable

//monthName based on the value of the variable

//month.

if(month == 1)

{

    monthName = "January";

}

else if(month == 2)

{

    monthName = "February";

}

else if(month == 3)

{

    monthName = "March";

}

else if(month == 4)

{

    monthName = "April";

}

else if(month == 5)

{

    monthName = "May";

}

else if(month == 6)

{

    monthName = "June";

}

else if(month == 7)

{

    monthName = "July";

}

else if(month == 8)

{

    monthName = "August";

}

else if(month == 9)

{

    monthName = "September";

}

else if(month == 10)

{

    monthName = "October";

}

else if(month == 11)

{

   monthName = "November";

}

else if(month == 12)

{

    monthName = "December";

}

//Return the value of the monthName.

return monthName;

}

//Define the function isValidDay().

bool Date::isValidDay(int d)

{

//If the day value is less than or equal to 0 or

//greater than 30, then return false.

if(d <= 0 || d > 30)

{

    return false;

}

//Otherwise, return true.

else

{

    return true;

}

}

//Define the function isValidMonth().

bool Date::isValidMonth(int m)

{

//If the month is less than or equal to 0 or greater

//than 12, then return false.

if(m <= 0 || m > 12)

{

    return false;

}

//Otherwise, return true.

else

{

    return true;

}

}

//Define the function isValidYear().

bool Date::isValidYear(int y)

{

//If the year is less than 1900 or greater than 2025,

//then return false.

if(y < 1900 || y > 2025)

{

    return false;

}

//Otherwise, return true.

else

{

    return true;

}

}

//Define the function isLeapYear().

bool Date::isLeapYear(int y)

{

//If the year is fully divisible by 4, then return

//true.

if((y % 400) == 0)

{

   return true;

}

//If the year is fully divisible by 100, then return

//false.

if((y % 100) == 0)

{

    return false;

}

//If the year is fully divisible by 4, then return

//true.

if((y % 4) == 0)

{

    return true;

}

//Otherwise, return false.

return false;

}

//Start the execution of the main() method.

int main()

{

//Create an object of the class Date.

Date d1;

//Display the date in three different formats.

cout << "Date for object d1 in different formats:";

cout << endl;

d1.dateFromatOne();

d1.dateFormatTwo();

d1.dateFormatThree();

//Show if the current year is a leap year.

cout << "Is " << d1.getYear() << " a leap year? ";

//If the result of the function call isLeapYear() is

//true, then display yes otherwise, display no.

if(Date::isLeapYear(d1.getYear()))

{

    cout << "Yes" << endl;

}

else

{

    cout << "No" << endl;

}

//Create an object of the class Date by passing

//required parameters to the Date class object.

Date d2(25, 12, 2012);

//Display the date in three different formats.

cout << "\nDate for object d2 in different formats:";

cout << endl;

d2.dateFromatOne();

d2.dateFormatTwo();

d2.dateFormatThree();

//Show if the current year is a leap year.

cout << "Is " << d2.getYear() << " a leap year? ";

//If the result of the function call isLeapYear() is

//true, then display yes otherwise, display no.

if(Date::isLeapYear(d2.getYear()))

{

    cout << "Yes" << endl;

}

else

{

    cout << "No" << endl;

}

//Create another object.

Date d3(32, 12, 2020);

//Display the date in three different formats.

cout << "\nDate for object d3 in different formats:";

cout << endl;

d3.dateFromatOne();

d3.dateFormatTwo();

d3.dateFormatThree();

//Show if the current year is a leap year.

cout << "Is " << d3.getYear() << " a leap year? ";

//If the result of the function call isLeapYear() is

//true, then display yes otherwise, display no.

if(Date::isLeapYear(d3.getYear()))

{

    cout << "Yes" << endl;

}

else

{

    cout << "No" << endl;

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Please write the program in C++ Problem Description: Design a class called Date. The class should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • - Classes Write a C++ program that creates a class called Date that includes three pieces...

    - Classes Write a C++ program that creates a class called Date that includes three pieces of information as data members, namely:  month (type int)  day (type int)  year (type int). Program requirements: - Provide set and a get member functions for each data member. - For the set member functions assume that the values provided for the year and day are correct, but in the setMonth member function ensure that the month value is in the...

  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

  • Create a new project called Date In This new project you are going to design a...

    Create a new project called Date In This new project you are going to design a class of Date. This mean that you need to create functions within the class to validate the Date for example: is it a leap year? How many days a months has? How many months are? Create a simple main menu capable of entering a date and validating a date. Instructions • Create a class of Date with attribues and member functions (remember the use...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • Write a class called Date that represents a date consisting of a year, month, and day....

    Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by the given number of seven-day weeks. public int daysTo( Date...

  • Use Java and creat proper classes to finish this problem Write a class called Date that...

    Use Java and creat proper classes to finish this problem Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT