1.9k views
5 votes
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 Regular Polygon that contains:

A private 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 named "x" that defines the x-coordinate of the polygon's
center with a default value of 0.
A private double data field named "y" that defines the y-coordinate of the polygon's center with a default value of 0.
A no-arg constructor that creates a regular polygon with default values.
A constructor that creates a regular polygon with the specified number of sides and length of side, centered at (0,0).
A constructor that creates a regular polygon with the specified number of sides and length of side, and x- and y-coordinates for the center.
The accessor and mutator methods for all data fields.
The method getPerimeter() that return the perimeter of the polygon.
The method getArea() that returns the area of the polygon. (The formula of a polygon with n sides with length s is: (n* s2)/(4* tan (π/n)))
Draw the UML diagram for the class and then implement the class. Write a test program that creates three Regular Polygon objects, created using a no-arg constructor, using Regular Polygon(6, 4), and using Regular Polygon(10, 4, 5.6, 7.8). For each object display its perimeter and area.

User Goralight
by
7.4k points

1 Answer

0 votes

Final answer:

A Regular Polygon class can be designed to represent a regular polygon with a specific number of sides and length of side. The class should include private data fields for the number of sides, length of side, and center coordinates, as well as constructors, accessor and mutator methods, and methods to calculate the perimeter and area of the polygon.

Step-by-step explanation:

A regular polygon is a polygon where all sides have the same length and all angles have the same degree. To design a class named RegularPolygon that represents this concept, we can include private data fields such as 'n' to store the number of sides, 'side' to store the length of the side, and 'x' and 'y' to store the coordinates of the polygon's center.

We can also include constructors to create regular polygons with default values or with specified values for the number of sides, length of side, and center coordinates. Accessor and mutator methods can be used to get and set the values of the data fields. In addition, the Regular Polygon class should have methods to calculate the perimeter and area of the polygon.

The get Perimeter() method can calculate the perimeter by multiplying the number of sides by the length of each side, and the get Area() method can calculate the area using the formula provided in the question: (n * s^2) / (4 * tan(π/n)). Finally, a test program can be written to create three Regular Polygon objects using different constructors and display their perimeters and areas.

User Boris N
by
7.8k points