w3resource

Java: Compute the area of a polygon

Java Basic: Exercise-35 with Solution

Write a Java program to compute the area of a polygon.

Area of a polygon = (n*s^2)/(4*tan(π/n))
where n is n-sided polygon and s is the length of a side.

Test Data:
Input the number of sides on the polygon: 7
Input the length of one of the sides: 6

Pictorial Presentation: Area of Polygon

Java: Compute the area of a polygon

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise35 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        // Prompt the user to input the number of sides on the polygon
        System.out.print("Input the number of sides on the polygon: ");
        
        // Read the number of sides from the user
        int ns = input.nextInt();
        
        // Prompt the user to input the length of one of the sides
        System.out.print("Input the length of one of the sides: ");
        
        // Read the length of one side from the user
        double side = input.nextDouble();
        
        // Calculate and display the area of the polygon
        System.out.print("The area is: " + polygonArea(ns, side) + "\n");
    }
    
    public static double polygonArea(int ns, double side) {
        // Calculate the area of a polygon based on the number of sides and side length
        return (ns * (side * side)) / (4.0 * Math.tan((Math.PI / ns)));
    }
}

Explanation:

In the exercise above -

  • First, it uses the "Scanner" class to obtain user input.
  • The user is prompted to input the number of sides on the polygon using System.out.print("Input the number of sides on the polygon: "), and the input is read into the variable 'ns' as an integer.
  • The user is prompted to input the length of one of the sides of the polygon using System.out.print("Input the length of one of the sides: "), and the input is read into the variable 'side' as a double.
  • The code then calls a separate method named "polygonArea" and passes the number of sides ns and the side length 'side' as arguments.
  • Inside the "polygonArea()" method:
    • It calculates the area of the polygon using the formula (ns side^2) / (4 tan(π / ns)), where 'ns' is the number of sides, 'side' is the length of one side, and tan(π / ns) represents the tangent of the angle formed by each side of the polygon, given by π / ns.
    • The calculated area is returned as a double.
  • In the "main()" method, the result of the "polygonArea()" method is printed, displaying the area of the polygon based on the user-provided number of sides and side length.

Sample Output:

Input the number of sides on the polygon: 7                                                                   
Input the length of one of the sides: 6                                                                       
The area is: 130.82084798405722 

Flowchart:

Flowchart: Java exercises: Compute the area of a polygon

Java Code Editor:

Previous: Write a Java program to compute the area of a hexagon.
Next: Write a Java program to compute the distance between two points on the surface of earth.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/basic/java-basic-exercise-35.php