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.

print('obj1: C1.f, C1.g') # C1.f calls C1.g; no superclass methods are called

Solutions

Expert Solution

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()

#====================================================================

print('obj1: C1.f, C1.g') # C1.f calls C1.g; no superclass methods are called

print('obj2: C2.f, C1.g') # C2.f calls C1.g; superclass method g() of C1 is called

print('obj3: C3.g') # C3.g doesn't call anything, simply returns 5; No superclass methods are called

print('obj4: C4.f, C3.g') # C4.f calls C3.g; superclass method g() of C3 is called

====================================================================


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...
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 =...
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")...
Consider the following 2-period model U(C1,C2) = min{3C1,4C2} C1 + S = Y1 – T1 C2...
Consider the following 2-period model U(C1,C2) = min{3C1,4C2} C1 + S = Y1 – T1 C2 = Y2 – T2 + (1+r)S Where C1 : first period consumption C2 : second period consumption S : first period saving Y1 = 20 : first period income T1 = 5 : first period lump-sum tax Y2 = 50 : second period income T2 = 10 : second period lump-sum tax r = 0.05 : real interest rate Find the optimal saving, S*
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
Consider the DataSource class in below code. Explain how inheritance is intended to be used with...
Consider the DataSource class in below code. Explain how inheritance is intended to be used with this class. Does this represent a good use of inheritance? Explain your answer // Base class for game configuration public abstract class DataSource {     private Graph map;     private HashMap <String,Room> rooms;     private ArrayList <Entity> entities;     private Player player;     protected HashMap < String, List<String[]> > tables;     // constructor     public DataSource() {     }     // Connect to the...
"""stack.py implements stack with a list""" class Stack(object): def __init__(self): #creates an empty stack. O(1) self.top...
"""stack.py implements stack with a list""" class Stack(object): def __init__(self): #creates an empty stack. O(1) self.top = -1 #the index of the top element of the stack. -1: empty stack self.data = [] def push(self, item): # add item to the top of the stack. O(1) self.top += 1 self.data.append(item) def pop(self): # removes and returns the item at the top O(1) self.top -=1 return self.data.pop() def peek(self): # returns the item at the top O(1) return self.data[len(self.data)-1] def isEmpty(self):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT