class Computer: def __init__ (self, name, ram, hd): self.name = name self.ram_amount = ram self.hd_size = hd def calculate(self): return "Crunching numbers!"To overwrite the inherited method calculate from Computer, which syntax should be used?Group of answer choicesclass Laptop(Computer): def calculate(self): return "Processing mathematics!"class Laptop(Computer): def calculate(self, num): return "Processing mathematics"I don't know.class Laptop: def calculate(self): return "Processing mathematics!"
Question
class Computer: def init (self, name, ram, hd): self.name = name self.ram_amount = ram self.hd_size = hd def calculate(self): return "Crunching numbers!"To overwrite the inherited method calculate from Computer, which syntax should be used?Group of answer choicesclass Laptop(Computer): def calculate(self): return "Processing mathematics!"class Laptop(Computer): def calculate(self, num): return "Processing mathematics"I don't know.class Laptop: def calculate(self): return "Processing mathematics!"
Solution
The correct syntax to overwrite the inherited method calculate from Computer is:
class Laptop(Computer):
def calculate(self):
return "Processing mathematics!"
This syntax creates a new class called Laptop that inherits from the Computer class. It then defines a new method called calculate that overrides the calculate method in the Computer class. When the calculate method is called on an instance of the Laptop class, it will return "Processing mathematics!" instead of "Crunching numbers!".
Similar Questions
Consider the following code:class DailyRoutine: def morning(self): print("coffee") print("shower")class MondayRoutine(DailyRoutine): def morning(self): print("jog") print("coffee") print("shower")We want the morning method in the MondayRoutine class to add to the parent class's functionality, rather than completely overwrite it. How do we do this in a way that doesn't duplicate so much code?Group of answer choicesI don't know.Use super() in the MondayRoutine class.Use super().morning() in MondayRoutinemorning() method.
Problem statementSend feedbackWhat will be the output of this code?class Student: def __init__(self,name): self._name = name class Student1(Student): def __init__(self,name): super().__init__(name)s = Student("saif")print(s._name)
Which of the following is correct?class A: def __init__(self): self.count=5 self.count=count+1a=A()print(a.count)560Error
Given the following code:class Rectangle: def __init__(self, width, height): self.width = width self.height = height def calculate_area(self): return self.width * self.heightWrite the definition of a class Square that:#1 inherits from the class Rectangle,#2 adds a method called calculate_perimeter that calculates and returns the square's perimeter#3 overrides the __str__ method to print out the square's area and perimeter
BackgroundStudy the following class definitions.class C1(): def f(self): return 2*self.g() def g(self): return 2class C2(C1): def f(self): return 3*self.g()class C3(C1): def g(self): return 5class C4(C3): def f(self): return 7*self.g()obj1 = C1()obj2 = C2()obj3 = C3()obj4 = C4()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.TaskFor each of obj1, obj2, obj3, and obj4, print the methods which are called following a call to objn.f(), where n is the number of the obj, in the format:objn: call1, call2, call3though there may be more or less than three calls.Take care to insert spaces after the colon and the commas.ExamplesYou should print:obj1: C1.f, C1.gand we have already done this for you in the starter code.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.