In: Computer Science
1. Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method) to test these two classes i need this in python
Answer: here is the answer for your question:
code:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
class rectangle(object):
def __init__(self,width,height):
self.width=width
self.height=height
def cal_perimeter(self):
return 2*self.width*self.height
def cal_area(self):
return self.width*self.height
class Box(rectangle):
def __init__(self,width,height,length):
self.length=length
rectangle.__init__(self,width,height)
def box_area(self):
return self.width*self.height
def box_volume(self):
return self.width*self.height*self.length
if __name__ == '__main__':
obj1=rectangle(20,40)
print("perimeter of rectangle is
:"+str(obj1.cal_perimeter()))
print("Area of rectangle is :"+str(obj1.cal_area()))
obj2=Box(10,20,30)
print("Area of rectangle is : "+str(obj2.box_area()))
print("perimeter of rectangle is :"+str(obj2.box_volume()))
print("END OF PROGRAM ")
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
OUTPUT:
I hope this would help you out
If you have any doubt, you can provide comment /feedback below the answer
Thanks