{"id":1351,"date":"2019-08-16T10:10:34","date_gmt":"2019-08-16T10:10:34","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1351"},"modified":"2024-12-07T14:58:59","modified_gmt":"2024-12-07T14:58:59","slug":"write-a-program-to-convert-fahrenheit-to-celsius-temperature-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-program-to-convert-fahrenheit-to-celsius-temperature-in-java\/","title":{"rendered":"Java program to Convert Fahrenheit to Celsius [2024 ]"},"content":{"rendered":"<h1>Java Program to Convert Fahrenheit to Celsius<\/h1>\n<p>In this tutorial, we will develop a Java program to convert Fahrenheit to Celsius temperature. The mathematical formula for converting Fahrenheit temperature to Celsius temperature is below:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Celsius = 5 * (Fahrenheit \u2013 32) \/9<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-17767\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/FahrenheitToCelsius.png\" alt=\"Fahrenheit To Celsius\" width=\"1535\" height=\"612\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/FahrenheitToCelsius.png 1535w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/FahrenheitToCelsius-300x120.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/FahrenheitToCelsius-1024x408.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/FahrenheitToCelsius-768x306.png 768w\" sizes=\"auto, (max-width: 1535px) 100vw, 1535px\" \/><\/p>\n<h3><\/h3>\n<h2>IPO chart<\/h2>\n<table border=\"1\" width=\"539\" cellspacing=\"1\" cellpadding=\"1\">\n<tbody>\n<tr>\n<td style=\"background-color: #efefef;\"><strong>Input<\/strong><\/td>\n<td style=\"background-color: #efefef;\"><strong>Process <\/strong><\/td>\n<td style=\"background-color: #efefef;\"><strong>Output <\/strong><\/td>\n<\/tr>\n<tr>\n<td>Temperature in Fahrenheit<\/td>\n<td>Celsius = 5.0 * (Fahrenheit &#8211; 32.0) \/ 9.0;<\/td>\n<td>Temperature in Celsius<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Example<\/h2>\n<p>Let&#8217;s calculate and evaluate one example calculation.<\/p>\n<p><strong>C = (5\/9)*(F &#8211; 32)<\/strong><\/p>\n<p>where:<br \/>\nF is the Fahrenheit Temperature<br \/>\nC is the Celsius Temperature.<\/p>\n<p><strong>Input<\/strong><\/p>\n<p>Let F = 77 degrees Fahrenheit<\/p>\n<p>Let&#8217;s calculate the equivalent Celsius temperature.<\/p>\n<p>The input is 77.<\/p>\n<p>C = (5\/9)*(77 &#8211; 32)<\/p>\n<p>C = (5\/9)*(45)<\/p>\n<p>C = 25<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>The equivalent Celsius temperature is 25 degrees.<\/p>\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><strong>Java Program<\/strong><\/h2>\n<p>The following program takes the input of Fahrenheit temperature from the user. It calculates the Celsius equivalent of the Fahrenheit temperature. Displays the Celsius temperature in the output.<\/p>\n<p>&nbsp;<\/p>\n<pre>public class FahrenheitToCelsius {\r\n\r\n    public static void main(String args[]) throws IOException {\r\n        BufferedReader input = null;\r\n        double fahrenheit,celsius;\r\n        try {\r\n            input = new BufferedReader(new InputStreamReader(System.in));\r\n            System.out.print(\"Please enter temperature in Fahrenheit : \");\r\n            fahrenheit = Double.parseDouble(input.readLine());\r\n\r\n            celsius = 5.0 * (fahrenheit - 32.0) \/ 9.0;\r\n            System.out.println(\"The temperature in Fahrenheit provided:\" \r\n+ fahrenheit + \" F\");\r\n            System.out.println(\"The temperature in Celsius is:\" \r\n+ celsius + \" C\");\r\n        }\r\n        catch(IOException ioe)\r\n        {\r\n            System.out.print(\"Unexpected IO error.\");\r\n        }\r\n        catch(NumberFormatException nfe)\r\n        {\r\n            System.out.print(\"Please check for valid input.\");\r\n        }\r\n        catch(Exception e)\r\n        {\r\n            System.out.print(\"Unexpected error.\");\r\n        }\r\n        finally\r\n        {<\/pre>\n<pre>          if (input != null) {\r\n          input.close();\r\n           }<\/pre>\n<pre>} } }<\/pre>\n<h2><strong>Sample Output<br \/>\n<\/strong><\/h2>\n<p>Now run the program with sample input let\u2019s say 32F. Calculate the output according the formula by hand.<\/p>\n<p>Celsius = 5 * (Fahrenheit \u2013 32) \/9<\/p>\n<p>= 5 * ( 32- 32) \/ 9<\/p>\n<p>= 5 * 0<\/p>\n<p>=0 C<\/p>\n<h3>Test Cases<\/h3>\n<p>Let&#8217;s test the program with sample test cases to check if the program is working as intended.<\/p>\n<p>&nbsp;<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n<tbody>\n<tr>\n<td style=\"background-color: #efefef;\"><strong>Test Case Scenario<\/strong><\/td>\n<td style=\"background-color: #efefef;\"><strong>Detailed Steps<\/strong><\/td>\n<td style=\"background-color: #efefef;\"><strong>Test Data<\/strong><\/td>\n<td style=\"background-color: #efefef;\"><strong>Expected Result<\/strong><\/td>\n<td style=\"background-color: #efefef;\"><strong>Actual Result<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Happy path. Temperature in Integers<\/td>\n<td>Run the Java program.<br \/>\nEnter Fahrenheit temperature<\/td>\n<td>32<\/td>\n<td>0<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Temperature in Real<\/td>\n<td>Run the Java program.<br \/>\nEnter Fahrenheit temperature<\/td>\n<td>98.4<\/td>\n<td>36.89<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Negative Test Case<\/td>\n<td>Run the Java program.<br \/>\nEnter Fahrenheit temperature<\/td>\n<td>Invalid data : dsafg<\/td>\n<td>Program Error Message<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Test Case: Enter Real numbers<\/p>\n<p>Please enter temperature in Fahrenheit : 98.4<br \/>\nThe temperature in Fahrenheit provided:98.4 F<br \/>\nThe temperature in Celsius is:36.888888888888886 C<\/p>\n<p>To round the output:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/questions\/how-to-round-a-double-value-in-java\/\"><strong>https:\/\/www.testingdocs.com\/questions\/how-to-round-a-double-value-in-java\/<\/strong><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-17755\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JavaProgramConvertFahrenheitCelsius.png\" alt=\"JavaProgramConvert Fahrenheit Celsius\" width=\"1651\" height=\"899\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JavaProgramConvertFahrenheitCelsius.png 1651w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JavaProgramConvertFahrenheitCelsius-300x163.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JavaProgramConvertFahrenheitCelsius-1024x558.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JavaProgramConvertFahrenheitCelsius-768x418.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/JavaProgramConvertFahrenheitCelsius-1536x836.png 1536w\" sizes=\"auto, (max-width: 1651px) 100vw, 1651px\" \/><\/h3>\n<h2><strong>Exercise<\/strong><\/h2>\n<p>Now that you know to convert Fahrenheit temperature to Celsius. Write a similar program to convert Celsius to Fahrenheit temperature. Your program should take input the Celsius temperature value and convert the temperature into Fahrenheit and display the output.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java Program to Convert Fahrenheit to Celsius : In this tutorial, we will develop a Java program to convert Fahrenheit to Celsius temperature. The mathematical formula<\/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-1351","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\/1351","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=1351"}],"version-history":[{"count":18,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1351\/revisions"}],"predecessor-version":[{"id":26285,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1351\/revisions\/26285"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}