Answer to Question #344567 in Python for Tahir Abbas

Question #344567

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



1
Expert's answer
2022-05-24T13:57:36-0400
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()
   

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS