Question

N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and...

N-Sided Polygon

An n-sided polygon is a planed figure whose sides have the same length and whose angles have the same degree.

Write a class called NSidedPolygon that contains the following components:

Private members to store the name of a polygon, the number of sides, and the length of each side. (You are expected to select the appropriate data types for each member.)

A constructor that accepts the number of sides and the length of each side.

A private method called setPolygonName that accepts no parameters.

This method should use a switch statement to assign the following names to the polygon, based on the number of sides:

Number of sides                                    Polygon

           3 Triangle

           4 Quadrilateral

           5 Pentagon

           6 Hexagon

           7 Heptagon

           8                                   Octagon

           9 Nonagon

10 Decagon

A method that calculates the perimeter of the polygon.

Recall: perimeter = number of sides * length of side.

A method that calculates the area of an n-sided polygon.

Recall: area = (n * s2) / (4 * tan (?/ n)). (You used this formula in assignment 5.)

A toString method that returns the name of the polygon, the length of each side, the perimeter and the area.

Create a DecimalFormat object within this method to format the perimeter and area to 3 decimal places.

Write a driver program to test your NSidedPolygon class. Your code should:

Ask the user for the number of polygons they want to enter. (Users must enter at least 1 polygon.)

Use the number of polygons (from 1.) to create an array of NSidedPolygons.The loop for this array should ask the user for the number of sides and the length of sides for each polygon the array will store.

Users must enter a value between 3 and 10 (inclusive) for the number of sides.

Users must enter a value greater than 1 for the length of each side.

The NSidedPolygon objects should be created within this loop.

Display the name of the polygon, the length of the side, the perimeter and the area for each polygon in the array.

Samples of the output are shown below. (Note, your output does not have to have the exact wording or format, but should accomplish the same tasks).

How many polygons? 0
You must enter at least 1 polygon!
Please re-enter: 3
Enter number of sides (between 3 and 10) for polygon1: 8
Enter length of sides (>= 1) for polygon1: 0.5
Side length must be >= 1. Re-enter length of side for polygon1: 1.5

Enter number of sides (between 3 and 10) for polygon2: 11
Sides must be between 3 and 10. Re-enter number of sides for polygon2: 2
Sides must be between 3 and 10. Re-enter number of sides for polygon2: 5
Enter length of sides (>= 1) for polygon2: 6.2

Enter number of sides (between 3 and 10) for polygon3: 9
Enter length of sides (>= 1) for polygon3: 3.4

Here is the information for all the 3 polygons:
Name: Octagon        Length of side: 1.5     Perimeter: 12     Area 10.864
Name: Pentagon       Length of side: 6.2     Perimeter: 31     Area 66.135
Name: Nonagon        Length of side: 3.4     Perimeter: 30.6      Area 71.462

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a input Output Regular Polygon 1 Area: 41.5947135609 Perimerter 24 Regular Polygon 2 Area: 123.174088144

==========================================================

Program

==========================================================

#for tan function imported math library
import math

class RegularPolygon:

   #Constructor defination
   def __init__(self, n, side,x=0,y=0):

       self.n = n
       self.side = side
       self.x=x
       self.y=y

   #All Accessors and mutators methods   
   def getNumberOfSides(self):

       return self.n
  
   def setNumberOfSides(self,n):
       self.n=n
  
   def getSideLength(self):
       return self.side
  
   def setSideLength(self,side):
       self.side=side

   def getXValue(self):
       return self.x
  
   def setXValue(self,x):
       self.x=x

   def getYValue(self):
       return self.y
  
   def setYValue(self,y):
       self.y=y

   #Area function
   def getArea(self):

       area = (self.n*self.side*self.side) /(4*math.tan(3.14/self.n))
       return area
      
   #Perimeter function
   def getPerimeter(self):

       perimeter = self.n*self.side
       return perimeter


#creating regular polygon object 1
reg1 = RegularPolygon(6,4)

print "Regular Polygon 1"
print "Area: "
print reg1.getArea()
print "Perimerter"
print reg1.getPerimeter()

#creating regular polygon object 2
reg2 = RegularPolygon(10,4,5.6,7.8)

print "Regular Polygon 2"
print "Area: "
print reg2.getArea()
print "Perimerter"
print reg2.getPerimeter()

Add a comment
Know the answer?
Add Answer to:
N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of the same length...

    //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). Design a class named RegularPolygon that contains: Aprivate int data field named n that defines the number of sides in the polygon with default value 3. A private double data field named side that stores the length of the side with default value 1. A private double data field...

  • (Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all...

    (Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all of its angles have the same degree (i.e., the polygon is both equilateral and equiangular). Design a class named RegularPolygon that contains: ■ A private int data field named n that defines the number of sides in the polygon. ■ A private float data field named side that stores the length of the side. ■ A private float data field named x that defines...

  • A regular polygon is an n-sided polygon in which all sides are of the same length...

    A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon isArea =Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area. Here is a sample run:

  • Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on...

    Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on page 362, reprinted below 9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides have the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular) Design a class named RegularPolygon that contains A private int data field named n that defines the number of sides in the polygon with default value of 3 A...

  • With the given C++ code, what is the best way to utilize the 'calculatePerimeter()' and 'calculateArea()'...

    With the given C++ code, what is the best way to utilize the 'calculatePerimeter()' and 'calculateArea()' functions in the inheriting class to work with multiple different polygons? I need them to work for polygon 'triangle' all the way to the polygon 'octagon', but am not sure how to implement the functions to work with the different shapes without making more inheriting classes. Here is what I have so far: class Shape { private: string color; string type; public: Shape(string c){...

  • symmetry nected in a line of 9. Explain why only three types of regular polygons tessellate...

    symmetry nected in a line of 9. Explain why only three types of regular polygons tessellate the plane. Choose the correct answer below. O A. In order for a regular polygon to tessellate the plane, its angle bisectors must intersect at 90, angles. OB. In order for a regular polygon to tessellate the plane, Its exterior angle measure must be a divisor of 360 Only three regular polygons have exterior angles that divide 360 Those polygons are the equilateral triangle,...

  • Write a program that displays a convex regular polygon with m or n sides, where m...

    Write a program that displays a convex regular polygon with m or n sides, where m is the last digit of your student number plus 3, n is the second last digit plus 3. Use two buttons named “before” and “after” to change the number of sides of the polygon. Click on “before” button will show a convex regular polygon with m sides and click on “after” will show a convex regular polygon with n sides. For example, a student...

  • A regular n-gon is a polygon with n sides of equal length, in which all angles...

    A regular n-gon is a polygon with n sides of equal length, in which all angles are equal. Join every pair of vertices by a chord, and let f(n) be the number of pairs of chords that cross each other; so for example f(3) = 0, f(4) 1, and f(5) = 5. Find a formula for f(n).

  • Java Lab In this lab, you will be implementing the following class hierarchy. Your concrete classes...

    Java Lab In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides (which is constant for each concrete shape). The perimeter method is implemented in the superclass as it does not change based on the number of sides. The area method must be overridden in each subclass as the area is dependent on the type of polygon. The areas of an equilateral triangle, square, and...

  • Write a function makePoly(x,y,n,l) that receives a coordinate point (x,y), an integer n representing the desired...

    Write a function makePoly(x,y,n,l) that receives a coordinate point (x,y), an integer n representing the desired number of sides (3 to 20) , and an integer l between 10 and 200 pixels, inclusive, representing the length of each side. The function will then use the turtle to draw the requested n-sided polygon at the designated (x,y) coordinate, having sides of length l. Be sure to reject arguments that do not conform to the restrictions given. (Hint: to get a turtle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT