{"id":1288,"date":"2016-08-16T06:18:49","date_gmt":"2016-08-16T06:18:49","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1288"},"modified":"2024-12-14T03:28:49","modified_gmt":"2024-12-14T03:28:49","slug":"write-a-program-to-convert-distance-in-kilometers-to-miles-and-vice-versa","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-program-to-convert-distance-in-kilometers-to-miles-and-vice-versa\/","title":{"rendered":"Java Program to convert distance in kilometers to miles?"},"content":{"rendered":"<h1>Java Program to convert distance in kilometers to miles?<\/h1>\n<p>In this tutorial, we will learn to develop a Java program to convert distance from kilometers to miles. We will make use of the formulas below for conversion. The mathematical formula for converting distance from kilometers to miles:<\/p>\n<p><strong>Miles = 0.62 * ( Distance in kilometers)<\/strong><\/p>\n<p>The mathematical formula for converting distance from miles to kilometers:<\/p>\n<p><strong>Kilometers = 1.60 * (Distance in miles )<\/strong><\/p>\n<h3>Tools used<\/h3>\n<p>Tools used to develop the program are as follows:<\/p>\n<ul>\n<li>JDK<\/li>\n<li>IntelliJ IDE<\/li>\n<\/ul>\n<h2><strong>Convert Distance Java Program<\/strong><\/h2>\n<p>The program allows users to input the two conversion options. The program checks for the options 1 and 2. It stops gracefully for any other input options.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.Scanner;\r\n\r\npublic class DistanceConverter {\r\n    public static void main(String args[]) {\r\n        Scanner input = null;\r\n        double km,mi,distance;\r\n        int option;\r\n        try {\r\n            input = new Scanner(System.in);\r\n            System.out.println(\"Choose an Option:\");\r\n            System.out.println(\"Convert Kilometers to Miles(1)\");\r\n            System.out.println(\"Convert Miles to Kilometers(2)\");\r\n            option = Integer.parseInt(input.nextLine());\r\n            if (option != 1 &amp;&amp; option != 2)\r\n            {\r\n                System.out.println(\"Plz choose option 1 or 2\");\r\n                System.exit(0);\r\n            }\r\n            if(option == 1) {\r\n                System.out.println(\"Enter Distance in Kilometers\");\r\n                distance = Double.parseDouble(input.nextLine());\r\n                mi = 0.62 * distance;\r\n                System.out.println(\"Distance in Miles is:\" + mi );\r\n\r\n            }\r\n            else if(option == 2) {\r\n                System.out.println(\"Enter Distance in Miles\");\r\n                distance = Double.parseDouble(input.nextLine());\r\n                km = 1.60 * distance;\r\n                System.out.println(\"Distance in Kilometers is:\" + km );\r\n            }\r\n        }\r\n        catch(Exception e)\r\n        {\r\n            System.out.print(\"Error.Program Terminated\");\r\n        }\r\n        finally\r\n        {\r\n            if (input != null) {\r\n                input.close();\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Output of the program<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1202\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Distance-converter-java-program.png\" alt=\"Distance converter java program\" width=\"1116\" height=\"681\" title=\"\"><\/p>\n<h2>Test Cases<\/h2>\n<p>Execute some test cases to verify the program output. We will execute two test cases on the program.<\/p>\n<p>Test Case 1-&gt; To verify the conversion from Kilometers to Miles option.<\/p>\n<p>Test Case 2-&gt; To verify the conversion from Miles to Kilometers option.<\/p>\n<p>Test Case 3 -&gt; This is an invalid test case. To verify how the program responds for invalid user input.<\/p>\n<h3>Test Case #1<\/h3>\n<p>Choose an Option:<br \/>\nConvert Kilometers to Miles(1)<br \/>\nConvert Miles to Kilometers(2)<br \/>\n1<br \/>\nEnter Distance in Kilometers<br \/>\n10<br \/>\nDistance in Miles is:6.2<\/p>\n<h3>Test Case #2<\/h3>\n<p>Choose an Option:<br \/>\nConvert Kilometers to Miles(1)<br \/>\nConvert Miles to Kilometers(2)<br \/>\n2<br \/>\nEnter Distance in Miles<br \/>\n10<br \/>\nDistance in Kilometers is:16.0<\/p>\n<h3>Test Case #3<\/h3>\n<p>Choose an Option:<br \/>\nConvert Kilometers to Miles(1)<br \/>\nConvert Miles to Kilometers(2)<br \/>\n3<br \/>\nPlz choose option 1 or 2<\/p>\n<p>&nbsp;<\/p>\n<p>Java Tutorial on this website:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java Program to convert distance in kilometers to miles? In this tutorial, we will learn to develop a Java program to convert distance from kilometers to miles. We will make use of the formulas below for conversion. The mathematical formula for converting distance from kilometers to miles: Miles = 0.62 * ( Distance in kilometers) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[24],"class_list":["post-1288","post","type-post","status-publish","format-standard","hentry","category-java-programs","tag-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1288"}],"version-history":[{"count":12,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1288\/revisions"}],"predecessor-version":[{"id":26324,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1288\/revisions\/26324"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}