Challenge 3: Implement a Calculator Class

In this exercise, you have to implement a calculator that can perform addition, subtraction, multiplication, and division.

Problem statement

Write a Python class called Calculator by completing the tasks below:

Task 1

Initializer

Implement an initializer to initialize the values of num1 and num2.

Properties

  • num1
  • num2

Task 2

Methods

  • add() is a method that returns the sum of num1 and num2.

  • subtract() is a method that returns the subtraction of num1 from num2.

  • multiply() is a method that returns the product of num1 and num2.

  • divide() is a method that returns the division of num2 by num1.

Input

Pass numbers (integers or floats) in the initializer.

Output

addition, subtraction, division, and multiplication

Sample input

obj = Calculator(10, 94);
obj.add()
obj.subtract()
obj.multiply()
obj.divide()

Sample output

104
84
940
9.4

Get hands-on with 1400+ tech skills courses.