Calculator using OOPs

Tutorial on Class Object & Method
  1. Basic Exercise on Object Oriented Programming
  2. Create one class calculator
  3. While creating object of calculator class take two user inputs.
  4. Display addition , subtraction, multiplication and division of two input numbers.

Exercise Solution
class calculator():
  def __init__(self,x,y):
    self.x=x
    self.y=y

  def add(self):
    print("Sum :",self.x+self.y)

  def subtraction(self):
    print("Subtraction :",self.x-self.y)  

  def multiplication(self):
    print("Multiplication :",self.x*self.y)

  def division(self):
    print("Division :",self.x/self.y)  
#x=int(input("Enter first number : "))
#y=int(input("Enter second number : "))
#n1=calculator(x,y) # object 
n1=calculator(5,4) # object 

n1.add()
n1.subtraction()
n1.multiplication()
n1.division()
Output
Sum : 9
Subtraction : 1
Multiplication : 20
Division : 1.25

Answer these Questions

  1. What is the name of the method used in calculator class ?
  2. List all objects created in above code ?
  3. What is the data type of created object
Tutorial on Class Object & Method Exercise constructor ( basic )

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer