Write a python program to add two distances using class and object concepts


This Python program defines a Distance class that represents a distance using kilometers, meters, and centimeters. The class provides methods to add two distances and to display a distance in a human-readable format. Here's an explanation of the program:

  • class Distance: This is the main class representing a distance. It has the following methods:
    • __init__(self, km=0, m=0, cm=0): The constructor initializes the object with optional values for kilometers (default is 0), meters (default is 0), and centimeters (default is 0).
    • add(self, other_distance): This method takes another Distance object as a parameter, adds the corresponding components (kilometers, meters, and centimeters), and returns a new Distance object.
    • display(self): This method returns a formatted string representation of the distance.
  • The program then prompts the user to enter two distances: one for distance1 and one for distance2. The user is asked to input the distance in kilometers, meters, and centimeters for each.
  • The Distance objects distance1 and distance2 are created with the user-provided values.
  • The add method is called on distance1, passing distance2 as the parameter. This results in a new Distance object result_distance that represents the sum of the two input distances.
  • The program displays the result by calling the display method on result_distance, which provides the sum of both distances in a human-readable format.

When you run this program, it will allow you to input two distances and then calculate and display the sum of those distances. The program takes care of adjusting units when necessary, ensuring that the result is displayed correctly in terms of kilometers, meters, and centimeters.


Source Code

class Distance:
    def __init__(self, km=0, m=0, cm=0):
        self.km = km
        self.m = m
        self.cm = cm
 
    def add(self, other_distance):
        total_km = self.km + other_distance.km
        total_m = self.m + other_distance.m
        total_cm = self.cm + other_distance.cm
 
        # Adjust units if necessary
        if total_cm >= 100:
            total_m += total_cm // 100
            total_cm %= 100
        if total_m >= 1000:
            total_km += total_m // 1000
            total_m %= 1000
 
        return Distance(total_km, total_m, total_cm)
 
    def display(self):
       return f"{self.km} KM {self.m} M {self.cm} CM"
 
print("Enter First Distance")
km1 = int(input("Enter Kilometers : "))
m1 = int(input("Enter Meters : "))
cm1 = int(input("Enter Centimeters : "))
distance1 = Distance(km1, m1, cm1)
 
print("\nEnter Second Distance")
km2 = int(input("Enter Kilometers : "))
m2 = int(input("Enter Meters : "))
cm2 = int(input("Enter Centimeters : "))
distance2 = Distance(km2, m2, cm2)
 
# Add the distances
result_distance = distance1.add(distance2)
 
# Display the result
print("Sum of both Distances is :",result_distance.display())

Output

Enter First Distance
Enter Kilometers : 53
Enter Meters : 150
Enter Centimeters : 240

Enter Second Distance
Enter Kilometers : 34
Enter Meters : 123
Enter Centimeters : 210
Sum of both Distances is : 87 KM 277 M 50 CM

Example Programs