Menu

[Solved] 1 20 Pts Complete Class Time Stores Manipulates Times 24 Hour Clock Specified Write Requir Q37249709

1. (20 pts) Complete the class Time, which stores andmanipulates times on a 24-hour clock. As specified below, write therequired methods, including those needed for overloading operators.Exceptions messages should include the class and method names, andidentify the error (including the value of all relevant arguments).Hint see the type_as_str function in the goody.py module.

  1. The class is initialized with three int values (the hour first,the minute second, the second third; all with default values of 0).If any parameter is not an int, or not in the correct range (thehour must be between 0 and 23 inclusive, the minute and second mustbe between 0 and 59 inclusive) raise an AssertionError with anappropriate string describing the problem/values. When initialized,the Time class should create exactly three attributes/selfvariables named hour, minute, and second (with these exact namesand no others attributes/self variables).

  2. Write the __getitem__ method to allow Time class objects to beindexed by either (a) an int with value 1 or 2 or 3, or (b) anylength tuple containing any combinations of just these threevalues: e.g., (1,3). If the index is not one of these types orvalues, raise an IndexError exception with an appropriate stringdescribing the problem/values. If the argument is 1, returns thehour; if the argument is 2, return the minute, and if the argumentis 3, return the second. If the argument is a tuple, return a tuplewith hour or minute or second substituted for each value in thetuple. So if t = Time(5,15,20) then t[1] returns 5 and t[2,3]returns (15,20). Note that calling t[1] will pass 1 as itsargument; calling t[1,2] will pass the tuple (1,2) as its argument.In fact, t[1,1] will pass the tuple (1,1) and return the tuple(5,5).

  3. Write methods that return (a) the standard repr function of aTime, and (b) a str function of a Time: str for a Time shows thetime in the standard 12 hour clock format: str(Time(13,10,5))returns ‘1:10:05pm’; str(Time(5,6,3)) returns ‘5:06:03am’;str(Time(0,0,0)) returns ’12:00:00am’. It is critical to write thestr method correctly, because I used it in the batch self-checkfile for testing the correctness of other methods.

  4. Write a method that interprets midnight as False and any othertime as True.

  5. Write a method that interprets the length of a Time as thenumber of seconds that have elapsed from midnight to the that time.So len(Time(0,0,0)) returns 0 and len(Time(23,59,59)) returns86,399; there are 86,400 seconds in a day: midnight tomidnight.

  6. Overload the == operator to allow comparing two Time objects forequality (if a time object is compared against an object from anyother class, it should return False). Note that if you define ==correctly, Python will be able to compute != by using not and==.

  7. Overload the < operator to allow comparing two Time objects.The left Time is less-than the right one if it comes earlier in theday than the right one. Also allow the right operand to be an int:in this case, return whether the length (an int, see above) of theTime is less-than the right int. If the right operand is any othertype, raise a TypeError exception with an appropriate stringdescribing the problem/values. Note that if you define <correctly, Python will be able to compute Time > Time and int> Time by using <.

  8. Overload the + operator to allow adding a Time object and anint, producing a new Time object as a result (and not mutating theTime object + was called on). If the other operand is not an int,raise a TypeError

    exception with an appropriate string describing theproblem/values (you may assume without checking that this argumentis non-negative). Both Time + int and int + Time should be allowedand have the same meaning. Hint: write code that adds one second toa Time; then iterate over this code the int number of times: thereare faster ways to do this, but this way is correct.

    9. Write the __call__ method to allow an object from this classto be callable with three int arguments: update the hour of theobject to be the first argument, and the minute of the object to bethe secondargument,andthesecondoftheobjecttobethethirdargument.ReturnNone.Ifanyparameterisnotlegal (see how the class is initialized), raisean AssertionError with an appropriate string describing theproblem/values.

Expert Answer


Answer to 1. (20 pts) Complete the class Time, which stores and manipulates times on a 24-hour clock. As specified below, write th… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *