Java Program to Convert Meters Per Second to Miles Per Hour

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

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

Java Program to Convert Meters Per Second to Miles Per Hour

Meter is used as a unit of length measurement while length is small and Mile is used as a unit of length which is used for long distance measurement.

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

1 Meter Per Second = 2.23694 Mile Per Hour

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

Mile Per Hour = Meters Per Second * 2.23694

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

Method-1: Java Program to Convert Meters Per Second to Miles 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 miles 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 meters per second to miles per hour
        double milePerHour=meterPerSecond*2.23694;
        //printing result
        System.out.println(meterPerSecond+" meter per second is equal to "+milePerHour+" miles per hour");
    }
}
Output:

1.0 meter per second is equal to 2.23694 miles per hour

Method-2: Java Program to Convert Meters Per Second to Miles 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 miles 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 meters per second to miles per hour
        double milePerHour=meterPerSecond*2.23694;
        //printing result
        System.out.println(meterPerSecond+" meter per second is equal to "+milePerHour+" miles per hour");
    }
}
Output:

Enter the value of meter per second: 4
4.0 meter per second is equal to 8.94776 miles per hour

Method-3: Java Program to Convert Meters Per Second to Miles 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 miles 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 meters per second to miles per hour
        double milePerHour=meterPerSecond*2.23694;
        //printing result
        System.out.println(meterPerSecond+" meter per second is equal to "+milePerHour+" miles per hour");
    }
}
Output:

Enter the value of meter per second: 5.5
5.5 meter per second is equal to 12.3031 miles 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: