Question

In: Computer Science

Python Please An n-sided regular polygon has all its sides of the same length and all...

Python Please

An n-sided regular polygon has all its sides of the same length and all its angles of the same degree. It is also called an equilateral and equiangular polygon. In this activity, you will design a class named RegularPolygon that contains:

  1. A private int data attribute named n that defines the number of sides of the polygon.
  2. A private float attribute named side that stores the length of the side.
  3. The constructor ( __init__ method) that creates a regular polygon with the specified n (default 3), side (default 1).
  4. The accessor and mutator methods for all data fields.
  5. The method getPerimeter () that returns the perimeter of the polygon. (use perimeter = n*side)
  6. The method getArea() the returns the area of the polygon. The formula for computing the area of a regular polygon of area = (n*s2)/(4*tan(pi/n)), s is the side.
  7. A __str__ method that will display the data attributes of RegularPolygon class.

Write a main function to test the RegularPolygon class.

  1. Create four objects of RegularPolygon class using a variety of calls to the initializer (or constructor).
  2. Make calls to the mutator methods and display the object each time.
  3. Make calls the accessor methods and display the data attribute each time.
  4. Make calls to the getPerimeter and getArea and display the value returned each time.

Solutions

Expert Solution

import math

class RegularPolygon:

    # default sides is 3 and default side of length = 1
    # all the members are set private
    def __init__(self, n = 3, side = 1):
        self.__n = n
        self.__side = side

    # accessors and mutators for sides and length
    # of side
    def setN(self, n):
        self.__n = n

    def getN(self):
        return self.__n

    def setSide(self, side):
        self.__side = side

    def getSide(self):
        return self.__side

    # calculating perimeter
    def getPerimeter(self):
        return self.__n * self.__side

    # calculating area
    def getArea(self):
        return (self.__n*self.__side)/(4*math.tan(math.pi/self.__n))

    # returns the string of all properties
    # of RegularPolygon object
    def __str__(self):
        return "Side         : " + str( self.getSide() ) + "\n"+ \
               "No. of Sides : " + str( self.getN() ) + "\n" + \
               "Perimeter    : " + str( self.getPerimeter() ) + "\n" + \
               "Area         : " + str( self.getArea() );


if __name__ == '__main__':

    rp1 = RegularPolygon()
    print(rp1)
    print("----------------------------")
    rp2 = RegularPolygon(5, 12.5)
    print(rp2)
    print("----------------------------")
    rp3 = RegularPolygon()
    rp3.setN(6)
    rp3.setSide(10.5)
    print(rp3)
    print("----------------------------")
    rp4 = RegularPolygon(side = 10)
    print(rp4)
    print("----------------------------")

Code

Output


Related Solutions

Find the perimeter of a regular 36 sided regular polygon inscribed in a circle with a...
Find the perimeter of a regular 36 sided regular polygon inscribed in a circle with a radius of 14 units. Then find the size of one of the inscribed angles.
12 identical charges q are placed at the corners of a regular 12-sided polygon. a) What...
12 identical charges q are placed at the corners of a regular 12-sided polygon. a) What is the net force on a test load Q at its center. b) Suppose one of 12q is removed, what will be the total force on Q?
Q 4 One can think of a circle as an N-sided polygon where N is a...
Q 4 One can think of a circle as an N-sided polygon where N is a very very large number. This is because as the number of sides of a polygon increases the shape of the polygon starts resembling that of a circle. In the light of this fact explain why the diffraction pattern produced by the aperture DOTS is made of alternating bright and dark circular bands. Hint: First determine the diffraction pattern produced by an N-sided polygon. Then...
Consider a rhombus that is not square (i.e., the four sides all have the same length, but the angles between sides is not 90°).
Consider a rhombus that is not square (i.e., the four sides all have the same length, but the angles between sides is not 90°). Describe all the symmetries of the rhombus. Write down the Cayley table for the group of symmetries,
The base of a right pyramid is a regular hexagon with sides of length 10 m....
The base of a right pyramid is a regular hexagon with sides of length 10 m. The altitude is 5 m. Find the total surface area of the pyramid. The area is _______m2.
A square membrane has all four sides (length a) tightly clamped. Some of its vibrational modes...
A square membrane has all four sides (length a) tightly clamped. Some of its vibrational modes are also dampened by a clamp placed at the center of the membrane. Find the first four distinct vibrational frequencies of this "square drum."
A convex polygon is defined as a polygon where all its internalangles are less than...
A convex polygon is defined as a polygon where all its internal angles are less than 180 degrees and no edges cross each other. You can assume the vertex with the smallest x-coordinate (assuming origin at bottom left) is the first coordinate and other vertices are numbered in a counter-clockwise direction. Figure 1 shows an example of such a polygon where V[1] is the first polygon. For simplicity, you can also assume all polygon vertices have distinct x and y...
A pentagon is formed by adding two sides of the same length to attach an isoceles...
A pentagon is formed by adding two sides of the same length to attach an isoceles triangle to one side of a rectangle. If the area is fixed at A, what are the dimensions (in terms of A) that will minimize the pentagon's perimeter?
Show that the number of triangulations of a regular n-gon is the same as the number...
Show that the number of triangulations of a regular n-gon is the same as the number of Catalan paths from (0,0) to (n−2, n−2). A Catalan path is defined as the following: we want to count the number of distinct paths from the point (0,0) to the point (n, n) subject to the following rules: •We must stay inside the box [0, n]×[0, n]. •We move one step at a time, either moving one unit East or one unit North....
4. Define the width of a rectangle as the longest length of its sides. Given a...
4. Define the width of a rectangle as the longest length of its sides. Given a closed rectangle A in Rn and a partition P of A, define the mesh of P as the maximum width of its subrectangles. Prove that a bounded function f : A → R is integrable on A if and only if, for every > 0, there is a δ > 0 such that U(f, P) − L(f, P) < for every partition P of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT