Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

Complete the class Time, which stores and manipulates times on a 24 hour clock. As specified below, write the required methods, including those needed for overloading operators. Exceptions messages should include the class and method names, and identify 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 (the hour must be between 0 and 23 inclusive, the minute and second must be between 0 and 59 inclusive) raise an AssertionError with an appropriate string describing the problem/values. When initialized, the Time class should create exactly three self variables named hour, minute, and second (with these exact names and no others self variables).
  2. Write the __getitem__ method to allow Time class objects to be indexed by either (a) an int with value 1 or 2 or 3, or (b) any length tuple containing any combinations of just these three values: e.g., (1,3). If the index is not one of these types or values, raise an IndexError exception with an appropriate string describing the problem/values. If the argument is 1, returns the hour; if the argument is 2, return the minute, and if the argument is 3, return the second. If the argument is a tuple, return a tuple with hour or minute or second substituted for each value in the tuple. 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 its argument; calling t[1,2] will pass the tuple (1,2) as its argument.
  3. Write methods that return (a) the standard repr function of a Time, and (b) a str function of a Time: str for a Time shows the time 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 the str method correctly, because I used it in the batch self-check file for testing the correctness of other methods.
  4. Write a method that interprets midnight as False and any other time as True.
  5. Write a method that interprets the length of a Time as the number 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)) returns 86,399; there are 86,400 seconds in a day: midnight to midnight.
  6. Overload the == operator to allow comparing two Time objects for equality (if a time object is compared against an object from any other class, it should return False). Note that if you define == correctly, Python willbeabletocompute!=byusing ==.
  7. Overload the < operator to allow comparing two Time objects. The left Time is less-than the right one if it comes earlier in the day 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 the Time is less-than the right int. If the right operand is any other type, raise a TypeError exception with an appropriate string describing 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 an int, producing a new Time object as a result (and not mutating the Time object + was called on). If the other operand is not an int, raise a TypeError

exception with an appropriate string describing the problem/values. Both Time + int and int + Time should be allowed and have the same meaning. Hint: write code that adds one second to a Time; then iterate over this code the int number of time.

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

Python, Programming

  • Category:- Python
  • Reference No.:- M91697261
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Python

Question write a simple python program that takes use

Question: Write a simple python program that takes use inputs as non-zero digits and converts them into binary form. The response must be typed, single spaced, must be in times new roman font (size 12) and must follow th ...

Question research pythons dictionary data type dictdiscuss

Question : Research Python's dictionary data type (dict). Discuss its interface and usage. Include examples. Discuss practical applications of dictionaries.

Homework -this homework will have both a short written and

Homework - This homework will have, both a short written and coding assignment. The problems that are supposed to be written are clearly marked. 1) (Written) Make heuristics Describe two heuristics for the slide problem ...

Lab assignment -background - we have discussed in detail

Lab Assignment - Background - We have discussed, in detail, the function of Stacks and Queues and how they are specifically implemented in Python. To get a better understanding of the utility of these data structures, we ...

The second task in this assignment is to create a python

The second task in this assignment is to create a Python program called pancakes.py that will determine the final order of a stack of pancakes after a series of flips.(PYTHON 3) Problem Task In this problem, your input w ...

Question a software company sells a package that retails

Question : A software company sells a package that retails for $99. Quantity discounts are given according to the following table: Quantity Discount 10 - 19 20% 20 - 49 30% 50 - 99 40% 100 or more 50% Write a program usi ...

In this programming assignment you will write a client

In this programming assignment, you will write a client pingprogram in Python. Your client will send a simple ping message to a server, receive a correspondingpong message back from the server, and determine the delay be ...

Assignment1 utilising python 3 build the following

Assignment 1. Utilising Python 3 Build the following regression models: - Decision Tree - Gradient Boosted Tree - Linear regression 2. Select a dataset (other than the example dataset given in section 3) and apply the De ...

Below zero - ice cream storethe local ice-cream store needs

Below Zero - ice cream store The local ice-cream store needs a new ordering system to improve customer service by streamlining the ordering process. The manager of the store has found that many orders are incorrect and s ...

Project reconnaissance and attack on ics

Project: Reconnaissance and Attack on ICS NetworksEnvironment Setup The second mini project will be based on Industrial Network Protocols, specifically the Modbus protocol. Please follow the instructions carefully to set ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As