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 q, 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:
Create a RegularPolygon class to model any regular polygon. Use the following declarations as a starting point for your lab work. Make sure you declare your methods and fields exactly as written here.
Write two constructor methods. The default constructor creates a 3-sided polygon (triangle) with side lengths 1. The other constructor takes an integer value representing the number of sides and a double value representing the length of the sides and constructs the corresponding regular polygon. In both cases, all the fields should be initialized to the correct values.
Write a public instance method named vertexAngle that calculates and returns the vertex angle as a double. This angle can be determined as follows:
where n represents the number of sides.
Write a public instance method named perimeter that calculates and returns the perimeter as a double. This value is determined simply as the number of sides, n, times the length of a side, s:
Write a private instance method named calcr that calculates the radius of the inscribed circle, r, and sets the value of the instance variable myr to the result. Make sure this method is called in each constructor so myr gets initialized. 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 Math class has a public static final field named PI
that holds the value of π. An alternative is to replace π with 180 in all the formulas here and use the toRadians method from the Math Class to convert from degrees to radians.
The cotangent function is not part of the Math class, however, the cotangent of an angle can be calculated as the reciprocal of the tangent as follows:
Write a private instance method named calcR that calculates the radius of the circumscribed circle, R, and sets the value of the instance variable myR to the result. Be sure to call this method in both constructors to initialize myR. The circumscribed circle is the circle that intersects each vertex of the polygon, 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 Math class, however, the cosecant of an angle can be calculated as the reciprocal of the sine as follows:
Write a public instance method named area that calculates the area of the regular polygon and returns the result as a double. It can be calculated as:
where n represents the number of sides and R represents the radius of the circumscribed circle.
All trigonometric functions in the 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)
Include getters for each of the instance variables: getNumSides, getSideLength, getR, and getr.
Test the default constructor by constructing a regular polygon with no parameters as follows:
P3_Wang_Michael_RegularPolygon poly = new P3_Wang_Michael_RegularPolygon();
Use the second constructor to test other regular polygons. 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 (these answers are rounded, but your answers should not be rounded):
All files must include your name and period in the format:
PX_LastName_FirstName_Title.java
. For example,P1_Wang_Michael_RegularPolygon.java
You must Sign In to submit to this assignment
Last modified: December 12, 2022
Back to Lab 6.1 Taxes