- Add Calculator class with add and multiply methods - Maintain original functions for compatibility - Update test code to use class interface
34 lines
1000 B
Python
34 lines
1000 B
Python
class Calculator:
|
|
"""A calculator class that provides basic arithmetic operations."""
|
|
|
|
def add(self, a, b):
|
|
"""Return the sum of two numbers."""
|
|
return a + b
|
|
|
|
def multiply(self, a, b):
|
|
"""Return the product of two numbers."""
|
|
return a * b
|
|
|
|
def add_two_numbers(a, b):
|
|
"""Return the sum of two numbers."""
|
|
return a + b
|
|
|
|
def multiply_two_numbers(a, b):
|
|
"""Return the product of two numbers."""
|
|
return a * b
|
|
|
|
if __name__ == "__main__":
|
|
# Example usage with the Calculator class
|
|
calc = Calculator()
|
|
|
|
# Test addition using Calculator class
|
|
sum_result = calc.add(5, 3)
|
|
print(f"The sum is: {sum_result}")
|
|
|
|
# Test multiplication using Calculator class
|
|
product_result = calc.multiply(5, 3)
|
|
print(f"The product is: {product_result}")
|
|
|
|
# Also test the original functions for compatibility
|
|
print(f"Function add result: {add_two_numbers(5, 3)}")
|
|
print(f"Function multiply result: {multiply_two_numbers(5, 3)}") |