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

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:

>>> d1 = Date(4, 10, 2016) >>> d2 = Date(5, 7, 2016) >>> d2.diff(d1) 27 >>> d1.diff(d2) -27 >>> d1 # Make sure the original objects did not change. 04/10/2016 >>> d2 05/07/2016 # Here are two that pass over a leap day. >>> d3 = Date(12, 1, 2015) >>> d4 = Date(3, 15, 2016) >>> d4.diff(d3) 105 

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:

day_of_week_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] 

Examples:

>>> d = Date(4, 4, 2016) >>> d.day_of_week() 'Monday' >>> Date(1, 1, 2100).day_of_week() 'Friday' >>> Date(7, 4, 1776).day_of_week() 'Thursday' 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!