Java: Calculate speed in meters per second, kilometers and miles per hour
Calculate Speeds (m/s, km/h, mph)
Write a Java program to take the user for a distance (in meters) and the time taken (as three numbers: hours, minutes, seconds), and display the speed, in meters per second, kilometers per hour and miles per hour (hint: 1 mile = 1609 meters).
Test Data
Input distance in meters: 2500
Input hour: 5
Input minutes: 56
Input seconds: 23
Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
float timeSeconds;
float mps,kph, mph;
System.out.print("Input distance in meters: ");
float distance = scanner.nextFloat();
System.out.print("Input hour: ");
float hr = scanner.nextFloat();
System.out.print("Input minutes: ");
float min = scanner.nextFloat();
System.out.print("Input seconds: ");
float sec = scanner.nextFloat();
timeSeconds = (hr*3600) + (min*60) + sec;
mps = distance / timeSeconds;
kph = ( distance/1000.0f ) / ( timeSeconds/3600.0f );
mph = kph / 1.609f;
System.out.println("Your speed in meters/second is "+mps);
System.out.println("Your speed in km/h is "+kph);
System.out.println("Your speed in miles/h is "+mph);
scanner.close();
}
}
Sample Output:
Input distance in meters: 2500 Input hour: 5 Input minutes: 56 Input seconds: 23 Your speed in meters/second is 0.11691531 Your speed in km/h is 0.42089513 Your speed in miles/h is 0.26158804
Flowchart:
Java Code Editor :
Improve this sample solution and post your code through Disqus
Previous: Write a Java program to compute body mass index (BMI).
Next: Write a Java program that reads a number and display the square, cube, and fourth power.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics