class Duration: def __init__(self, hours, minutes): self.hours = hours self.minutes = minutes def __add__(self, other): total_hours = self.hours + other.hours total_minutes = self.minutes + other.minutes if total_minutes >= 60: total_hours += 1 total_minutes -= 60 return Duration(total_hours, total_minutes) first_trip = Duration(1, 45) second_trip = Duration(0, 42) first_time = first_trip + second_trip second_time = second_trip + second_trip print(first_time.hours, first_time.minutes) print(second_time.hours, second_time.minutes) WHAT IS THE OUTPUT
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
class Duration:
def __init__(self, hours, minutes):
self.hours = hours
self.minutes = minutes
def __add__(self, other):
total_hours = self.hours + other.hours
total_minutes = self.minutes + other.minutes
if total_minutes >= 60:
total_hours += 1
total_minutes -= 60
return Duration(total_hours, total_minutes)
first_trip = Duration(1, 45)
second_trip = Duration(0, 42)
first_time = first_trip + second_trip
second_time = second_trip + second_trip
print(first_time.hours, first_time.minutes)
print(second_time.hours, second_time.minutes)
WHAT IS THE OUTPUT
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images