Java Program to Convert Kilometer Per Hour to Meters Per Second

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

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

Java Program to Convert Kilometer Per Hour to Meters Per Second

Kilometer is used as a unit to measure length of long distance like road distance and Meter is used as a unit of length to measure smaller length.

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

1 Kilometer Per Hour = 0.277778 Meter Per Second

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

Feet Per Second =  Kilometer Per Hour * 0.277778

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

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

Approach:

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

Program:

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

1.0 kilometer per hour is equal to 0.277778 meters per second

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

Approach:

  • Take the value of double variable ‘kilometerPerhour‘ as user input by using Scanner class.
  • Then convert kilometer per hour to meters per second 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 kilometer per hour: ");
        //declared double variable kilometerPerHour 
        //and taking it's value as user input
        double kilometerPerHour=sc.nextDouble();
        //converting kilometer per hour to meters per second
        double metersPerSecond=kilometerPerHour * 0.277778;
        //printing result
        System.out.println(kilometerPerHour+" kilometer per hour is equal to "+metersPerSecond+" meters per second");
    }
}
Output:

Enter the value of kilometer per hour: 60
60.0 kilometer per hour is equal to 16.666680000000003 meters per second

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

Approach:

  • Take the value of double variable ‘kilometerPerHour‘ as user input by using Scanner class.
  • Then call a user defined method say convertValue() by passing kilometerPerHour¬†value as parameter.
  • Inside method convert kilometer per hour to meters per second 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 kilometer per hour: ");
        //declared double variable kilometerPerHour 
        //and taking it's value as user input
        double kilometerPerHour=sc.nextDouble();
        //calling user defined method converValue()
        convertValue(kilometerPerHour);
    }
    
    //convertValue() user defined method 
    public static void convertValue(double kilometerPerHour)
    {
        //converting kilometer per hour to meters per second
        double metersPerSecond=kilometerPerHour * 0.277778;
        //printing result
        System.out.println(kilometerPerHour+" kilometer per hour is equal to "+metersPerSecond+" meters per second");
    }
}
Output:

Enter the value of kilometer per hour: 17
17.0 kilometer per hour is equal to 4.722226 meters per second

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: