Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in python : simple and clear: please see all the requirements and clocktest.py file in the last. 7:18 M @VI Ft Jll 95% i 020

in python : simple and clear:

please see all the requirements and clocktest.py file in the last.

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

7:18 M @VI Ft Jll 95% i 020 Intersession Objects aments Points 25 Due Wednesday by 11:59pm Submitting a file upload us Read everything before doing anything! Create a class named Clock to represent a time in hours and minutes. (I am using the name Clock because there is an existing module named time and I want to avoid conflicts.) Store this class in a file named clock.py. As hinted in the previous paragraph, objects of this class have two attributes that must be named hour and minute. The hour ranges from 0 to 23, and the minute from 0 to 59. Your class will implement the following methods: _init__(self, hour, minute) The constructor creates a new Clock with the given hour and minute. Your constructor must ensure that, no matter what numbers are given, the hour is from 0 and 23 and the minute from O and 59. Thus, if someone tries this: import clock t1 = clock.Clock(19, 12) t2 = clock.Clock(-5, 74) The first object represents the time 7:12 p.m. The second object has bad input, so do whatever you feel is reasonable to create a valid time. My code creates a clock representing 5:14 a.m. from the bad input. (Hint: I used abs and % to force the numbers into the range | wanted.) _str_(self) TILL 7:18 00 MA ON Jll 95% _str_(self) This method returns a string representing the time in 24-hour European notation; so this code: import clock t = clock.Clock( 17, 8) t2 = clock.Clock(3, 45) print(t) print(t2) Produces this output: 1708 0345 add(self, other) This method returns a new Clock object that is the result of adding the two times. For example: import clock t1 = clock.Clock(2, 37) # 2:37 a.m. t2 = clock. Clock(5, 29) # 5:29 a.m. t3 = t1.add(t2) print(t3) # prints 0806. subtract(self, other) This method returns a new Clock object that is the result of subtracting the two times. For example: import clock t1 = clock.Clock(18, 20) t2 = clock.Clock(3, 45) t3 = t1.subtract(t2) print(t3) # prints 1435 Your method should always subtract the smaller time from the larger time, so if I had written t3 = t2. subtract(t1) I would have gotten the same result. Hint: you can use abs() to make your life easier. The next two functions must be declared before the class declaration. O 7:18 00 MP @V 95% i the class declaration. from_military(s) This function takes a string that has a military time in it as its argument and returns a new Clock object with the corresponding time. You might use it as follows: import clock t = clock.from_military('1546') print(t) # output will be 1546 from_am_pm(s) This function takes a string that has a time in AM/PM format it as its argument and returns a new Clock object with the corresponding time. You might use it as follows: import clock t = clock.from_am_pm('3:46 p.m.') print(t) # output will be 1546 Your function must accept the AM or PM in upper or lower case, with or without periods, but you may presume there will be at least one space after the digits. Testing Your Code Download the clocktest.py. E program and put it in the same directory with your clock.py module. When you run the test program, it will do a fixed set of computations. It will then repeatedly prompt you to enter two times (one in military format, another in AM/PM format), add them, and subtract them. When You Finish Upload your clock.py file to Canvas. You don't have to rename it with your last and first name. Hints I wrote a method (with iust self as its - zoom + # Use this program to test your class. import clock t1 = clock.clock(18, 20) t2 = clock.clock(3, 47) added = t1.add(t2) subtract1 = t1. subtract(t2) subtract2 = t2. subtract(t1) print('Added is ', added) # should print: Added is 2207 print('Subtract smaller is', subtract1) # should print: Subtract smaller is 1433 print('Subtract larger is', subtract2) # should print: Subtract larger is 1433 30 - mil_string = input('Enter a military time in format hhmm or ENTER to quit: ') while mil_string != '': am_pm_string = input('Enter a time in hh:mm AM/PM format: '). t1 = clock.from_military (mil_string) t2 = clock.from_am_pm(am_pm_string) t3 = t1.add(t2) t4 = t1.subtract(t2) print('Times are', ti, and', t2) print('Add result:', t3) print('Subtract result:, t4) print() mil_string = input('Enter a military time in format hhmm or ENTER to quit: ')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions