solutions

Calculator with static methods

Build a calculator using only static and class methods.

This is the way it should work:

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

Note that I've never created an instance of the calculator. I'm using the regular class.

One final addition. The subtract method must be implemented using the add method. Something like this:

subtract = add(x, y * -1)

Test Cases

test subtract - Run Test

def test_subtract():
    assert Calculator.subtract(5, 2) == 3

test divide - Run Test

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

test multiply - Run Test

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

test add - Run Test


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

test divide decimal - Run Test

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