Java Program to Convert Hectare to Square Meter and Square Meter to Hectare

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

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

Java Program to Convert Hectare to Square Meter and Square Meter to Hectare

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

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

1 Hectare =  10000 Square Meter
1 Square Meter =  1e-4 Hectare

Formula to convert Hectare to Square Meter.

Square Meter = Hectare * 10000

Formula to convert Square Meter to Hectare.

Hectare  = Square Meter * 1e-4

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

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

Approach:

  • Declare Hectare and Square Meter value.
  • Then convert Hectare to Square Meter and Square Meter  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 meter declared
        double squaremeter = 1;
        //value of hectare declared
        double hectare = 1;
        
        //converting Hectare to square meter
        double sm = hectare * 10000;
        //converting square meter to Hectare 
        double h = squaremeter * 1e-4;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square meter: "+ sm);   
        System.out.println("Value of "+squaremeter+" square meter in hectare: "+ h);   
   }
}
Output:

Value of 1.0 hectare in square meter: 10000.0
Value of 1.0 square meter in hectare: 1.0E-4

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

Approach:

  • Take user input of Hectare and Square Meter value.
  • Then convert Hectare to Square Meter and Square Meter 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 squaremeter
        System.out.println("Enter value of square meter: ");  
        double squaremeter = 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 meter
        double sm = hectare * 10000;
        //converting square meter to Hectare 
        double h = squaremeter * 1e-4;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square meter: "+ sm);   
        System.out.println("Value of "+squaremeter+" square meter in hectare: "+ h);   
   }
}
Output:

Enter value of square meter: 
50000
Enter value of hectare: 
50
Value of 50.0 hectare in square meter: 500000.0
Value of 50000.0 square meter in hectare: 5.0

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 Meter value.
  • Call a user defined method by passing Hectare and Square Meter value as parameter.
  • Inside method convert Hectare to Square Meter and Square Meter 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 squaremeter
        System.out.println("Enter value of square meter: ");  
        double squaremeter = 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(squaremeter, hectare);
   }
   
   //convert() method to convert square meter to hectare and vice versa
   public static void convert(double squaremeter, double hectare)
   {
        
        //converting Hectare to square meter
        double sm = hectare * 10000;
        //converting square meter to Hectare 
        double h = squaremeter * 1e-4;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square meter: "+ sm);   
        System.out.println("Value of "+squaremeter+" square meter in hectare: "+ h);   
   }
}
Output:

Enter value of square meter: 
40909
Enter value of hectare: 
2
Value of 2.0 hectare in square meter: 20000.0
Value of 40909.0 square meter in hectare: 4.0909

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: