# Java Notes

# Kilometer to Miles - IO Class

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
    // KmToMilesMethods.java -- Converts kilometers to miles.
    // Michael Maus, 9 Sept 2004
    //
    // Note: Move input methods to their own class.

    import javax.swing.*;
    import java.text.*;

    public class KmToMilesInputClass {

        public static void main(String[] args) {
            //... Constants
            final double MILES_PER_KILOMETER = 0.621;
            final DecimalFormat ONE_DECIMAL_PLACE = new DecimalFormat("0.0");

            //... Variables
            double kilometers;  // Number of kilometers.
            double miles;       // Number of miles.
            IOHandler io;

            //... Initialization
            io = new IOHandler();

            //... Input
            kilometers = io.getDouble("Enter number of kilometers.");

            //... Computation
            miles = kilometers * MILES_PER_KILOMETER;

            //... Output
            io.putString(kilometers + " kilometers is "
                       + ONE_DECIMAL_PLACE.format(miles) + " miles.");

            //... Stop
            System.exit(0);  // Stop GUI program
        }
    }

    class IOHandler {
        public double getDouble(String prompt) {
            String tempStr;
            tempStr = JOptionPane.showInputDialog(null, prompt);
            return Double.parseDouble(tempStr);
        }

        public void putString(String output) {
            JOptionPane.showMessageDialog(null, output);
        }
    }