Question

Create your own date class. Create a MyDate class with these methods. A constructor that accepts...

Create your own date class.

  1. Create a MyDate class with these methods.
    • A constructor that accepts a month, day, and year
    • A method that determines if the year is a leap year
    • A method that returns the date in this format, "1/9/2020"
    • A method that returns the date in this format, "January 9, 2020"
    • A method that returns the day
    • A method that returns the name of the month
    • A method that returns the number of days in the month
    • A method that returns the year
    • A method that returns the season
      • Spring = Mar, Apr, May
      • Summer = Jun, Jul, Aug
      • Fall = Sep, Oct, Nov
      • Winter = Dec, Jan, Feb
  2. Do NOT use any built-in date classes or methods in your date class.
  3. Write a program using your class that
    1. Asks the user for a month, day, and year
    2. Creates a date object
    3. Prints information like the example run below.
    4. Make sure the values line up.

Example run

Please type in a month (1 - 12): 1
Please type in a day (1 - 31): 28
Please type in a year: 2020

Long Date: January 28, 2020
Short Date: 1/28/2020
Month: 1
Month Name: January
Day: 28
Year: 2020
Is Leap Year: Yes
Days in the Month: 31
Season: Winter

Homework Answers

Answer #1

Code

#include<iostream>
using namespace std;

class MyDate
{
    int month,day,year;
    string yrs[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    public:
    MyDate(int m,int d,int y)
    {
        month=m;
        day=d;
        year=y;
    }
    bool isleap()
    {
        if(year%400==0)
            return true;
        else if(year%100==0)
            return false;
        else if(year%4==0)
            return true;
        return false;
    }
    string shortformat()
    {
        string s="";
        s=s+to_string(month)+'/'+to_string(day)+'/'+to_string(year);
        return s;
    }
    string longformat()
    {
        string s="";
        s=s+yrs[month-1]+' '+to_string(day)+", "+to_string(year);
        return s;
    }
    int getday()
    {
        return day;
    }
    int getmonth()
    {
        return month;
    }
    string getmonthname()
    {
        return yrs[month-1];
    }
    int getyear()
    {
        return year;
    }
    int daysinmonth()
    {
        if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
            return 31;
        else if(month==2)
        {
            if(isleap())
                return 29;
            else 
                return 28;
        }
        return 30;
    }
    string season()
    {
        if(month==3 || month==4 || month==5)
            return "Spring";
        if(month==6 || month==7 || month==8)
            return "Summer";
        if(month==9 || month==10 || month==11)
            return "Fall";
        return "Winter";
    }
};
int main()
{
    int m,d,y;
    cout<<"Please type in a month (1 - 12): ";
    cin>>m; //input month
    cout<<"Please type in a day (1 - 31): ";
    cin>>d; //int date 
    cout<<"Please type in a year: ";
    cin>>y; //int year
    MyDate ob(m,d,y); //create object and call the constructor
    cout<<"Long Date: "<<ob.longformat();
    cout<<"\nShort Date: "<<ob.shortformat();
    cout<<"\nMonth: "<<ob.getmonth();
    cout<<"\nMonth Name: "<<ob.getmonthname();
    cout<<"\nDay: "<<ob.getday();
    cout<<"\nYear: "<<ob.getyear();
    cout<<"\nIs Leap Year: "<<ob.isleap();
    cout<<"\nDays in the Month: "<<ob.daysinmonth();
    cout<<"\nSeason: "<<ob.season();
    return 0;
}

Terminal Work

.

Know the answer?
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
Similar Questions
Create the following class in C++ Create a new class call Date. Date will contain the...
Create the following class in C++ Create a new class call Date. Date will contain the date stored as day of the month, month and the year. Provide appropriate set and get methods for the class. Use doxygen comments to document Date.h (Date specification). Think about how the class will be used. Modify the input data file to cater for dates. The date in the data file refers to when the mark for the unit was obtained. Unit test the...
Write the following in Java In this section, you will overload methods to display dates. The...
Write the following in Java In this section, you will overload methods to display dates. The date-displaying methods might be used by many different applications in an organization, such as those that schedule jobs, appointments, and employee reviews. The methods take one, two, or three integer arguments. If there is one argument, it is the month, and the date becomes the first day of the given month in the year 2020. If there are two arguments, they are the month...
Class VacationPackage java.lang.Object triptypes.VacationPackage Constructor Summary Constructors Constructor and Description VacationPackage(java.lang.String name, int numDays) Initializes a...
Class VacationPackage java.lang.Object triptypes.VacationPackage Constructor Summary Constructors Constructor and Description VacationPackage(java.lang.String name, int numDays) Initializes a VacationPackage with provided values. Method Summary All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description boolean equals(java.lang.Object other) Provides a logical equality comparison for VacationPackages and any other object type. double getAmountDue() This method provides the remaining amount due to the travel agent for this trip less any deposit made upfront. abstract double getDepositAmount() This method provides the required...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Please read the requirements: Write a C++ function, date, that will accept a date via a...
Please read the requirements: Write a C++ function, date, that will accept a date via a parameter in the month-day-year format, mmddyyyy, (e.g., 10262020 stands for the date October 26, 2020), and then will determine the corresponding month number, day, and year of that date, returning these three integer values to the calling function via arguments. An example of a call to the function date is shown in the following statement: date (10262020, month, day, year); This call to the...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Lab 3 – Pseudocode and Parallel Arrays This lab requires you to think about the steps...
Lab 3 – Pseudocode and Parallel Arrays This lab requires you to think about the steps that take place in a program by writing pseudocode. Read the following program prior to completing the lab. Design an application in which the number of days for each month in the year is stored in an array. (For example, January has 31 days, February has 28, so on. Assume that the year is not a leap year.) Also, use a parallel array to...
public class Date {    private int month;    private int day;    private int year;...
public class Date {    private int month;    private int day;    private int year;    public Date( int monthValue, int dayValue, int yearValue )    {       month = monthValue;       day = dayValue;       year = yearValue;    }    public void setMonth( int monthValue )    {       month = monthValue;    }    public int getMonth()    {       return month;    }    public void setDay( int dayValue )    {       day =...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT