Craft 6

Beautiful Code
Ka-Ping Yee

Make sure you understand the material in Explore 6 before you do this assignment.

This assignment will be done in partners.

Calvin — Scott
Derek — Thanh
Jason — Jun
Nerissa — Karl
David — Omair
Richard — Kevin
Morgan — Nadia
Varun — Hunter
Peterson — Chris
Peter — Jacob

Design and implement a class called Date for representing a date in the Western calendar (i.e. year, month, day). Put this class in a file called date.py so it can be imported as a module. Your class should support:

Your module should provide two ways of creating Date objects:

It is up to you to design the module as you see fit. You get to choose the method names, create any helper functions you need, and so on. You can decide whether one of the string formats should be produced by __str__, or whether you want two separate methods. You can decide whether you want the Date constructor (that is, Date._init__) to support both kinds of creation, or whether you want the Date constructor to make dates one way and to have a separate function in your module to make dates the other way.

Note: Your date module should not import anything.

Details: You will need to take care of leap years. Any year that is a multiple of 4 is a leap year, but century years (multiples of 100) are not leap years. However, every fourth century year (multiple of 400) is a leap year. In a leap year, February has 29 days; in non-leap years, February has 28 days.

Advice: It will be useful for you to have a function that decides whether a given year is a leap year, and another function to figure out how many days are in a given month of a given year.

Please document the behaviour of all your methods and functions. (Methods have docstrings at the beginning, just like functions do.) Be precise in specifying what they do. Again, you don't have to write reams of text; just specify exactly what a user of the module would need to know. Your module should also have a docstring at the very top, to describe the general purpose of the module and to explain anything that isn't already explained in the other docstrings.

Based on the page produced by calling help() on your imported module, a reasonably intelligent person should be able to predict what results your class or module would produce in any situation. For example, by referring to your docstrings, it should be possible to answer questions like: How are negative numbers handled? Exactly what date formats are accepted? Are single-digit months or days accepted? What about two-digit years? Are strings with trailing or leading spaces acceptable?

Above all, aim to make your implementation and specification match. (It goes both ways: make sure the module correctly performs as you have specified, and make sure you have accurately and precisely specified what it does.)

Log in to submit this assignment.


This assignment is due on Monday 3 March at 6 pm. You get a bonus if the assignment is submitted at least 24 hours early.