Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python question: a Date class Please refer to https://www.chegg.com/homework-hel

ID: 3865665 • Letter: P

Question

Python question: a Date class

Please refer to https://www.chegg.com/homework-help/questions-and-answers/methods-python-class-date-class-stores-manipulates-dates-represented-day-month-year-constr-q13439755

for the previous questions and solutions.

6. Write a method diff(self, other) that returns an integer that represents the number of days between self and other.

Notes:

This method should not change self nor should it change other during its execution.

The sign of the return value is important! In particular:

If self and other represent the same calendar date, this method should return 0.

If self is before other, this method should return a negative integer equal to the number of days between the two dates.

If self is after other, this method should return a positive integer equal to the number of days between the two dates.

Suggested Approach:

Since this method should not change the original objects, you should first create true copies of self and other.

Then, use is_before or is_after to figure out which date comes first.

You can use the tomorrow method that you have already written in a similar way to how you used it in the add_n_days method to count up from one date to another. However, unlike in that method, in diff it is not clear how many times you need to call tomorrow to get an appropriate count from one date to the other. What kind of loop is well-suited for this kind of problem?

Once you know how many days separate the two values, you can again use is_before or is_after to figure out whether the returned result should be positive or negative.

You should not try to subtract years, months, and days between the two dates. This technique is too prone to mistakes.

You should also not try to use add_n_days to implement your diff method. Checking all of the possible difference amounts will be too slow!

Examples:

Write a method day_of_week(self) that returns a string that indicates the day of the week of the Date object that calls it. In other words, the method should return one of the following strings: 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'.

Suggested Approach:

Try using the diff method from a known date. For example, how could it help to know the number of days between the called object and a Date object representing Monday, April 4, 2016? How might the modulus (%) operator help?

Calling diff will give you a negative number if the Date you are operating on comes before the known date used by day_of_week. You should leave the result as a negative number in such cases; you should not take its absolute value.

It will be useful to copy and paste the following list to the first line of your method:

Examples:

Explanation / Answer

class Date:
def __init__(self, month, day, year):
self.month = month
self.day = day
self.year = year


def __repr__(self):
s = "%02d/%02d/%04d" % (self.month, self.day, self.year)
return s


def isLeapYear(self):
if self.year % 400 == 0: return True
elif self.year % 100 == 0: return False
elif self.year % 4 == 0: return True
return False
  
def copy(self):
dnew = Date(self.month, self.day, self.year)
return dnew
  
  
  
def tomorrow(self):
fdays = 28 + self.isLeapYear()
DIM = [0,31,fdays,31,30,31,30,31,31,30,31,30,31]
self.day += 1
if self.day > DIM[self.month]:
self.day = 1
self.month += 1
if self.month == 13:
self.year += 1
self.month = 1
def yesterday(self):
fdays = 28 + self.isLeapYear()
DIM = [0,31,fdays,31,30,31,30,31,31,30,31,30,31]
self.day -= 1
if self.day < 1:
self.month -= 1
if self.month < 1:
self.year -= 1
self.month = 12
self.day = DIM[self.month]
def isBefore (self, d2):
if d2.year != self.year:
return self.year < d2.year
if d2.month != self.month:
return self.month < d2.month
return self.day < d2.day
def isAfter (self, d2):
if d2.year != self.year:
return self.year > d2.year
if d2.month != self.month:
return self.month > d2.month
return self.day > d2.day
def diff (self, d):
cnt = 0
d1 = self.copy()
d2 = d.copy()
while d1.isBefore(d2):
d1.tomorrow()
cnt -= 1
while d1.isAfter(d2):
d1.yesterday()
cnt += 1
return cnt
  
def dow (self):
weekday = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
return weekday[(self.diff(Date(4, 4, 2016)))%7]

Explaination:

In this program there is a class Date with following functions

1) _init_ method takes three arguments : month , date , year

2) _repr_ method that represent the date in proper format of "mm/dd/yyyy"

3) isLeapYear() function takes an argument and check wheater the year of date is leap year or not. An year is leap year if it is divisible by 4 or 100 or 400. So I have used % operator here

4) copy() function to copy the content of original objects

5) tomorrow() function takes an argument. Here first we isLeapYear function to check whether the date is of leap year or not. If it is of leap year than feb month will have 29 days else 28 days. This way i have made a list of number of days in 12 months. If the day of the date is last day of the month then day becomes 1 and month is incremented by 1. Similarly if date is of last month then month becomes 1 and year is incremented by 1.

6)yesterday() function is same as tommorow() function except here we subtract and not add.

7) isBefore() function and isAfter() function will check wheather the first date is greater or second date is greater.

8) diff() function first copies the dates by calling the copy() function. Then uses while loop to first call isBefore function. This loop will call tommorow function till isBefore() function returns true. and decrementing the count the number of times loop is executed. Then it again uses while loop to call isAfter function. This loop will call yeaterday function till isAfter() function returns true. and incrementing the count the number of times loop is executed. At last this function return the count.

9) dow() function will return the day of the week of the date. Here I have taken 4 April 2016 as the known date which is monday. First i have calculated the difference between the two dates.Mod 7. The remainder so obtained will be index of the weekday and return the day of the week.