Do the question with only python
Write a program in which you need to
Create a class called ‘SHAPE’ (should be made an abstract class) having two instance variable Length (double), Height (double)
instance method Get Data () to get and assign the values (to length &Height) from the user
it also has a method Display Area () to compute and display the area of the geometrical object.
Inherit two specific classes ‘TRIANGLE’ and ‘RECTANGLE’ from the base class that utilize the base class method.
Using these three classes design a program that will accept dimension of a triangle/rectangle interactively and display the area.
Hint:
Area of Triangle=1/2(L*h)
Area of Rectangle=L*h
class SHAPE():
Length = 0.0
Height = 0.0
def getData(self, length = None, height = None):
if (length!= None):
self.Length = length
if (height!= None):
self.Height = height
return [self.Length,self.Height]
def displayArea(self):
raise NotImplementedError()
def usesdisplayArea(self):
return self.displayArea() + 1
class TRIANGLE(SHAPE):
def displayArea(self):
print(self.Length * self.Height * 0.5)
class RECTANGLE(SHAPE):
def displayArea(self):
print( self.Length * self.Height)
while(True):
print("Rectangle or triangle [r/t](q to stop)?")
enteredWord = input().lower()
if(enteredWord[0]=="r"):
tempShape = RECTANGLE()
elif(enteredWord[0] == "t"):
tempShape = TRIANGLE()
elif(enteredWord[0]=="q"):
break
print("Enter height and length: ")
tempShape.Length, tempShape.Height = [int(x) for x in input().split()]
tempShape.displayArea()
Comments
Leave a comment