Challenge 3: Implement a Calculator Class
In this exercise, you have to implement a calculator that can perform addition, subtraction, multiplication, and division.
We'll cover the following
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 ofnum1
andnum2
. -
subtract()
is a method that returns the subtraction ofnum1
fromnum2
. -
multiply()
is a method that returns the product ofnum1
andnum2
. -
divide()
is a method that returns the division ofnum2
bynum1
.
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 1200+ tech skills courses.