JAVA is one of the famous programming languages of all time and also it is the most effective programming language. Following are some of the advantages of JAVA: simple and user-friendly, object-oriented, and it can run on any machine without any special software.
In this JAVA project, we will develop an application to convert Fahrenheit into Celcius with the help of JAVA programming.
JAVA Kit will be shipped to you and you can learn and build using tutorials. You can start for free today!
Project implementation
Following are the functions used:
Always we need to declare the class first in java, type the following to do the same:
public class Temperature {
Also, save the file name as temperature.java which is exactly the same as the class name.
First, we will look into the toCelsius function.
public double toCelcius (double f) {
# In this line we returned a double and we also accept a double value which is “toFahrenheit”
double c;
#Now we are declaring doublec = (f - 32.0)/ 1.8;
#This is the standard formula to convert Fahrenheit to celsius.return c;
#Here we are returning the calculated centigrade value}
Want to develop practical skills on JAVA? Checkout our latest projects and start learning for free
Now we will look into the toFahrenheit function.
public double toFahrenheit (double c)
#In the above line, we are returning a double and we also accept a double which is the Celsius value
double f;
#declare a double value for the Fahrenheit valuef = (c * 1.8) + 32.0;
# this the formula to convert Celsius to Fahrenheitreturn f;
# here we are returning the Fahrenheit valueNow, we will look into the main function.
public static void main(String[] args) {
Temperature temperature = new Temperature (); #instantiate temperature object
double fTemp = 77.5; #declare fTemp, which is a Fahrenheit temperature variable and the value is set to 77.5.
double cTemp = temperature.toCelsius(fTemp); #here we are calculating the cTemp by calling our Celsius method and the fTemp value is passed in this argument.
system.out.println(fTemp + “Fahrenheit = “ +cTemp + “Celsius.”) #As we got the both values now print the fTemp and cTemp as results with the help of print statement.
Now, the same thing is reversed,
cTemp = 37.2;
#C value is set to 37.2fTemp = temperature.toFahrenheit(cTemp);
#Here we calculate the fTemp using the two Fahrenheit function by passing in cTemp as an argument.system.out.pringln(cTemp + “Celcius = “+ fTemp +”Fahrenheig.”)
#This prints both the values.}
If you run the program it will print more decimal points. You can solve that by typing “%.2f” in the print statement, which trims all the decimal values to two decimal places.
In %.2f, f-stands for floating-point and %- is a variable.
Refer below for the entire code:
public class Temperature {
public static void main(String[] args) {
Temperature temperature = new Temperature();
double fTemp = 77.5;
double cTemp = temperature.toCelsius(fTemp);
system.out.printf(“%2.f Fahrenheit = %.2f Celsius. \n”, fTemp, cTemp)
cTemp = 37.2;
fTemp = temperature.toFahrenheit(cTemp);
system.out.printf(“%.2f Celsius = %.2f Fahrenheit.\n”, cTemp, fTemp)
}
public double toCelsius(double f) {
return (f - 32.0) / 1.8;
}
public double toFahrenheit(double c) {
return (c *1.8) + 32.0;
}
}
Run the program and test it.
Skyfi Labs helps students learn practical skills by building real-world projects.
You can enrol with friends and receive kits at your doorstep
You can learn from experts, build working projects, showcase skills to the world and grab the best jobs.
Get started today!
Join 250,000+ students from 36+ countries & develop practical skills by building projects
Get kits shipped in 24 hours. Build using online tutorials.
Stay up-to-date and build projects on latest technologies