Ask Question
3 February, 07:52

For this exercise, you'll use the Rectangle class below (you can assume that the length and width are measured in feet). Write a class named Carpet that has two data members: size and costPerSqFoot. It should have a constructor that takes a Rectangle object and a float as parameters and uses them to initialize its data members. It should also have a method named cost that asks the size data member for its area and uses that to calculate and return the cost of the Carpet. This is an example of class composition because the Carpet class contains a Rectangle object as one of its data members.

class Rectangle:

"""

Represents a geometric rectangle

"""

def __init__ (self, length, width):

self. length = length

self. width = width

def area (self):

return self. length * self. width

def perimeter (self):

return 2 * self. length + 2 * self. width

+5
Answers (1)
  1. 3 February, 10:54
    0
    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

    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).
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “For this exercise, you'll use the Rectangle class below (you can assume that the length and width are measured in feet). Write a class ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers