Java Program to Convert Meters Per Second to Kilometers Per Hour

In the previous article we have discussed about Java Program to Convert Meters Per Second to Feet Per Second

In this article we are going to see how to convert meters per second to kilometers per hour by using Java programming language.

Java Program to Convert Meters Per Second to Kilometers Per Hour

Meter is used as a unit of length measurement while length is small and Kilometer is used as a unit of length measurement like distance.

Before jumping into the program, first let’s know the relation between meters per second and kilometers per hour.

1 Meter Per Second = 3.6 Kilometer Per Hour

Then the formula to convert meters per second to kilometers per hour is

Kilometers Per Hour = Meters Per Second * 3.6

Let’s see different ways to convert meters per second to kilometers per hour.

Method-1: Java Program to Convert Meters Per Second to Kilometers Per Hour By Using Static Input Value

Approach:

  • Declare a double variable say ‘meterPerSecond‘ and initialize it’s value.
  • Then convert meters per second to kilometers per hour by using formula.
  • Print the result.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //declared value of meter per second
        double meterPerSecond=1;
        //converting meter per second to kilometer per hour
        double kilometerPerHour=meterPerSecond*3.6;
        //printing result
        System.out.println(meterPerSecond+" meter per second is equal to "+kilometerPerHour+" kilometer per hour");
    }
}
Output:

1.0 meter per second is equal to 3.6 kilometer per hour

Method-2: Java Program to Convert Meters Per Second to Kilometers Per Hour By Using User Input Value

Approach:

  • Take the value of double variable ‘meterPerSecond‘ as take its’ value user input.
  • Then convert meters per second to kilometers per hour by using formula.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the value of meter per second: ");
        //declared double variable meterPerSecond 
        //and taking it's value as user input
        double meterPerSecond=sc.nextDouble();
        //converting meter per second to kilometer per hour
        double kilometerPerHour=meterPerSecond*3.6;
        //printing result
        System.out.println(meterPerSecond+" meter per second is equal to "+kilometerPerHour+" kilometer per hour");
    }
}
Output:

Enter the value of meter per second: 10
10.0 meter per second is equal to 36.0 kilometer per hour

Method-3: Java Program to Convert Meters Per Second to Kilometers Per Hour By Using User Defined Method

Approach:

  • Take the value of double variable ‘meterPerSecond‘ as take its’ value user input.
  • Then call a user defined method say convertValue() by passing meterPerSecond¬†value as parameter.
  • Then convert meters per second to kilometers per hour by using formula.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the value of meter per second: ");
        //declared double variable meterPerSecond 
        //and taking it's value as user input
        double meterPerSecond=sc.nextDouble();
        //calling user defined method converValue()
        convertValue(meterPerSecond);
    }
    
    //convertValue() user defined method 
    public static void convertValue(double meterPerSecond)
    {
        //converting meter per second to kilometer per hour
        double kilometerPerHour=meterPerSecond*3.6;
        //printing result
        System.out.println(meterPerSecond+" meter per second is equal to "+kilometerPerHour+" kilometer per hour");
    }
}
Output:

Enter the value of meter per second: 6
6.0 meter per second is equal to 21.6 kilometer per hour

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: