Answer:
The solution codes are written in Java as follows:
- import java.util.Scanner;
- public class ConvertMilesToKilometers {
- public static double convert_to_km(double miles)
- {
- double km = miles * 1.60934;
- return km;
- }
- public static void main(String[] args) {
- System.out.print("Enter miles: ");
- Scanner input = new Scanner(System.in);
- double inputMiles = input.nextDouble();
- System.out.println(inputMiles + " mile(s) = " + convert_to_km(inputMiles) + " km");
- }
- }
Step-by-step explanation:
Line 1 (Import Java Scanner Class)
- Since the application is expected to get an input miles from user, the Java Scanner class is needed.
- The Java Scanner class will be required to create a Scanner object to get input from user (Line 15).
Line 5 -10 (Create a method to convert mile to km)
- This method accept one input (miles) and convert it to km.
- km is returned as the output.
Line 14-16 (Get user input for miles )
- A Scanner object is created in Line 15.
- To get user input for miles, the nextDouble() method of the Scanner object is used (Line 16).
- The nextDouble() is a method dedicated to get a decimal number as input (since miles is often in decimal format rather than an integer).
Line 18 (call method to convert miles to km and display the output)
- Call convert_to_km() method by passing the user input miles (inputMiles) as argument.
- The method will convert the miles to km and return output to the main method and display it in terminal using println() method.