Question: The Date class Save your copy as date.py Notice that in this Date class there are three attributes, also known as data members: An attribute

The Date class
Save your copy as date.py
Notice that in this Date class there are three attributes, also known as data members:
An attribute holding the month (this is self.month)
An attribute holding the day of the month (this is self.day)
An attribute holding the year (this is self.year)
The Date class should have an __init__ method and a __str__ method. As we've discussed in class,
the double underscores before and after these method names indicate that these methods are special
methods that Python knows to look for. In the case of __init__, this is the method that Python looks for
when making a new Date object. In the case of __str__, this is the method that Python looks for when it
needs to represent the object as a string.
Q1(25pt) : implement the __init__(self, month, day, year) method and
__str__(self) method. For __str__ method, it should construct and return a string with the month,
day, and the year, formatted nicely to have exactly two digits places for the month, two digit places for the
day, and four for the year, e.g.,
>>> d = Date(1,31,2021)
>>> str(d)
>>>01/31/2021
Now, implement a few methods for the Date class from scratch. Be sure to add a docstring/method
specification to each of the methods you write!
Q2(25 pt) : add the method isLeapYear(self) to your Date class. The method should return True if
the current object represents a Leap year, return False otherwise. Please check Wikipedia for the
definition of Leap year. E.g.,
>>> d = Date(11,8,2011)
>>> d.isLeapYear()
False
>>> d2= Date(3,15,2012)
>>> d2.isLeapYear()
True
Q3(25 pt) : add the method tomorrow(self) to your Date class. This method should NOT RETURN
ANYTHING! Rather, it should change the calling object so that it represents one calendar day after the
date it originally represented. This means that self.day will definitely change. What's
more, self.month and self.year might change. You may define the list DIM =
(0,31,28,31,30,31,30,31,31,30,31,30,31) at the very top of your python file to help you
determine how many days there are in any particular month (self.month).
>>> d = Date(12,31,2010)
>>> str(d)
12/31/2010
>>> d.tomorrow()
>>> str(d)
01/01/2011
>>> d = Date(2,28,2012)
>>> d.tomorrow()
>>> str(d)
02/29/2012
>>> d.tomorrow()
>>> str(d)
03/01/2012
Q4(25 pt) : add the method isBefore(self, d2)to your Date class. This method should
return True if the calling object is a calendar date before the input named d2(which will always be an
object of type Date). If self and d2 represent the same day, this method should return False. Similarly,
if self is after d2, this should return False.
>>> d = Date(11,11,2011)
>>> d2= Date(1,1,2012)
>>> d.isBefore(d2)
True
>>> d2.isBefore(d)
False
>>> d.isBefore(d)
False

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!