Java Program to Convert Hectare to Square Mile and Square Mile to Hectare

In the previous article, we have discussed about Java Program to Convert Hectare to Square Meter and Square Meter to Hectare

In this article we will see how to convert Hectare to Square Mile and Square Mile to Hectare by using Java programming language.

Java Program to Convert Hectare to Square Mile and Square Mile to Hectare

Before jumping into the program let’s know the relationship between Hectare and Square Mile and how we can convert Hectare to Square Mile and vice versa.

Generally Hectare and Square Mile are used as unit in case of field/land length measurement.

1 Hectare =  0.00386102 Square Mile
1 Square Mile =  258.999 Hectare

Formula to convert Hectare to Square Mile.

Square Mile  = Hectare * 0.00386102

Formula to convert Square Mile to Hectare.

Hectare  = Square Mile  * 258.999

Let’s see different ways to convert Hectare to Square Mile and Square Mile to Hectare.

Method-1: Java Program to Convert Hectare to Square Mile and Square Mile to Hectare By Using Static Input Value

Approach:

  • Declare Hectare and Square Mile value.
  • Then convert Hectare to Square Mile and Square Mile  to Hectare by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //value of square mile declared
        double squaremile = 1;
        //value of hectare declared
        double hectare = 1;
        
        //converting Hectare to square mile
        double sm = hectare * 0.00386102;
        //converting square mile to Hectare 
        double h = squaremile * 258.999;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square mile: "+ sm);   
        System.out.println("Value of "+squaremile+" square mile in hectare: "+ h);   
   }
}
Output:

Value of 1.0 hectare in square mile: 0.00386102
Value of 1.0 square mile in hectare: 258.999

Method-2: Java Program to Convert Hectare to Square Mile and Square Mile to Hectare By Using User Input Value

Approach:

  • Take user input of Hectare and Square Mile value.
  • Then convert Hectare to Square Mile and Square Mile to Hectare by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable squaremile
        System.out.println("Enter value of square mile: ");  
        double squaremile = sc.nextDouble();
        //Taking the value input of double variable hectare
        System.out.println("Enter value of hectare: ");  
        double hectare = sc.nextDouble();
        
        //converting Hectare to square mile
        double sm = hectare * 0.00386102;
        //converting square mile to Hectare 
        double h = squaremile * 258.999;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square mile: "+ sm);   
        System.out.println("Value of "+squaremile+" square mile in hectare: "+ h);   
   }
}
Output:

Enter value of square mile: 
20
Enter value of hectare: 
2000
Value of 2000.0 hectare in square mile: 7.722040000000001
Value of 20.0 square mile in hectare: 5179.9800000000005

Method-3: Java Program to Convert Hectare to Square Meter and Square Meter to Hectare By Using User Defined Method

Approach:

  • Take user input of Hectare and Square Mile value.
  • Call a user defined method by passing Hectare and Square Mile value as parameter.
  • Inside method convert Hectare to Square Mile and Square Mile to Hectare by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable squaremile
        System.out.println("Enter value of square mile: ");  
        double squaremile = sc.nextDouble();
        //Taking the value input of double variable hectare
        System.out.println("Enter value of hectare: ");  
        double hectare = sc.nextDouble();
        //calling user defined method convert()
        convert(squaremile, hectare);
   }
   
   //convert() method to convert square mileto hectare and vice versa
   public static void convert(double squaremile, double hectare)
   {
        //converting Hectare to square mile
        double sm = hectare * 0.00386102;
        //converting square mile to Hectare 
        double h = squaremile * 258.999;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square mile: "+ sm);   
        System.out.println("Value of "+squaremile+" square mile in hectare: "+ h);   
   }
}
Output:

Enter value of square mile: 
2
Enter value of hectare: 
200
Value of 200.0 hectare in square mile: 0.772204
Value of 2.0 square mile in hectare: 517.998

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: