Question

In: Computer Science

Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def...

Inheritance - Method Calls

Consider the following class definitions.

class C1():
    def f(self):
        return 2*self.g()

    def g(self):
        return 2

class C2(C1):
    def f(self):
        return 3*self.g()


class C3(C1):
    def g(self):
        return 5

class C4(C3):
    def f(self):
        return 7*self.g()

obj1 = C1()
obj2 = C2()
obj3 = C3()
obj4 = C4()

For this problem you are to consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy, working out what happens will not always be straightforward.

For each of obj1, obj2, obj3, and obj4, print out the methods which will be called following a call to objn.f(). You should print a single line in the format: objn: call1, call2, call3.

So for example, when obj1.f() is called, you should print:

    obj1: C1.f, C1.g

We’ve already done this for you to give you an example.

Note: You don’t need to actually call the methods, you just need to write a print statement, as for obj1.f() above. All you need are the four print statements.

----------   USING PYTHON ----------

Solutions

Expert Solution

PYTHON CODE:

class C1():
    def f(self):
        return 2*self.g()

    def g(self):
        return 2

class C2(C1):
    def f(self):
        return 3*self.g()


class C3(C1):
    def g(self):
        return 5

class C4(C3):
    def f(self):
        return 7*self.g()

# creating object for class C1,C2,C3,C4
obj1 = C1()
obj2 = C2()
obj3 = C3()
obj4 = C4()

# print statements displaying the method call hierarchy for obj1,obj2,obj3,obj4
print("obj1: C1.f, C1.g")
print("obj2: C2.f, C1.g")
print("obj3: C1.f, C3.g")
print("obj4: C4.f, C3.g")

SCREENSHOT FOR CODING:

explanation:

For obj1:

   if obj1.f() is executed, both method f() and g() will be invoked from C1.
   so C1.f,C1.g

For obj2:

   Class C2 inherits C1. So there is no g() method in C2.
   so,obj2.f() is executed, f() from Class C2 and g() method
   from class C1 is invoked.
   so C2.f,C1.g

For obj3:
   class C3 inherits C1. So there is no f() method in C3.
   so obj3.f() is executed, f() from class C1 will be invoked
   and g() from c3 will be invoked.
   so C1.f,C3.g

For obj4:
   class C4 inherits C3. so There is no g() method in C4.
   so obj4.f() is executed, f() from class C4 will be invoked
   and g() from c3 will be invoked.


Related Solutions

Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def g(self): return 2 class C2(C1): def f(self): return 3*self.g() class C3(C1): def g(self): return 5 class C4(C3): def f(self): return 7*self.g() obj1 = C1() obj2 = C2() obj3 = C3() obj4 = C4() For this problem you are to consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy, working out what happens will...
python class Node(): def __init__(self, value): pass class PostScript(): def __init__(self): pass    def push(self, value):...
python class Node(): def __init__(self, value): pass class PostScript(): def __init__(self): pass    def push(self, value): pass    def pop(self): return None    def peek(self): pass    def is_empty(self): pass def stack(self): pass    def exch(self): pass    def index(self): pass    def clear(self): pass    def dup(self): pass    def equal(self): pass    def depth(self): pass    stack = PostScript() def decode(command_list): for object in command_list: if object == '=': stack.equal() elif object == 'count': stack.depth() elif object ==...
For python... class car:    def __init__(self)          self.tire          self.gas    def truck(self,color)        &nb
For python... class car:    def __init__(self)          self.tire          self.gas    def truck(self,color)               style = color                return    def plane(self,oil)              liquid = self.oil + self.truck(color) For the plane method, I am getting an error that the class does not define __add__ inside init so I cannot use the + operator. How do I go about this.             
1) What is the argument in “class AddressBook(object):” 2) What is the roll of “def __init__(self):”?...
1) What is the argument in “class AddressBook(object):” 2) What is the roll of “def __init__(self):”? 3) What is the roll of “def __repr__ (self):”? 4) Please copy and run “Addressbook” Python program. Submit the code and the output 5) Discuss the two outputs’ differences (data type). 6) Please add 2 more people and report the output. Code: class AddressBook(object): def init_(self): self.people=[] def add_entry(self, new_entry): self.people.append(new_entry) class AddressEntry(object): def __init__(self, first_name=None, family_name= None, email_address= None, DOB= None): self.first_name =...
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return...
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return x ** 3 ##def function_3( x ) : return x ** 4 ## ###### create a list of callbacks to each of the functions ######by referencing their names ## ##callbacks = [ function_1 , function_2 , function_3 ] ## ######display a heading and the result of passing a value to each of the ######named functions: ## ##print( '\nNamed Functions:' ) ##for function in callbacks...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def __str__(self): s = "" for j in range(7, -1, -1): #for each row for i in range(8): # for each column if self.pieces[j][i] is None: # if self.pieces[j][i] is None s += "." # populate the row with '.' val for each column else: s += self.pieces [j][i].get_symbol() s += "\n" #after each row add a new line return s # return after iterating...
class employee: name = str('')    hourlyWage=0 hoursWorked=0 def getPayment(self): payment=self.hourlyWage*self.hoursWorked return payment    class payrollApp(employee):...
class employee: name = str('')    hourlyWage=0 hoursWorked=0 def getPayment(self): payment=self.hourlyWage*self.hoursWorked return payment    class payrollApp(employee):    def printStatement(self): print('The Employee Name is ' + self.name) print('The Employee Hourly wage is ' + str(self.hourlyWage)) print('No of hours worked ' + str(self.hoursWorked)) print('The Employee payment is ' + str(employee.getPayment(self))) emp = [] x=0 a=True totalPayout=0 while a:    emp.append(payrollApp()) emp[x].name=input("Enter Employee Name: ") emp[x].hourlyWage=int(input("Enter hourly wage: ")) emp[x].hoursWorked=int(input("Enter No of hours worked: ")) totalPayout= totalPayout + (emp[x].hourlyWage * emp[x].hoursWorked) print("\n") x=x+1...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):             ...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):              self.name = ""              self.ss = self.age = int(0)              self.smoker = self.HBP = self.HFD = self.points = int(0)              self.link = None              #if list not empty              if p != None:                     p.link = self        ptrFront = ptrEnd = None choice = int(0) def menu():        print( "\n\tLL Health Clinic\n\n")        print( "1. New patient\n")        print( "2. View patient by...
write pseudocode for this program . thank you import random class cal():    def __init__(self, a,...
write pseudocode for this program . thank you import random class cal():    def __init__(self, a, b):        self.a = a        self.b = b    def add(self):        return self.a + self.b    def mul(self):        return self.a * self.b    def div(self):        return self.a / self.b    def sub(self):        return self.a - self.b def playQuiz():    print("0. Exit")    print("1. Add")    print("2. Subtraction")    print("3. Multiplication")    print("4. Division")...
using repl or python class House():    valuationRate = 10       def __init__(self,city,sqft,price):        ...
using repl or python class House():    valuationRate = 10       def __init__(self,city,sqft,price):         self.city = city         self.sqft = sqft         self.price = price           def getPrice(self):         return self.price    def applyValuation(self):         self.price += self.price * self.valuationRate/100 # create class Townhouse that inherits from class House # class Townhouse should have valuationRate = 5    # implement method setPrice(self,price) in class Townhouse    # create an object House: city=Atlanta, sqft=10000, price=200000   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT