Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In pyton : Overload the necessary operator ( s ) ( that is _ _ gt _ _ ) so that instead of having to
In pyton :
Overload the necessary operatorsthat is gt so that instead of
having to write
if taftert:
we can use the more convenient
if t t:
Rewrite all other relational operators in step
Create some test cases to test out the above changes Consider
specifically the case where the number of seconds to add to the time is
negative. Fix up increment, so that it handles this case if it does not do
so already. You may assume that you will never subtract more
seconds than are in the time object.
Given that physical time cannot be negative, or must time always
move in the forward direction. Adjust your code above in step to so
that it will never go into negative time.
I have created the code and implemented the question from till however I am not able to do and The professor stated that "negative time protection" was missing from my code. Can anyone please check my code and help me understand how to add negative time protection in the code. Thank you
My code:
class MyTime:
def initself hrs mins secs:
Create a MyTime object initialized to hrs mins, secs
totalsecs hrs mins secs
self.hours totalsecs # Split in h m s
leftoversecs totalsecs
self.minutes leftoversecs
self.seconds leftoversecs
def strself:
return ::formatselfhours, self.minutes, self.seconds
def addself other:
return MyTime self.toseconds other.toseconds
def subself other:
return MyTime self.toseconds other.toseconds
def ltself other:
return self.toseconds other.toseconds
def leself other:
return self.toseconds other.toseconds
def gtself other:
return self.toseconds other.toseconds
def geself other:
return self.toseconds other.toseconds
def eqself other:
return self.toseconds other.toseconds
def neself other:
return self.toseconds other.toseconds
def incrementself secs:
self.seconds secs
while self.seconds :
self.seconds
self.minutes
while self.minutes :
self.minutes
self.hours
while self.seconds :
self.seconds
self.minutes
while self.minutes :
self.minutes
self.hours
while self.hours :
self.hours
def tosecondsself:
Return the number of seconds represented by this instance"""
return self.hours self.minutes self.seconds
def afterself time:
Return True if I am strictly greater than time
return self.toseconds timetoseconds
def addtimet t:
secs ttoseconds ttoseconds
return MyTime secs
def addtimet t:
h thours thours
m tminutes tminutes
s tseconds tseconds
sumt MyTime secs
return sumt
my main code:
from MyTime import MyTime
X MyTime
Y MyTime
Z X Y
printZ
print
# Test Relational Operators
printX Y: X Y
printX Y: X Y
printX Z: X Z
printY Z: Y Z
printX Y: X Y
printX Y: X Y
print
# Test increment
def incrementt secs:
tseconds secs
tincrement
Zincrement
printZ
Zincrement
printZ
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started