Home > Back-end >  Print the area of the polygon which is apothem*perimeter
Print the area of the polygon which is apothem*perimeter

Time:08-05

from cmath import tan
import math

class mainClass(object):
    def __init__(self, No_of_sides, length_of_sides):
        self.No_of_sides = No_of_sides
        self.length_of_sides = length_of_sides

    def peri(self): #This is the perimeter method
        p = self.No_of_sides*self.length_of_sides
        return p
    
    def apothem_(self): #This is the apothem method
        apo = self.length_of_sides/ (2* tan (180/self.No_of_sides))
        return apo

I am trying to print the area of the polygon in this class:

class areaclass(object): 
    def area_ (self):
        obj = mainClass()
        data1 = obj.apothem_()
        data2 = obj.peri()
        area = data1*data2
        print("The area of the polygon is: ", area)
        

mc = mainClass( No_of_sides = float(input("What is the no of polygon sides: ")),
            length_of_sides = float(input("What is the length of each side: ")))


ac=areaclass()

CodePudding user response:

The only instance of mainClass you are instantiating with values is mc when it is called by ac=areclass no values are provided to mainClass for No_of_sides, length_of_sides

CodePudding user response:

I analyzed your code and I believe that you need to rework the code a bit to perform class inheritance. In other words, instead of creating a main class and then trying to get information from it in your area class, the area class probably needs to be created from the main class. With that, I reworked your program a bit as noted in the following code snippet.

from cmath import tan
import math

class mainClass(object):
    def __init__(self, No_of_sides, length_of_sides):
        self.No_of_sides = No_of_sides
        self.length_of_sides = length_of_sides

    def peri(self): #This is the perimeter method
        p = self.No_of_sides*self.length_of_sides
        return p
    
    def apothem_(self): #This is the apothem method
        apo = self.length_of_sides/ (4 * tan (math.pi/self.No_of_sides))
        return apo
        
class areaclass(object): 
    def __init__(self, No_of_sides, length_of_sides):
        self.obj = mainClass(No_of_sides, length_of_sides)
    def area_ (self):
        data1 = self.obj.apothem_()
        data2 = self.obj.peri()
        area = data1*data2
        print("The area of the polygon is: ", area)
        return area
        

ac = areaclass(int(input("What is the no of polygon sides: ")), float(input("What is the length of each side: ")))
            
print(ac.area_())

Some of the main points to note:

  • The "areaclass" class is defined by referencing the "mainClass" class.
  • In the main test code, the object "ac" is defined by passing the number of sides and length of sides, which then gets passed along to the "mainClass".
  • In testing out the "apo" calculation, I found that I needed to revise the denominator to use a constant of "4" and use the value of "math.pi" as the tangent function is expecting radians.

With that, testing out various regular polygons for area appeared to calculate the proper area.

Give that a try if you would. No doubt, there are other bits you can do to polish this up. FYI, the "apo" formula returns a complex number but the "imaginary" part does come back as zero. You might want to check out just extracting the "real" number portion as that is the value you would want.

  • Related