Building a Date Class
In this exercise, we’ll be creating our own datatype: a class called Date.
The Date Class
Here is some starter code for the Date class.
The Date class has three attributes:
yearto hold the yearmonthto hold the month of the yeardayto hold the day of the month
The parameters year, month, and day are different than the attributes year, month, and day. The parameters are variables whose values are only accessible from the __init__ method during the creation of a Date object while code in __init__ is being executed. On the other hand, the attributes are bound to specific Date objects and can be accessed as long as the object still exists by prepending self..
The Date class has three methods:
__init__is the constructor. A constructor is a method that contains code that gets executed for newDateobjects as they are created.__repr__is a method that gets called when aDateobject is used in a print statement or any string context. When you callprint(d)for someDateobjectd, the__repr__method is called to generate the string that gets printed.isLeapYearis a user-defined method that returns whether aDateobject’s associated year is a leap year.
Methods
Traditionally, functions called by objects are called methods. Methods are just special functions that are defined with respect to a class and get called by specific objects of that class. For example:
Even without passing any parameters as input to a method, methods have access to any data associated with the object they’re called on. You can see in isLeapYear that self.year is used to access the year attribute of whichever Date object calls it.
Self
In the above example, a method defined once in the Date class is called twice — on different Date objects. In a method definition (where the function is defined in the class), the self keyword is used to refer to the object that called the method.
If you take a look at the methods in the Date class starter code, you’ll see that the first parameter in all of them is self. Methods in Python always take self as their first parameter. Even in the case of isLeapYear where there are no parameters passed when it’s called, it still has the self parameter because it is a method.
Getting Started
Let’s mess around with our Date class to get familiar:
More Methods
Now, let’s add two methods to our Date class: copy and equals.
Copy
Make sure this goes inside your Date class!
The copy method returns a newly constructed Date object with the same associated year, month, and day as the Date object being copied. We can check this by look at memory addresses with the id function.
You can see that both d and d2 hold the same date but are two distinct Date objects stored in different places.
Equals
The equals method returns True if the calling object (named self) and the input object (named d2) represent the same calendar date, otherwise False. That is to say, the year, month, and day attributes for each object are equal.
Exercises
Problem 1 Write the tomorrow method for Date.
tomorrow should not return anything. Rather, it should change the date stored in the calling object so that it represents the next day.
Problem 2 Write the yesterday method for Date.
yesterday should not return anything. Rather, it should change the date stored in the calling object so that it represents the previous day.
Problem 3 Write the addNDays method for Date.
addNDays takes one argument as input (N, a nonnegative integer). It should not return anything. Rather, it should change the calling object so that it represents N calendar days following the original date.
How might you use tomorrow to implement this?
addNDays should also print (including the original and new date) each date between the original and new date. Remember that we can print Dates just by passing a Date object to print.
You can make your own tests here.
Problem 4 Write the subNDays method for Date.
subNDays takes one argyment (N, a nonnegative integer). It should not return anything. Rather, it should change the calling object so that it represents N calendar days before the original date.
How might you use yesterday to implement this?
subNDays should also print (including the original and new date) each date between the original and new date. Remember that we can print Dates just by passing a Date object to print.
For testing, try reversing the cases used for testing addNDays.
Problem 5 Write the isBefore method for Date.
isBefore takes one argument (d2, a Date object) and returns True if the date associated with the calling object occurs before the date associated with d2. Otherwise, it returns False.
Problem 6 Write the isAfter method for Date.
isAfter takes one argument (d2, a Date object) and returns True if the date associated with the calling object occurs after the date associated with d2. Otherwise, it returns False.
For testing, try reversing the cases used for testing isBefore.
Problem 7 Write the diff method for Date.
diff takes one argument (d2, a Date object) and returns an integer representing the number of days between the calling object and d2.
Unlike previous methods implemented thusfar, diff should NOT change the calling object or d2. If you want to manipulate one of them, you’ll have to make a copy first. The sign of the returned integer is important: negative when self is before d2 and positive when self is after d2.
Don’t try and count years and months between the dates — just stick to days. On that same note, don’t use addNDays or subNDays either. Using addNDays or subNDays amounts to checking every possible difference between two days, which is too slow.
Problem 8 Write the dow method for Date.
dow takes no arguments and returns a string representing the day of the week of the date associated with the calling Date object.
How might it help to find the diff from a known date, like Tuesday, November 11, 2008? How might the mod operator ( % ) help?
Extra Bonus Challenge
Write the nyCounter function. This is not a method so should not be defined in the Date class. However, it does use the Date class!
nyCounter takes two arguments (start and stop, both integers representing years) and prints how often New Year’s Day occurs on each day of the week.