Java Program to Convert Miles Per Hour to Meter Per Second

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

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

Java Program to Convert Miles Per Hour to Meter Per Second

Both Miles and Meter are used as a unit of length to measurement. While Mile is used for long distance measurement, Meter is generally used for smaller length measurement.

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

1 Mile Per Hour = 0.44704 Meter Per Second

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

Meters Per Second = Miles Per Hour * 0.44704

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

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

Approach:

  • Declare a double variable say ‘milesPerHour‘ and initialize it’s value.
  • Then convert miles 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 miles per hour
        double milesPerHour=1;
        //converting miles per hour to meters per second
        double metersPerSecond=milesPerHour*0.44704;
        //printing result
        System.out.println(milesPerHour+" mile per hour is equal to "+metersPerSecond+" meters per second");
    }
}
Output:

1.0 mile per hour is equal to 0.44704 meters per second

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

Approach:

  • Take the value of double variable ‘milesPerHour‘ as take its’ value user input.
  • Then convert miles 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 miles per hour: ");
        //declared double variable milesPerHour
        //and taking it's value as user input
        double milesPerHour=sc.nextDouble();
        //converting miles per hour to meters per second
        double metersPerSecond=milesPerHour*0.44704;
        //printing result
        System.out.println(milesPerHour+" miles per hour is equal to "+metersPerSecond+" meters per second");
    }
}
Output:

Enter the value of miles per hour: 5
5.0 miles per hour is equal to 2.2352 meters per second

Method-3: Java Program to Convert Miles Per Hour to Meter Per Second By Using User Defined Method

Approach:

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

Enter the value of miles per hour: 8.9
8.9 miles per hour is equal to 3.978656 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: