Implement a class Operation with a method execute(). Extend this class into AddOperation, SubtractOperation, and MultiplyOperation, each implementing the execute() method appropriately.
Example 1:
Input: AddOperation with numbers 5 and 3 Output: 8
Example 2:
Input: SubtractOperation with numbers 5 and 3 Output: 2
Override the execute() method in each subclass to perform the respective operation.
class Operation:
def execute(self, a, b):
pass
class AddOperation(Operation):
def execute(self, a, b):
return a + b
class SubtractOperation(Operation):
def execute(self, a, b):
return a - b
class MultiplyOperation(Operation):
def execute(self, a, b):
return a * b
# Test the classes
add_operation = AddOperation()
print(add_operation.execute(5, 3)) # Output: 8
subtract_operation = SubtractOperation()
print(subtract_operation.execute(5, 3)) # Output: 2
multiply_operation = MultiplyOperation()
print(multiply_operation.execute(5, 3)) # Output: 15
Unlock AI & Data Science treasures. Log in!