Java hexagon – Java Program to Find Area of Hexagon

Java hexagon: In the previous article, we have seen Java Program to Find Area of Octagon

In this article we are going to see how to find the area of hexagon using Java programming language.

Java Program to Find Area of Hexagon

Before Jumping into the program directly let’s see how we can find the area of hexagon.

Explanation:

 Formula to find area of hexagon (approx.) = (3*(√3/2))*a*a

Where,

  • a represents side length of a hexagon.

Example:

Let one of the sides of hexagon be “a” = 1
So, vol. of hexagon = (3*(√3/2))*a*a 
                             => (3*(√3/2))*1*1
                             => (3*(√3/2)) = 2.5980

Let’s see different ways to find the area of Hexagon.

Method-1: Java Program to Find Area of Hexagon By Using Static Value

Approach:

  • Declare a double variable say “a”, assign the value to it, where ‘a‘ holds the value for one of the side length of hexagon.
  • Find the surface area of hexagon using the formula (3*(√3/2))*a*a
  • Print the result.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        //declared side length value of hexagon 
        double a = 1;
        //find area of hexagon using formula
        double ar  = (3*(Math.sqrt(3)/2))*a*a;
        System.out.println("The area of hexagon is: " + ar);
    }
}

Output:

The area of hexagon is: 2.598076211353316

Method-2: Java Program to Find Area of Hexagon By Using User Input Value

Approach:

  • Declare a double variable say “a”, and take the value as user input, where ‘a‘ holds the value for one of the side length of hexagon.
  • Find the surface area of hexagon using the formula (3*(√3/2))*a*a
  • Print the result.

Program:

import java.util.*;
class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner sc = new Scanner(System.in);
        //taking user input of side length value of hexagon 
        System.out.println("Enter the value of one side of the hexagon:");
        double a = sc.nextDouble();
        
        //find area of hexagon using formula
        double ar  = (3*(Math.sqrt(3)/2))*a*a;
        System.out.println("The area of hexagon is: " + ar);
    }
}

Output:

Enter the value of one side of the hexagon:
2
The area of hexagon is: 10.392304845413264

Method-3: Java Program to Find Area of Hexagon By Using User Defined Method

Approach:

  • Declare a double variable say “a”, and take the value as user input, where ‘a‘ holds the value for one of the side length of hexagon.
  • Call a user defined method say findArea() and pass the side length i.e. ‘a‘ as parameter.
  • Inside the method find the surface area of hexagon using the formula (3*(√3/2))*a*a
  • Print the result.

Program:

import java.util.*;
class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner sc = new Scanner(System.in);
        //taking user input of side length value of hexagon 
        System.out.println("Enter the value of one side of the hexagon:");
        double a = sc.nextDouble();
        //calling user defined method findArea()
        findArea(a);
    }
    public static void findArea(double a)
    {
        //find area of hexagon using formula
        double ar  = (3*(Math.sqrt(3)/2))*a*a;
        System.out.println("The area of hexagon is: " + ar);
    }
}

Output: 

Enter the value of one side of the hexagon: 
2 
The area of hexagon is: 10.392304845413264

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Related Java Articles: