Answer:
The solution code is written in Python:
- class Carpet:
- def __init__(self, rect, cost):
- self.size = rect
- self.costPerSqFoot = cost
-
- def cost(self):
- carpetCost = self.size.area() * self.costPerSqFoot
- return carpetCost
Step-by-step explanation:
Presume that there is existence of Rectangle class as given in the question, Carpet class is written. The Carpet constructor is defined that take Rectangle object, rect, and cost as parameter (Line 2). To create data member of Carpet class, keyword "self" is used to precede with the name of the data members, size and costPerSqFoot (Line 3-4). The data members are initialized with the parameter rect and cost, respectively.
Next, cost method is defined (Line6 - 8). Within the cost method, the area method of Rectangle object is invoked by expression, self.size.area() and this will return the area value and multiplied with the costPerSqFoot to get the carpet cost and return it as output (Line 8).