// DistanceConv.java // Java program that, given a distance in metres outputs that distance in kilometres // rounded to the nearest whole kilometre. // For example, given 1100m the output will be 1km, and given 1900m the output will be 2km. import java.io.*; // java Input/Output package public class DistanceConv // declares class DistanceConv { static double kilo; static double meter; public static void main( String[] args) // begin main method { InputStreamReader stdin = new InputStreamReader(System.in); // class reads characters BufferedReader console = new BufferedReader(stdin); // class buffereing character streams String s; // input distance System.out.println("Please Enter the distance in meters"); try // prompt and input { s = console.readLine(); // reading a string meter = Double.parseDouble(s); // parsing a double value from the string } catch(IOException ioex) // exeption handling { System.out.println("Input error"); // error output if invalid data System.exit(1); // program terminates } //conversion kilo = converToKilo(meter); //calling the function to do the job //output System.out.println(meter + " is equivalent to " + kilo + " kilometers"); } // end method main private static double converToKilo(double meter) // method convert { double temp; temp = meter/1000; // every 1000 meters make a kilometer return Math.round(temp); // Math.round rounds the decimal number to the nearest integer } // end method convert } // end class DistanceConv