In: Computer Science
Python Program:
Description
Create an object-oriented program that uses inheritance to perform
calculations on a rectangle or a square.
Specifications
Sample Output (Your output should be similar or can be the same to the text in the following box)
|
Rectangle
Calculator Continue? (y/n): y Rectangle or square?
(r/s): s Continue? (y/n): n Thank you for using my app. |
class 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
class Square(Rectangle):
def __init__(self,length):
super().__init__(length,length)
if __name__== "__main__":
print("Rectangle Calculator")
while True:
c=input("Rectangle or Square? (r/s): ")
if c=='r':
l=int(input("Heigth: "))
b=int(input("Width: "))
rect=Rectangle(l,b)
print("Perimeter: ",rect.perimeter())
print("Area: ",rect.area())
if c=='s':
l=int(input("Length: "))
squa=Square(l)
print("Perimeter: ",squa.perimeter())
print("Area is: ",squa.area())
s=input("Continue? (y/n): ")
if s=='n':
break
print("Thank you for using my app")

Output:
