Develop an inheritance hierarchy based upon a Polygon class that has abstract me
ID: 3878956 • Letter: D
Question
Develop an inheritance hierarchy based upon a Polygon class that has abstract methods area( ) and perimeter( ). Implement classes Triangle, Quadrilateral, Pentagon, Hexagon, and Octagon that extend this base class, with the obvious meanings for the area( ) and perimeter( ) methods. Also implement classes, IsoscelesTriangle, EquilateralTriangle, Rectangle, and Square, that have the appropriate inheritance relationships. Finally, write a simple program that allows users to create polygons of the various types and input their geometric dimensions, and the program then outputs their area and perimeter.
By python program
Explanation / Answer
class Polygon(object): def __init__(self, n): self.number_of_sides = n def print_num_sides(self): print('There are ' + str(self.number_of_sides) + 'sides.') class Triangle(Polygon): def __init__(self, lengths_of_sides): Polygon.__init__(self, 3) self.lengths_of_sides = lengths_of_sides # list of three numbers def get_area(self): a, b, c = self.lengths_of_sides # calculate the semi-perimeter s = (a + b + c) / 2 return (s*(s-a)*(s-b)*(s-c)) ** 0.5 class Rectangle(Polygon): def __init__(self, lenghts_of_sides): Polygon.__init__(self, 4) self.lengths_of_sides = lengths_of_sides # list of two numbers def get_area(self): x, y = self.lenghts_of_sides return x * y
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.