Given a parent class "Standard Calculator" consisting of one child class "Scientific Calculator".
Create the required parent class and child class using(Object-oriented programming concept) means to use the python class object to create this relationship.
The standard calculator(parent) can perform two types of operations: an addition and a subtraction
The standard calculator(child)is capable of inputting only two integers. But the scientific calculators(child) can also interpret decimal values.
The scientific calculator(child) is capable of four operations: addition, subtraction, cosine and sine of values.
class Standard_Calculator():
def __init__(self):
self.a = 0
self.b = 0
def sum(self, a, b):
self.a = int(a)
self.b = int(b)
return self.a + self.b
def addict(self, a, b):
self.a = int(a)
self.b = int(b)
return self.a - self.b
def code(self, s):
if s.split()[1] == '+':
return self.sum(s[0], s[2])
elif s.split()[1] == '-':
return self.addict(s[0], s[2])
class Scientific_Calculator(Standard_Calculator):
def sum(self, a, b):
self.a = float(a)
self.b = float(b)
return self.a + self.b
def addict(self, a, b):
self.a = float(a)
self.b = float(b)
return self.a - self.b
def code(self, s):
import math
if s.split()[0] == 'sin':
return math.sin(float(s[1]))
elif s.split()[0] == 'cos':
return math.cos(float(s[1]))
elif s.split()[1] == '+':
return self.sum(s[0], s[2])
elif s.split()[1] == '-':
return self.addict(s[0], s[2])
return None
Comments
Leave a comment