Challenge 5: Implement a Calculator Class

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

Problem Statement

Write a Java class called Calculator with

  • private fields:

    • num1 and num2 (double type)

And methods:

  • add(), a method which returns the addition of two numbers

  • subtract(), a method which returns the subtraction of num1 from num2

  • multiply(), a method which returns the multiplication of numbers

  • divide(), a method which returns the division of num2 by num1

  • Define a parameterized constructor which takes two parameters num1 and num2 and assigns these variables to the respective fields in the class.

Input

Pass double point numbers in the parameterized constructor

Output

addition, subtraction, division, and multiplication

Sample Input

Calculator obj = new 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.