logo CodeStepByStep logo

Date_year

Language/Type: Python classes

Suppose a class of objects named Date has been already written that contains the following members:

member name type description
Date(month, day) constructor constructs a new date representing the given month and day
d._month
d._day
data attributes attributes to store the month and day
d.month property property to get/set the month
d.day property property to get/set the day
d.advance() method advances to the next day, wrapping to the next month and/or year if necessary
d.days_in_month() method returns the number of days in the month stored by your date object
d == other method returns True if the two dates have the same state
str(d) method returns a string representation such as "7/4" for July 4

Modify your Date class and submit an entire new version of the class with the following changes:

  • Add a year property to the Date class so that it represents not only a month and date but also a specific year.
  • Add a method is_leap_year that returns True if the given year is a leap year. Leap years are ones that are divisible by 4, except for those that are divisible by 100 but not by 400. For example, 1996 is a leap year, as is 2000; but 1997, 1979, 1700, 1800, and 1900 are not.

You'll also need to modify other methods as appropriate to respond to your new year state. Specifically:

  • Modify the constructor to accept a year, month, and day as parameters, in that order.
  • Modify the advance method to properly advance the year if you go from December 31 to January 1.
  • Modify the days_in_month method to incorporate leap years. Specifically, the month of February has 29 days during a leap year, not 28.
  • Modify the __eq__ method to compare the year as well as the month and date.
  • Modify the __str__ method to include the year at the start of the returned string, such as "1979/9/19".

Submit the complete new class with all modifications described above.

Class: Write a complete Python class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.