{"id":1837,"date":"2018-01-05T07:28:32","date_gmt":"2018-01-05T07:28:32","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=1837"},"modified":"2022-03-12T14:14:35","modified_gmt":"2022-03-12T14:14:35","slug":"write-a-java-program-to-convert-liters-to-gallons","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-convert-liters-to-gallons\/","title":{"rendered":"Java program to convert liters to gallons."},"content":{"rendered":"<h3>Problem Statement<\/h3>\n<p>Write a Java program to convert liters to gallons. Given that one liter is equal to 0.264 gallons.\u00a0 The mathematical conversion equation to convert is as follows:<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/latex.codecogs.com\/gif.latex?\\huge&amp;space;1&amp;space;liter&amp;space;=&amp;space;0.264&amp;space;gallon\" alt=\"\\huge 1 liter = 0.264 gallon\" align=\"absmiddle\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<h3>IPO(Input, Processing, Output )<\/h3>\n<p>IPO chart for the program is shown below:<\/p>\n<p>Input for the program: amount in liters.<\/p>\n<p>Processing<\/p>\n<p>gallons = liters * 0.264<\/p>\n<p>Round the output to 3 decimals.<\/p>\n<p>Output: Print the gallons.<\/p>\n<h2>Tools Used<\/h2>\n<p>We will use the below tools to develop the program.<\/p>\n<ul>\n<li>JDK ( Java Development Kit)<\/li>\n<li>Eclipse IDE( Integrated Development Environment)<\/li>\n<\/ul>\n<h2>Eclipse IDE Setup<\/h2>\n<p>Some steps and instructions to create a Java application on Eclipse IDE:<\/p>\n<ul>\n<li>Create Java Project in Eclipse<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/<\/a><\/p>\n<ul>\n<li>Create Java Package in Eclipse<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/\">https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/<\/a><\/p>\n<ul>\n<li>Create Java Class in Eclipse<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/\">https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/<\/a><\/p>\n<ul>\n<li>Run Java Project in Eclipse<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/<\/a><\/p>\n<h2>Java program<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * Java program to convert liters to gallons\r\n *\/\r\npackage questions;\r\n\r\nimport java.util.Scanner;\r\n\r\n\/**\r\n * @author \r\n *\r\n *\/\r\npublic class LitersToGallonsJavaProgram {\r\n\r\n  \/**\r\n   * @param args\r\n   *\/\r\n  public static void main(String[] args) {\r\n    \/\/ variables \r\n    double liters;  \/\/ liters\r\n    double gallons; \/\/ gallons \r\n    char choice='y';\r\n    Scanner keyboard = new Scanner( System.in );\r\n    while(choice=='y' || choice== 'Y') {\r\n      System.out.print( \"Enter liters to convert to gallons:= \" ); \r\n      liters = keyboard.nextDouble( ); \r\n      \/\/ 1 Liter = 0.264 Gallons.\r\n      gallons = 0.264 * liters;  \r\n      System.out.print( liters + \" Liters = \" );\r\n\r\n      System.out.println( (double)Math.round(gallons * 1000d) \/ 1000d +\r\n \" gallons.\" );\r\n      System.out.print( \"Do you want to convert \r\nanother amount? [y\/n]:= \" ); \r\n      choice = keyboard.next().charAt(0); \r\n    }\r\n    keyboard.close();\r\n  } \/\/ end main\r\n} \/\/end class\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Screenshot<\/h2>\n<p>Screenshot of the program code in the IDE window.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1840 size-full\" title=\"convert liters to gallons\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Liters-To-Gallons.png\" alt=\"convert liters to gallons\" width=\"1741\" height=\"947\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Liters-To-Gallons.png 1741w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Liters-To-Gallons-300x163.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Liters-To-Gallons-1024x557.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Liters-To-Gallons-768x418.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Liters-To-Gallons-1536x835.png 1536w\" sizes=\"auto, (max-width: 1741px) 100vw, 1741px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>Output<\/h2>\n<p>Save the program code and execute test cases to verify the program output.<\/p>\n<p>Enter liters to convert to gallons:= 1<br \/>\n1.0 Liters = 0.264 gallons.<br \/>\nDo you want to convert another amount? [y\/n]:= y<br \/>\nEnter liters to convert to gallons:= 3.785<br \/>\n3.785 Liters = 0.999 gallons.<br \/>\nDo you want to convert another amount? [y\/n]:= n<\/p>\n<p>&nbsp;<\/p>\n<p>The program will loop and continue to compute the conversion while the user enters either y or Y as specified in the loop condition.<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;<\/p>\n<p>Java Tutorial on this website:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1837\" data-postid=\"1837\" class=\"themify_builder_content themify_builder_content-1837 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement Write a Java program to convert liters to gallons. Given that one liter is equal to 0.264 gallons.\u00a0 The mathematical conversion equation to convert is as follows: &nbsp; &nbsp; IPO(Input, Processing, Output ) IPO chart for the program is shown below: Input for the program: amount in liters. Processing gallons = liters * [&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":[],"class_list":["post-1837","post","type-post","status-publish","format-standard","hentry","category-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\/1837","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=1837"}],"version-history":[{"count":8,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1837\/revisions"}],"predecessor-version":[{"id":21753,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1837\/revisions\/21753"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}