Fahrenheit to Celcius conversion application using JAVA

Published on . Written by

Fahrenheit to Celcius conversion application using JAVA

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.


Skyfi Labs Projects
In this JAVA project, we will develop an application to convert Fahrenheit into Celcius with the help of JAVA programming.

Read more..

SLNOTE
Project implementation

Following are the functions used:

  • toCelsius - which converts the temperature into celsius
  • toFahrenheit - which converts the temperature into Fahrenheit
  • main (String[] args) - a simple test method which is our main function
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 double

            c = (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

 }


SLLATEST
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 value

f = (c * 1.8) + 32.0; this the formula to convert Celsius to Fahrenheit

return f; # here we are returning the Fahrenheit value

Now, 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.2

fTemp = 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.


SLDYK
Kit required to develop Fahrenheit to Celcius conversion application using JAVA:
Technologies you will learn by working on Fahrenheit to Celcius conversion application using JAVA:


Any Questions?


Subscribe for more project ideas