Question: In pyton : Overload the necessary operator ( s ) ( that is _ _ gt _ _ ) so that instead of having to

In pyton :
Overload the necessary operator(s)(that is __gt__) so that instead of
having to write
2. if t1.after(t2): ...
we can use the more convenient
if t1> t2: ...
3. Rewrite all other relational operators in step 3,4(==,<,<=,>,>=,!=).
4. 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.)
5. Given that physical time cannot be negative, or must time always
move in the forward direction. Adjust your code above in step 1 to 6 so
that it will never go into negative time.
I have created the code and implemented the question from 2 till 3 however I am not able to do 4 and 5. 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 __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
totalsecs = hrs*3600+ mins*60+ secs
self.hours = totalsecs //3600 # Split in h, m, s
leftoversecs = totalsecs %3600
self.minutes = leftoversecs //60
self.seconds = leftoversecs %60
def __str__(self):
return "{0}:{1}:{2}".format(self.hours, self.minutes, self.seconds)
def __add__(self, other):
return MyTime(0,0, self.to_seconds()+ other.to_seconds())
def __sub__(self, other):
return MyTime(0,0, self.to_seconds()- other.to_seconds())
def __lt__(self, other):
return self.to_seconds()< other.to_seconds()
def __le__(self, other):
return self.to_seconds()<= other.to_seconds()
def __gt__(self, other):
return self.to_seconds()> other.to_seconds()
def __ge__(self, other):
return self.to_seconds()>= other.to_seconds()
def __eq__(self, other):
return self.to_seconds()== other.to_seconds()
def __ne__(self, other):
return self.to_seconds()!= other.to_seconds()
def increment(self, secs):
self.seconds += secs
while self.seconds >=60:
self.seconds -=60
self.minutes +=1
while self.minutes >=60:
self.minutes -=60
self.hours +=1
while self.seconds <0:
self.seconds +=60
self.minutes -=1
while self.minutes <0:
self.minutes +=60
self.hours -=1
while self.hours <0:
self.hours +=24
def to_seconds(self):
""" Return the number of seconds represented by this instance"""
return self.hours *3600+ self.minutes *60+ self.seconds
def after(self, time2):
""" Return True if I am strictly greater than time2"""
return self.to_seconds()> time2.to_seconds()
def add_time2(t1, t2):
secs = t1.to_seconds()+ t2.to_seconds()
return MyTime(0,0, secs)
def add_time(t1, t2):
h = t1.hours + t2.hours
m = t1.minutes + t2.minutes
s = t1.seconds + t2.seconds
sum_t = MyTime(0,0, secs)
return sum_t
my main code:
from MyTime import MyTime
X = MyTime(1,2,3)
Y = MyTime(2,3,4)
Z = X - Y
print(Z)
print()
# Test Relational Operators
print("X < Y:", X < Y)
print("X > Y:", X > Y)
print("X <= Z:", X <= Z)
print("Y >= Z:", Y >= Z)
print("X == Y:", X == Y)
print("X != Y:", X != Y)
print()
# Test increment
def increment(t, secs):
t.seconds += secs
t.increment()
Z.increment(800)
print(Z)
Z.increment(-800)
print(Z)

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!