logo CodeStepByStep logo


Language/Type: Python classes
Related Links:

Add a method named shift to the Date class that adjusts the state of a Date object forward or backward based on an integer parameter. 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
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

Add a mutator method to the Date class named shift that moves the date forward or backward in time by the given number of days, wrapping months as necessary. For example, September 19 shifted by 7 days is September 26, and September 30 shifted by 1 day is October 1. You should also handle negative numbers; September 19 shifted by -7 days is September 12, and September 12 shifted by -15 days is August 28. For example:

d = Date(7, 4)
# now d.month is 7, d.day is 10
Member function: Write a member function that will become part of an existing class. You do not need to write the complete 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.