Kilometer to Miles Converter in Java GUI

In this tutorial, i will teach you how to create a program for converting kilometer to miles in java with GUI. It is necessary for java beginners to undergo and learn this simple program that i have made. Now, let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of kmToMiles.java. 2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the kilometer.
  1. import javax.swing.*;
  2.  
  3. </java
  4. 3. Declare your variables.
  5. <java>
  6. String kmStr;
  7. double km;
  8. double mi;
We will use kmStr as String as an inputbox, variable km for kilometer, and mi for miles. 4. Use variable kmStr as an InputDialogBox for inputting kilometer. And convert the input to double (hence we will input only number here) with the use of variable km.
  1. kmStr = JOptionPane.showInputDialog(null, "Enter kilometers.");
  2. km = Double.parseDouble(kmStr);
5. Create the code for formula. We all know that 1km is equivalent to 0.621 miles. So, we will use this formula and pass the value to mi variable.
  1. mi = km * 0.621;
6. After computing, display the equivalent value of kilometers to miles using JOptionPane.showMessageDialog.
  1. JOptionPane.showMessageDialog(null, km + " kilometers is "
  2. + mi + " miles.");
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*;
  2.  
  3. public class kmToMiles {
  4. public static void main(String[] args) {
  5.  
  6. String kmStr;
  7. double km;
  8. double mi;
  9.  
  10. //... Input
  11. kmStr = JOptionPane.showInputDialog(null, "Enter kilometers.");
  12. km = Double.parseDouble(kmStr);
  13.  
  14. //... Computation
  15. mi = km * 0.621;
  16.  
  17. //... Output
  18. JOptionPane.showMessageDialog(null, km + " kilometers is "
  19. + mi + " miles.");
  20. }
  21.  
  22. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment