Question
8.21 Implement a class Polygon that abstracts regular polygons and supports class methods: __init__(): A constructor that takes as input the number of sides and
8.21 Implement a class Polygon that abstracts regular polygons and supports class methods: __init__(): A constructor that takes as input the number of sides and the side length of a regular n-gon (n-sided polygon) object perimeter(): Returns the perimeter of n-gon object area(): returns the area of the n-gon object Note: The area of a regular polygon with n sides of length s is s 2n 4 tan( n)
>>> p2 = Polygon(6, 1) >>> p2.perimeter() 6 >>> p2.area() 2.5980762113533165
8.37 Implement classes Square and Triangle as subclasses of class Polygon from Problem 8.21. Each will overload the constructor method __init__ so it takes only one argument l (the side length), and each will override method area() that computes the area using a simpler implementation. The method __init__ should make use of the superclass __init__ method, so no instance variables (l and n) are defined in subclasses. Note: The area of an equilateral triangle of side length s is s 2 3/4. >>> s = Square(2) >>> s.perimeter() 8 >>> s.area() 4 >>> t = Triangle(3) >>> t.perimeter() 9 >>> t.area() 6.3639610306789285
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started