Java program to Convert Fahrenheit to Celsius
Overview
In this tutorial, we will develop a java program to convert Fahrenheit to Celsius temperature. The mathematical formula to converting Fahrenheit temperature to Celsius temperature is below:
Celsius = 5 * (Fahrenheit – 32) /9
IPO chart
Input | Process | Output |
Temperature in Fahrenheit | Celsius = 5.0 * (fahrenheit – 32.0) / 9.0; | Temperature in Celsius |
Example
Let’s calculate and evaluate one example calculation.
C = (5/9)*(F – 32)
where:
F is the Fahrenheit Temperature
C is the Celsius Temperature.
Input
Let F = 77 degrees Fahrenheit
Let’s calculate the equivalent Celsius temperature.
The input is 77.
C = (5/9)*(77 – 32)
C = (5/9)*(45)
C = 25
Output
The equivalent Celsius temperature is 25 degrees.
Eclipse IDE Setup
Some steps and instructions to create a Java application on Eclipse IDE:
- Create Java Project in Eclipse
https://www.testingdocs.com/create-a-new-java-project-in-eclipse/
- Create Java Package in Eclipse
https://www.testingdocs.com/create-java-package-in-eclipse-ide/
- Create Java Class in Eclipse
https://www.testingdocs.com/create-a-new-java-class-in-a-project/
- Run Java Project in Eclipse
https://www.testingdocs.com/run-java-project-in-eclipse/
Java Program
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.
public class FahrenheitToCelsius { public static void main(String args[]) throws IOException { BufferedReader input = null; double fahrenheit,celsius; try { input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Please enter temperature in Fahrenheit : "); fahrenheit = Double.parseDouble(input.readLine()); celsius = 5.0 * (fahrenheit - 32.0) / 9.0; System.out.println("The temperature in Fahrenheit provided:" + fahrenheit + " F"); System.out.println("The temperature in Celsius is:" + celsius + " C"); } catch(IOException ioe) { System.out.print("Unexpected IO error."); } catch(NumberFormatException nfe) { System.out.print("Please check for valid input."); } catch(Exception e) { System.out.print("Unexpected error."); } finally {
if (input != null) { input.close(); }
} } }
Sample Output
Now run the program with sample input let’s say 32F. Calculate the output according the formula by hand.
Celsius = 5 * (Fahrenheit – 32) /9
= 5 * ( 32- 32) / 9
= 5 * 0
=0 C
Test Cases
Let’s test the program with sample test cases to check if the program is working as intended.
Test Case Scenario | Detailed Steps | Test Data | Expected Result | Actual Result |
Happy path. Temperature in Integers | Run the Java program. Enter Fahrenheit temperature |
32 | 0 | |
Temperature in Real | Run the Java program. Enter Fahrenheit temperature |
98.4 | 36.89 | |
Negative Test Case | Run the Java program. Enter Fahrenheit temperature |
Invalid data : dsafg | Program Error Message |
Test Case: Enter Real numbers
Please enter temperature in Fahrenheit : 98.4
The temperature in Fahrenheit provided:98.4 F
The temperature in Celsius is:36.888888888888886 C
To round the output:
https://www.testingdocs.com/questions/how-to-round-a-double-value-in-java/
Exercise
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.
More information on Java can be found on the official website: