Question

Program Description: Design a class named Date. The class should store a date in three integers:...

Program Description:
Design a class named 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:
3/29/1988
29 MARCH 1988
MARCH 29, 1988
Input Validation:
 Do not accept values for day greater than 31 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 2050 or less than 1900.
Be sure to your program with several different dates, but for your output you report to me, use the exact test values from the example output.
Helpful notes:
 You should create a .h file and two different .cpp files for this lab.
 You will not need to create constructors/a destructor, but you may create constructor(s) if you wish
 You should consider making a private helper function for turning the month into its word equivalent.
 There is a method in the string class called to_string() that will take an integer and return the string representation of it. You may find this useful.
Grading Rubric:
1. Correctness (meets specification) 5 points
2. Design (well-constructed solution) 2
3. Validation & Verification (Testing) 2
4. Style (readable for your client – me) 1
Total 10 points
Example output:
Enter a day (1-31): -2
Enter a day (1-31): 29
Enter a month (1-12): 13
Enter a month (1-12): 8
Enter a year (1900-2050): 2076
Enter a year (1900-2050): 1938
Here is your date in 3 different formats:
8/29/1938
29 AUGUST 1938
AUGUST 29, 1938

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

// date.h

#ifndef DATE_H_

#define DATE_H_

#include <string>

using namespace std;

class Date

{

private:

       int day, month, year;

       static string monthNames[] ;

public:

       void setDay(int day);

       void setMonth(int month);

       void setYear(int year);

       int getDay();

       int getMonth();

       int getYear();

       string getMMDDYYYY();

       string getDayMonthYear();

       string getMonthDayYear();

};

#endif /* DATE_H_ */

//end of date.h

// date.cpp

#include "date.h"

// initialize the monthNames array

string Date::monthNames[] = {"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};

// function to set the day

void Date:: setDay(int day)

{

       if(day >=1 && day <=31)

             this->day = day;

       else

             this->day = 1;

}

// function to set the month

void Date:: setMonth(int month)

{

       if(month >=1 && month <= 12)

             this->month = month;

       else

             this->month = 1;

}

// function to set the year

void Date:: setYear(int year)

{

       if(year >=1900 && year <=2050)

             this->year = year;

       else

             this->year = 1900;

}

// function to return the day

int Date:: getDay()

{

       return day;

}

// function to return the month

int Date:: getMonth()

{

       return month;

}

// function to return the year

int Date:: getYear()

{

       return year;

}

// function to return the date in the format mm/dd/yyyy

string Date:: getMMDDYYYY()

{

       return to_string(month)+"/"+to_string(day)+"/"+to_string(year);

}

// function to return the date in the format dd month yyyy

string Date:: getDayMonthYear()

{

       return to_string(day)+" "+monthNames[month-1]+" "+to_string(year);

}

// function to return the date in the format month dd, yyyy

string Date:: getMonthDayYear()

{

       return monthNames[month-1]+" "+to_string(day)+", "+to_string(year);

}

//end of date.cpp

// main.cpp

//main.cpp : Driver program to test the Date class

#include "date.h"

#include <iostream>

using namespace std;

int main()

{

       int day, month, year;

       cout<<"Enter a day(1-31) : ";

       cin>>day;

       while(day < 1 || day > 31)

       {

             cout<<"Enter a day(1-31) : ";

             cin>>day;

       }

       cout<<"Enter a month(1-12) : ";

       cin>>month;

       while(month < 1 || month > 12)

       {

             cout<<"Enter a month(1-12) : ";

             cin>>month;

       }

       cout<<"Enter a year(1900-2050) : ";

       cin>>year;

       while(year < 1900 || year > 2050)

       {

             cout<<"Enter a year(1900-2050) : ";

             cin>>year;

       }

       Date date;

       date.setDay(day);

       date.setMonth(month);

       date.setYear(year);

       cout<<"\nHere is your date in 3 different formats:"<<endl;

       cout<<date.getMMDDYYYY()<<endl;

       cout<<date.getDayMonthYear()<<endl;

       cout<<date.getMonthDayYear()<<endl;

       return 0;

}

//end of main.cpp

Output:

Add a comment
Know the answer?
Add Answer to:
Program Description: Design a class named Date. The class should store a date in three integers:...
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
  • 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...

  • Create class Date with the following capabilities: a) Output the date in multiple formats, such as...

    Create class Date with the following capabilities: a) Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day...

  • 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...

  • 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...

  • 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:...

  • Create a class called Date212 to represent a date. It will store the year, month and...

    Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...

  • - 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...

  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate class) Design a class named MyDate. The class contains: • The data fields year, month, and day that represent a date. Month is 0-based, i.e., 0 is for January. • A no-arg constructor that creates a MyDate object for the current date. • A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. • A constructor hat constructs a MyDate object with the specified year, month, and...

  • 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...

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