Chapter 6 Question 36 (Geometry: area of a regular polygon)
*6.36 (Geometry: the area of a regular polygon) A regular polygon is a polygon with n sides, each of which has the same length and the angles of all corners (that is, the polygon is both equilateral and equiangular). The formula for calculating the area of a regular polygon is:
use the following method header to write the method to return the area of the regular polygon: public static double area(int n, double side) Write a main method that prompts the user to enter the number of sides and the sides of the regular polygon Long, and then display its area. Here is a running example: Enter the number of sides: 5 Enter the side:6.5 The area of the polygon is 72.690170 *6.36(Geometry: area of a regular polygon) A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (ie, the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is
Write a method that returns the area of a regular polygon using the following header: public static double area(int n, double side) Write a main method that prompts the user to enter the number of sides and the side of a regular polygon and displays its area. Here is a sample run: Enter the number of sides: 5 Enter the side:6.5 The area of the polygon is 72.690170
Reference Code:
package chapter06;import java.util.Scanner;publicclassCode_36{
publicstaticvoidmain(String[] args){
Scanner inputScanner =newScanner(System.in);
System.out.print("Enter the number of sides: ");int numberOfSide = inputScanner.nextInt();
System.out.print("Enter the side: ");double side = inputScanner.nextDouble();double area =area(numberOfSide, side);
System.out.printf("The area of the polygon is %f", area);}publicstaticdoublearea(int n,double side){
double area =(n * Math.pow(side,2))/(4* Math.tan(Math.PI / n));return area;}}
The results show that:
Enter the number of sides:5
Enter the side:6.5
The area of the polygon is 72.690170
Process finished with exit code 0