How to Convert Celsius to Fahrenheit and Vice Versa in Java

The Java standard library doesn't include a ready-made class for performing common conversions, such as switching between Fahrenheit and Celsius. However, that shouldn't stop you from building one of your own classes, and because Java is an object-oriented language, if you take the care to place it in your own class library, you'll never need to worry about this problem again.

Things You'll Need

  • Java SDK
  • Text editor or Java IDE
Show More

Instructions

    • 1

      Create a TempConverter.java file. You can do this in any text editor that allows you to save files with arbitrary extensions; however, you'll soon learn to love the extra features that a dedicated Java IDE such as Eclipse or NetBeans offers.

    • 2

      Write a skeleton of your class with javadoc comments. It should look like this:

      /**
      * A temperature converter
      * @author Amber Rollins
      */
      class TempConverter {
      // The .0 is very important! It forces Java to use floating point division instead of integer division!
      public static final double FtC_RATIO = 5.0/9.0;
      public static final double CtF_RATIO = 9.0/5.0;

      /**
      * Convert a Fahrenheit value to Celsius.
      * @param f the temperature in Fahrenheit
      * @return temperature in Celsius
      */
      public static double convertToCelcius(double f);

      /**
      * Convert a Celsius value to Fahrenheit.
      * @param c the temperature in Celsius
      * @return the temperature in Fahrenheit
      */
      public static double convertToFahrenheit(double c);

      public static void main(String[] args);
      }

      While Javadoc comments are not required by the language, it's a good habit to always write Javadoc comments with your code. It makes it easier to understand for others and even for yourself if you need to come back and fix a bug later.

    • 3

      Write your convertToCelsius method. This method has been declared public, meaning it is available for use by outside classes. It's been declared static so that it can be run without an instantiation if the TempConverter class. And it returns a double in order to give the user a generous level of accuracy to work with. If absolutely perfect accuracy is critical, then it might be better to replace the use of double here with a BigDecimal class in order to avoid some small inaccuracies that can occur with doubles, but that is overkill for most applications.

      The method will simply look like this:

      return (f - 32) * FtC_RATIO;

    • 4

      Write your convertToFahrenheit method. Just like the last method, this one is public and static and returns a double. The code will look very similar to the other method in the class:

      return (c * CtF_RATIO) + 32;

    • 5

      Write a test main method that tests your code. Try the following:

      double temperature = 98;
      System.out.println("The temperature is " + temperature + " degrees F.");
      temperature = TempConverter.convertToCelcius(temperature);
      System.out.println("This equals " + temperature + " degrees C.");
      temperature = TempConverter.convertToFahrenheit(temperature);
      System.out.println("This equals " + temperature + " degrees F.");

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured