Answer to Question #189453 in Python for Ranee Gopee

Question #189453

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.



1
Expert's answer
2021-05-05T10:29:36-0400
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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS