Skip to main content
Lesson 7 - More About Methods
Lesson MenuPreviousNext
  
L.A.7.2 - Polygon page 9 of 10

Background:

Polygons are two-dimensional shapes formed by line segments. The segments are edges that meet in pairs at corners called vertices. A polygon is regular if all its sides are equal and all its angles are equal.

For an n-sided regular polygon of side s, the angle at any vertex is θ, and the radii of the inscribed and circumscribed circles are r and R respectively. A 5-sided regular polygon (pentagon) would be represented as follows:


Assignment:

  1. Create a RegularPolygon class to model any regular polygon. Use the following declarations as a starting point for your lab work.

    class RegularPolygon
    {
    
       private int myNumSides;        // # of sides
       private double mySideLength;   // length of side
       private double myR;            // radius of circumscribed circle
       private double myr;            // radius of inscribed circle
        
       // constructors
       public RegularPolygon()
       {
       }
       
       public RegularPolygon(int numSides, double sideLength)
       {
       }
    
       // private methods
       private void calcr()
       {
       }   
     
       private void calcR()
       {
       }   
       
       // public methods
       public double vertexAngle()
       {
       }
    
       public double Perimeter()
       {
       }
       
       public double Area()
       {
       }
    
       public double getNumside()
       {
       }
    
       public double getSideLength()
       {
       }
       
       public double getR()
       {
       }
       
       public double getr()
       {
       }
    }
  2. Write two constructor methods. The default constructor creates a 3-sided polygon (triangle). The other constructor takes an integer value representing the number of sides and a double value representing the length of side, and constructs the corresponding regular polygon.

  3. Write a method that calculates the vertex angle, θ. This angle can be determined as follows:

    where n represents the number of sides.

  4. Write a method that calculates the perimeter. This value is determined simply as the number of sides, n, times the length of a side, s:

  5. Write a method that calculates the radius of the inscribed circle, r. The inscribed circle is the circle that can be drawn inside of the regular polygon such that it is tangent to every side of the polygon, for example, the smaller circle in the diagram above. It can be calculated as:

    where n represents the number of sides, s represents the length of a side, and cot() is the trigonometric function, cotangent. We use the value instead of 180 in the formula because the Java math functions assume that angles are given in radians instead of degrees. Note: the built-in Java method math.PI produces the value of . An alternative is to replace with 180 in all the formulas here and use following method from the Math Class to convert from degrees to radians:

    Math.toRadians(double angdeg)

    The cotangent function is not part of the Java Math library, however, the cotangent of an angle can be calculated as the reciprocal of the tangent as follows:

  6. Write a method that calculates the radius of the circumscribed circle, R. The circumscribed circle is the circle that intersects each vertex of the polygon contacts the circle, for example the larger circle in the diagram above. R can be calculated as:

    where n represents the number of sides, s represents the length of a side and csc() is the trigonometric function, cosecant. The cosecant function is not part of the Java Math Class, however, the cosecant of an angle can be calculated as the reciprocal of the sine as follows:

  7. Write a method that calculates the area of the regular polygon. It can be calculated as:

    where n represents the number of sides and R represents the radius of the circumscribed circle.

  8. All trigonometric methods in the Java Math Class take radians as the parameter. To convert an angle measured in degrees to the equivalent angle measured in radians use the following method from the Math Class:

    public static double toRadians(double angdeg)
  9. Write a testing class with a main() that constructs a RegularPolygon and calls each method. A sample run of the program for a polygon with 4 sides of length 10 would give:

    number of sides = 4
    length of side = 10.00
    radius of circumscribed circle = 7.07
    radius of inscribed circle = 5.00
    vertex angle = 90.0
    perimeter = 40.00
    area = 100.00

Instructions:

  1. Format all floating point values to two decimal places (0.01).

  2. Test the default constructor by constructing a regular polygon with no parameters as follows:

    RegularPolygon poly = new RegularPolygon();
  3. Use the following values to test your functions:

    Square: number of sides = 4, length of side = 10
    Octagon: number of sides = 8, length of side = 5.75
    Enneadecagon: number of sides = 19, length of side = 2
    Enneacontakaihenagon: number of sides = 91, length of side = 0.5

    Answers:

     nsθ(degrees)rR perimeter  area 
    Square41090.005.007.0740.0010.00
    Octagon85.75135.006.947.5146.00159.64
    Enneadecagon192161.055.996.0838.00113.86
    Enneacontakaihenagon910.5176.047.247.2445.5164.68


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.