Problem a (LA2a.java) Write a program to compute the area, perimeter, and interior angle of a regular polygon. First, have the user supply the number of sides of the polygon (a whole number, which must be 3 or greater) and the side length (any positive number). Then, compute the area via the following equation: where n is the number of sides and s is the side length. Note that the equations are equivalent, with the first using degrees and the second using radians. Then, compute the perimeter via the following equation: and then the interior angle (in degrees) via the following equation: So, an example run of your program would look like the following: (texts marked with blue color are the user’s input.) Enter number of sides: 3 Enter side length: 1 Area: 0.433 Perimeter: 3.000 Interior Angle: 60.000 degrees You should express the outputs using exactly three decimal places (rounding where necessary). Tips: (i) the Math.tan() method will compute the tangent of a value in radians, (ii) the Math.toRadians() method will convert a value in degrees to radians, and (iii) the Math.PI constant can be used for pi (π). Area = s2n 4tan( 180 n ) = s2n 4tan( ⇡ n ) Perimeter = ns Interior Angle = 180 360
*****Make sure error messages println in console and the program stops when less than 3 sides are entered or when negative numbers are entered
******make sure the word degrees is outputted after the value of Interior angle is given
Program
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int n,s;
double per,iAngle, area;
Scanner in = new Scanner(System.in);
System.out.print("Enter number of
sides: ");
n=in.nextInt();
if(n<3)
{
System.out.println("Invalid
sides");
System.exit(0);
}
System.out.print("Enter side
length:");
s=in.nextInt();
if(s<1)
{
System.out.println("Invalid
sides");
System.exit(0);
}
double
t=Math.toRadians(180/n);
area=
(s*s*n)/(4*Math.tan(t));
per=n*s;
iAngle= (n-2)*180/n;
System.out.print("Area: "); System.out.format("%.3f",
area);
System.out.print("\nPerimeter: ");
System.out.format("%.3f", per);
System.out.print("\nInterior Angle: ");
System.out.format("%.3f", iAngle); System.out.print("
degrees");
}
}
Get Answers For Free
Most questions answered within 1 hours.