Java Software Solutions > PP2.5 > Solution

Problem:

Write an application that converts miles to kilometers. (One mile
equals 1.60935 kilometers.) Read the miles value from the user
as a floating point value.

Output:

Enter the distance in miles: 423
That distance in kilometers is: 680.75505

Solution:

import java.util.Scanner;

public class MilesToKilometers
{
   //-----------------------------------------------------------------
   //  Converts miles into kilometers. The value for miles is read
   //  from the user.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      final double MILES_PER_KILOMETER = 1.60935;

      double miles, kilometers;

   Scanner scan = new Scanner(System.in);

      System.out.print ("Enter the distance in miles: ");
      miles = scan.nextDouble();

      kilometers = MILES_PER_KILOMETER * miles;

      System.out.println ("That distance in kilometers is: " +
                          kilometers);
   }
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact