Create a program Java to ask the user for a distance in meters and the time taken (hours, minutes, seconds).
Display the speed, in meters per second, kilometers per hour and miles per hour.
1 mile = 1609 meters.
1200 0 13 45
Speed in meters/sec is 1.4545455 Speed in km/h is 5.236364 Speed in miles/h is 3.2544215
import java.io.*; public class SpeedUnits { public static void main(String[] args) throws IOException { BufferedReader obj = new BufferedReader(new InputStreamReader(System.in)); float distance = Float.parseFloat(obj.readLine()); float hour = Float.parseFloat(obj.readLine()); float min = Float.parseFloat(obj.readLine()); float sec = Float.parseFloat(obj.readLine()); float timeBySeconds = (hour * 3600) + (min * 60) + sec; float mps = distance / timeBySeconds; float kph = (distance / 1000.0f) / (timeBySeconds / 3600.0f); float mph = kph / 1.609f; System.out.println("Speed in meters/sec is " + mps); System.out.println("Speed in km/h is " + kph); System.out.println("Speed in miles/h is " + mph); } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Variables are an essential component in any programming language, and Java is no exception. In this post, we will explore the concept of variables in Java and how they are used in programming.
Data types are a fundamental concept in programming and play a crucial role in Java, a strongly typed programming language. In this section, we will explore the basic concepts of data types in Java and their importance in application development.
Primitive data types in Java are fundamental elements that represent simple values and are used to store basic data.
In Java, wrapper classes are a set of classes that encapsulate primitive data types.
Write a program to ask the user for three letters and display them in reverse order.
Write a Java program that requests the width (x) and height (y) of a rectangle and calculate the perimeter, area and diagonal.
Create a Java program that prompts the user for 2 integers and displays a random number on the screen between those values.
Practice with easy and intermediate Java exercises on different types of text, numeric or boolean data.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.