Question
In the previous week, you should have created a class called Rectangle. This week, you will perform inheritance with the Rectangle class. Some Background: Rugs
In the previous week, you should have created a class called Rectangle. This week, you will perform inheritance with the Rectangle class. Some Background: Rugs have a set size and are sold for a set price. On the other hand, Carpets are sold by cutting a swath from a roll -- so the size of a carpet (and its cost) can be variable. Create a class called Rug that inherits from Rectangle. Then give the Rug class a price variable. The length, width and price of the Rug should be set in the constructor of the Rug class. Give the Rug class a getPrice method that returns the cost. Also give the Rug class an overridden __str__() method that returns a string that is a report on the data in a Rug object. Create another class called Carpet that also inherits from Rectangle. It should have a price_per_square_foot variable. The length, width, and price_per_square_foot are set in the constructor of the Carpet class. Give the Carpet glass a getPrice method that calculates the price of the Carpet and returns it. Also give the Carpet class an overridden __str__() method that returns a string that is a report on the data in a Carpet object. Then write a main method in which a Rug and a Carpet object are created. Print a description of those objects using their __str__() methods.
The __str__() methods of the two classes should return a string that contains the following information:
Rug class: That the object is a rug, the rug's length, width, area, and price. Carpet class: That the object is a carpet, the carpet's length, width, area, cost per square foot and price.
Examples:
r = Rug(10, 10, 80) #note the constructor parameters are the length, width, and price print(r) #displays: 'Rug, length=10, width=10, area=100, cost=$80.00' c = Carpet(12,12,1.05) #note the constructor parameters are the length, width, and cost per square foot print(c) #displays: 'Carpet, length=12, width=12, area=144, cost per sq. foot=$1.05, total cost=$151.20'
Getting numbers into currency format can be tricky. The simplest way to do it is shown on page 255 of your textbook.
This is what i have done. Can you please let me know what changes i should do?
class rectangle(object): __length = 0 __width = 0 def __init__(self,length,width): self.__length = length self.__width = width def getLength(self): return self.__length def setLength(self,length): if length < 0: self.__length = 1 else: self.__length = length def getWidth(self): return self.__width def setWidth(self,width): if width < 0: self.__width = 1 else: self.__width = width def getArea(self): return self.__length*self.__width
class rug(rectangle): def__init__(self,length,width,price = 1) self.__price =price super().__init__(length,width)
def getPrice(self): return self._rectangle__length*self._rectangle def __str__(self): return "rug length:" + str(self._rectangle__length) str(self._rectangle__width) + ",price:" + str(self.__rectangle__price)
class carpet(rectangle): def__init__(self,length,width,price_per_square_foot) rectangle.__init__(self,length,width) self.__price_per_square_foot = price_per_square def getPrice(self): return self._rectangle__length*self._rectangle__length def__str__(self) return "carpet(length:" + str(self._rectangle__) str(self._rectangle__length) + ",price_per_square_foot" str (self.__price_per_square_foot) + ")"
def main(): while True: length = float(input("10:")) width = float(input("10:")) rect = rectangle(length, width) print("Area of rectangle:",rect.getArea()) Rug = rug(length,width,10) Carpet = carpet(length,width,11)
list= [Rug, Carpet] for obj in List: print(obj.__str__())
Choice = input("continue?(y/n):") print() if choice !="y": print("Bye!") break main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started