Solution 1

class Calculator(object):
    def __init__(self):
        pass

    def add(self, num1, num2):
        return num1 + num2

    def subtract(self, num1, num2):
        return num1 - num2

    def multiply(self, num1, num2):
        return num1 * num2

    def divide(self, num1, num2):
        return float(num1) / num2

OOP Calculator

The objective of the following assignment is to build a simple calculator using Object Oriented Programming.

This is the interface it should follow:

calculator = Calculator()

calculator.add(2, 4)  # 6
calculator.subtract(8, 1)  # 7
calculator.multiply(3, 5)  # 15
calculator.divide(5, 2)  # 2.5

Test Cases

test divide - Run Test

def test_divide():
    calc = Calculator()
    assert calc.divide(9, 3) == 3.0

test multiply - Run Test

def test_multiply():
    calc = Calculator()
    assert calc.multiply(7, 9) == 63

test add - Run Test

def test_add():
    calc = Calculator()
    assert calc.add(5, 2) == 7

test divide decimal - Run Test

def test_divide_decimal():
    calc = Calculator()
    assert calc.divide(9, 2) == 4.5

test subtract - Run Test

def test_subtract():
    calc = Calculator()
    assert calc.subtract(5, 2) == 3
class Calculator(object): pass