Temperature Conversion Program in Java

Here's a simple program to convert temperatures between degrees Celcius, Fahrenheit and Kelvin written in Java. There's no GUI used here - all output is done via the command line.
The forumulae for the conversions were obtained from Temperature conversion forumlas on Wikipedia

File: Converter.java

JAVA:
  1. /**
  2. * Conversion between temperatures in Celcius, Fahrenheit and Kelvin
  3. * Uses conversion forumulas from the wikipedia:
  4. * http://en.wikipedia.org/wiki/Temperature_conversion_formulas
  5. * User: Steven
  6. * Date: 27-Dec-2005
  7. */
  8. public class Converter {
  9.     public Converter()
  10.     {
  11.  
  12.     }
  13.  
  14.     // Method to convert from degrees Celcius to degrees Fahrenheit
  15.     public static float celciusToFahrenheit(float degCelcius)
  16.     {
  17.         float degFahrenheit;
  18.         degFahrenheit = degCelcius * 9/5 + 32;
  19.         return degFahrenheit;
  20.     }
  21.  
  22.     // Method to convert from degrees Fahrenheit to degrees Celcius
  23.     public static float fahrenheitToCelcius(float degFahrenheit)
  24.     {
  25.         float degCelcius;
  26.         degCelcius =  (degFahrenheit - 32) * 5/9;
  27.         return degCelcius;
  28.     }
  29.  
  30.     // Method to convert from degrees Celcius to degrees Kelvin
  31.     public static float celciusToKelvin(float degCelcius)
  32.     {
  33.         float degKelvin;
  34.         degKelvin = degCelcius + 273.15f;
  35.         return degKelvin;
  36.     }
  37.  
  38.     // Method to convert from degrees Kelvin to degrees Celcius
  39.      public static float kelvinToCelcius(float degKelvin)
  40.     {
  41.         float degCelcius;
  42.         degCelcius = degKelvin - 273.15f;
  43.         return degCelcius;
  44.     }
  45.  
  46.     // Main method demonstrating usage of above methods
  47.     public static void main(String[] args)
  48.     {
  49.         System.out.print("100 degrees Celcius in Fahrenheit is: ");
  50.         System.out.println(celciusToFahrenheit(100));
  51.  
  52.         System.out.print("-40 degrees Fahrenheit in Celcius is: ");
  53.         System.out.println(fahrenheitToCelcius(-40));
  54.  
  55.         System.out.print("0 degrees Celcius in Kelvin is: ");
  56.         System.out.println(celciusToKelvin(0));
  57.  
  58.         // to convert from Kelvin to Fahrenheit, combine two methods:
  59.         System.out.print("373.15 degrees Kelvin in Fahrenheit is: ");
  60.         System.out.println(celciusToFahrenheit(kelvinToCelcius(373.15f)));
  61.     }
  62. }

If you have a low-traffic website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!


No Responses to “Temperature Conversion Program in Java”  

  1. No Comments

Leave a Reply

You must log in to post a comment.


Categories