58.4k views
0 votes
In the context of creating two classes, one named "Coffee" and the other "Cream," where Cream has an instance variable "percentage," and Coffee has a "cost" instance variable, which of the following is a correct implementation?

a) class Coffee:
def init(self, percentage):
self.cost = percentage
class Cream:
def init(self, cost):
self.percentage = cost
b) class Coffee:
def init(self, cost):
self.cost = cost
class Cream:
def init(self, percentage):
self.percentage = percentage
c) class Coffee:
def init(self, percentage):
self.percentage = percentage
class Cream:
def init(self, cost):
self.cost = cost
d) class Coffee:
def init(self, cream_percentage):
self.cost = cream_percentage
class Cream:
def init(self, coffee_cost):
self.percentage = coffee_cost

1 Answer

4 votes

Final answer:

The correct implementation of the two classes, Coffee with a cost instance variable and Cream with a percentage instance variable, is option (b), matching each class's init method's parameter with the relevant instance variable.

Step-by-step explanation:

In the context of creating two classes, Coffee and Cream, where Cream has an instance variable percentage, and Coffee has a cost instance variable, the correct implementation is:

class Coffee:

def init(self, cost):
self.cost = cost

class Cream:

def init(self, percentage):
self.percentage = percentage

This corresponds to the option (b) provided in the context. Each class's init method correctly assigns the given parameter to the appropriate instance variable, which is "cost" for Coffee and "percentage" for Cream.

User Dbz
by
8.6k points