Program to Create a Class which Performs Basic Calculator Operations

Python Program to Create a Class which Performs Basic Calculator Operations

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Object-Oriented Programming(OOPS):

The task of Object-Oriented Programming (OOP) is to create “objects.” An object is a collection of interconnected variables and functions. These variables are frequently referred to as object attributes, and functions are referred to as object behavior. These objects improve and clarify the program’s structure.

An automobile, for example, can be an item. If we regard the car to be an item, its properties include its color, model, price, brand, and so on. And its behavior/function would be acceleration, deceleration, and gear shift.

Another example: If we consider a dog to be an object, its properties would be its color, breed, name, weight, and so on. And his behavior/function would include things like walking, barking, playing, and so on.

Object-Oriented programming is well-known for implementing real-world elements such as objects, hiding, inheritance, and so on in programming. It facilitates visualization because it is similar to real-world scenarios.

Python has always been an object-oriented language, as have other general-purpose computer languages. It allows us to develop a program using an Object-Oriented methodology. Python makes it easy to create and use classes and objects.

The task is to write a python program using classes that perform simple calculator operations.

Program to Create a Class which Performs Basic Calculator Operations in Python

Below are the steps to implement the simple calculator in python using classes.

It works best when a user enters equations for the machine to solve. We’ll start by creating a code in which the user enters the numbers that they want the computer to use.

We’ll do this by utilizing Python’s built-in input() function, which accepts user-generated keyboard input. Within the parenthesis of the input() function, we can supply a string to prompt the user. A variable will be assigned to the user’s input.

Make the code prompt for two numbers because we want the user to enter two numbers for this application. When requesting input, we should include a space at the end of our string to separate the user’s input from the prompting string.

Approach:

  • Give the two numbers as static input and store them in two variables.
  • Create a class and use a parameterized constructor to initialize its values.
  • Create methods that calculate the results of adding, subtracting, multiplying, and dividing the given two numbers.
  • Create an object to represent the class.
  • Pass the given two numbers to the class as an argument.
  • Using the object, call the appropriate function based on the user’s selection.
  • The final result will be printed using the print() function.
  • The Exit of the program.

Below is the implementation:

# class
class calculator():
  # use a parameterized constructor to initialize its values.
    def __init__(self, number1, number2):
        self.number1 = number1
        self.number2 = number2
 # function which calculates the sum of the given two numbers

    def addNumbers(self):
        return self.number1+self.number2
 # function which calculates the product of the given two numbers

    def mulNumbers(self):
        return self.number1*self.number2
 # function which divides the given two numbers

    def divNumbers(self):
        return self.number1/self.number2
 # function which calculates the difference of the given two numbers

    def subNumbers(self):
        return self.number1-self.number2


# Give the two numbers as static input and store them in two variables.
number1 = 28
number2 = 11
# Create an object to represent the class.
calobj = calculator(number1, number2)
option = 1
# using while loop
while option != 0:
    print("0. For Exit")
    print("1. For Addition of given two numbers", number1, ',', number2)
    print("2. For Subtraction of given two numbers", number1, ',', number2)
    print("3. For Multiplication of given two numbers", number1, ',', number2)
    print("4. For Division of given two numbers", number1, ',', number2)
    option = int(input("Enter the option/choice you want = "))
    if option == 1:
        print("The additon of given numbers", number1,
              ',', number2, '=', calobj.addNumbers())
    elif option == 2:
        print("The difference of given numbers", number1,
              ',', number2, '=', calobj.subNumbers())
    elif option == 3:
        print("The product of given numbers", number1,
              ',', number2, '=', calobj.mulNumbers())
    elif option == 4:
        print("The division of two numbers", number1,
              ',', number2, '=', calobj.divNumbers())
    elif option == 0:
        print("Exit of program")
    else:
        print("Enter the choice again")
    print()

Output:

0. For Exit
1. For Addition of given two numbers 28 , 11
2. For Subtraction of given two numbers 28 , 11
3. For Multiplication of given two numbers 28 , 11
4. For Division of given two numbers 28 , 11
Enter the option/choice you want = 3
The product of given numbers 28 , 11 = 308

0. For Exit
1. For Addition of given two numbers 28 , 11
2. For Subtraction of given two numbers 28 , 11
3. For Multiplication of given two numbers 28 , 11
4. For Division of given two numbers 28 , 11
Enter the option/choice you want = 2
The difference of given numbers 28 , 11 = 17

0. For Exit
1. For Addition of given two numbers 28 , 11
2. For Subtraction of given two numbers 28 , 11
3. For Multiplication of given two numbers 28 , 11
4. For Division of given two numbers 28 , 11
Enter the option/choice you want = 1
The additon of given numbers 28 , 11 = 39

0. For Exit
1. For Addition of given two numbers 28 , 11
2. For Subtraction of given two numbers 28 , 11
3. For Multiplication of given two numbers 28 , 11
4. For Division of given two numbers 28 , 11
Enter the option/choice you want = 4
The division of two numbers 28 , 11 = 2.5454545454545454

0. For Exit
1. For Addition of given two numbers 28 , 11
2. For Subtraction of given two numbers 28 , 11
3. For Multiplication of given two numbers 28 , 11
4. For Division of given two numbers 28 , 11
Enter the option/choice you want = 0
Exit of program

Explanation:

  • The calculator class is created, and the __init__() function is used to initialize the class’s values.
  • There are defined methods for adding, subtracting, multiplying, and dividing two integers and returning their respective outcomes.
  • The menu is printed, and the user makes his or her selection.
  • The two integers from the user-supplied as inputs are used to generate an instance for the class.
  • The object is used to call the appropriate method based on the user’s selection.
  • The loop is terminated when the choice is 0(End of the infinite loop)
  • The final result is printed.