CSci 1100 Lab 9 — Date class


Category: You will Instantly receive a download link for .zip solution file upon Payment


5/5 - (2 votes)

Checkpoint 1
The dates will be stored with three attributes: the year, the month, and the day of the
month, all as integers. These attributes will be created / assigned in the initializer methods.
Therefore, please implement the following methods for the Date class
• __init__: This should take a year, a month and a day with default values of 1900,
1, 1.
>>> d1 = Date(1972, 3, 27)
>>> d2 = Date(1998) # Will be January 1, 1998
• __str__: Format the data as a string with year/month/day. For example,
>>> d1 = Date(1972, 3, 27)
>>> s = str(d1)
>>> s
>>> s1 = Date(1983, 11, 2)
>>> print(s1)
The example in is especially important here. As a hint for how to insert
the ‘0’ before the ‘3’ in ’03’, you may make use of the rjust method of strings.
For example,
>>> s = ‘5’
>>> s1 = s.rjust(2,’0′)
>>> print(s1)
>>> s = ’21’
>>> s1 = s.rjust(2,’0′)
>>> print(s1)
In the call to rjust, the 2 is the number of spaces, and the ‘0’ is the character to
inserted when there aren’t enough characters in the string to be printed. You could
also use the string format method to generate the string using the {:02d} format
• same_day_in_year: This should determine if two dates are on the same day within
a year, even if they are not within the same year. For example, given
>>> d1 = Date(1972, 3, 27)
>>> d2 = Date(1998, 4, 13)
>>> d3 = Date(1996, 4, 13)
>>> d1.same_day_in_year(d2)
>>> d2.same_day_in_year(d3)
There is already code in the main area of to partially test these. The test code
does not include code for testing the default assignments so you should add that. It also
does not test all of the cases, so you will need to add some of your own.
To complete Checkpoint 1: show your code and the result of running it to a lab TA or
a mentor.
Checkpoint 2 — Date Class Methods
Continuing with the Date class, please implement and test the following two methods, each
of which involves significantly more logic that the first three methods.
• is_leap_year: This should return true if the year is a leap year. Leap years occur
when the year is divisible by 4. The exception to this rule is years that are divisible
by 100 but not 400. Got it? In other words, 2000, 2004, 2008 and 2012 were all leap
years, but 1900, 2002, and 2011 were not. As examples, using the above values of d1
and d2
>>> d1.is_leap_year()
>>> d2.is_leap_year()
• __lt__: This is the “less than” operator and it should return true if the first Date is
earlier than the second.
>>> d1 = Date(1972, 3, 27)
>>> d2 = Date(1998, 4, 13)
>>> d3 = Date(1998, 5, 13)
>>> d4 = Date(1998, 4, 11)
>>> d1 < d2 True >>> d2 < d3 True >>> d3 < d4 False Observe that there is a great deal more functionality that we could (and should) add to the Date class to make it fully functional, but these methods are enough for now. To complete Checkpoint 2: Add code to test your new methods to the main code of Show your code and the test results to a TA or a mentor. Your testing code should show Date objects and method call outputs that test the different conditions that each method has to handle. The example calls above cover some but not all of these conditions. Checkpoint 3 — Birthdays Create a new .py file for Checkpoint 3 that starts with the line from Date import * This will allow you to use the Date class without having to write something like d = Date.Date() which is what you would have to do if you just imported Date. Instead, you can simply write: d = Date() Start by writing a function that reads birthdays from a file (birthdays.txt is the test file we have provided) and returns a list of Date objects. This will be the first time you have worked with lists of objects that we have created, but it is no different from handling lists of, say, string objects. Write main function code that scans through this list to find and output: • the earliest birthday — corresponding to the person who would be the oldest person represented in the file, • the latest birthday — corresponding to the youngest person represented in the file, and • the name of the month that has the most birthdays. Use the month_names list in To complete Checkpoint 3: Show a TA or Mentor your code and output. 3 Additional challenges (no extra credit) Implement and test a function called julian_day that returns an integer giving the “Julian day” corresponding to the current date. The Julian day is the numbered day within the year, with January 1 being Julian day 1, February 1 being Julian day 32, February 28 being Julian day 59, and March 1 being Julian day 60 or 61, depending on whether or not it is a leap year. This method should make use of the days_in_month list. Here are examples of using the method >>> d1 = Date(1972, 3, 27)
>>> d2 = Date(1998, 4, 13)
>>> d1.julian_day()
>>> d2.julian_day()
You must make use of is_leap_year in your julian_day function.