The purpose of this assignment is to have you write a Java program us.docx
1. The purpose of this assignment is to have you write a Java
program using methods to compute and output the area of an n
sided polygon where the area is defined by the following
mathematical formula: area = (n * (s * s))/(4 * tan(3.1416/n))
where s = length of a side of a regular polygon with all sides of
equal length and all angles are the same and n = the number of
sides. For this assignment set n = 5 for a pentagon and let the
side s vary from 1 to 20. Your output should let the size of a
side vary from 1 to 20. You should have one method to compute
the resulting area and another method to determine whether the
area is consider small, medium or large according to the
following table. Your column headings should include side,
area and classification.
Solution
Please follow the code and comments for dscription :
CODE :
import java.util.Scanner;
public class PolygonArea {
@SuppressWarnings("MalformedFormatString")
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of sides: ");
int numberOfSides = input.nextInt();
2. double side = 0;
System.out.println("The tabulated areas of the polygon
for different sides from 1 to 20 are as follows :");
System.out.println(" ");
System.out.format("%32s %10s %15s",
"SIDE", "AREA", "CLASSIFICATION");
for (int x = 1; x <= 20; x++) {
side = x;
double parea = area(numberOfSides, side);
String classification = cls(parea);
System.out.println(" ");
System.out.format("%32s %10s %15s", side,
parea, classification);
}
input.close();
}
public static double area(int n, double side) {
double answer = (n * (side * side)) / (4 *
(Math.tan((Math.PI / n))));
return answer;
}
3. public static String cls(double ar){
String classi;
if(ar<=10){
classi = "Small";
}
else if (ar>= 11 && ar <=25){
classi = "Medium";
}
else{
classi = "Large";
}
return classi;
}
}
OUTPUT :
Enter the number of sides:
5
The tabulated areas of the polygon for different sides from 1 to
20 are as follows :
SIDE AREA CLASSIFICATION
1.0 1.720477400588967 Small
2.0 6.881909602355868 Small
4. 3.0 15.484296605300703
Medium
4.0 27.52763840942347 Large
5.0 43.01193501472417 Large
6.0 61.93718642120281 Large
7.0 84.30339262885938 Large
8.0 110.11055363769388 Large
9.0 139.35866944770632 Large
10.0 172.0477400588967 Large
11.0 208.177765471265 Large
12.0 247.74874568481124 Large
13.0 290.7606806995354 Large
14.0 337.21357051543754 Large
15.0 387.10741513251753 Large
16.0 440.44221455077553 Large
17.0 497.2179687702114 Large
18.0 557.4346777908253 Large
19.0 621.092341612617 Large
20.0 688.1909602355868 Large
Hope this is helpful.