Skip to content
November 14, 2008 / Narayan

Temperature Convert

NOW IN THIS PART U WILL LEARN HOW TO CONVERT TEMPERATURE TO FAHRENHEIT OR VICE VERSA

public class Temperature {

	public static double temp;
	public static final double FAHRENHIET_FREEZ = 32.0;
	public static final double FAHRENHIET_BOIL = 220.0;
	public static final double CELCIUS_FREEZ = 0.0;
	public static final double CELCIUS_BOIL = 100.0;

	//client code
	//now here you can input the value either of fahrenheit
	//or celcius only (but only one);
	public static double celcius;
	public static double fahrenheit;

	public static double fahrenheitToCel(){

		return (fahrenheit-FAHRENHIET_FREEZ)*(5.0/9.0);
	}

	public static double celToFahrenheit(){

		return celcius*(9.0/5.0) + FAHRENHIET_FREEZ;

	}
	public void display(){

		if(this.fahrenheit > FAHRENHIET_FREEZ){
		   System.out.println("The temperature is "+
                   this.fahrenheit +" Fahrenheit which is equal to "
                   + Temperature.fahrenheitToCel() +" celcius ");
		}
	    else{
	      System.out.println("The temperature is "+this.celcius
                  +" Celcius which is equal to " +
                   Temperature.celToFahrenheit() +" Fahrenheit " );
	     }

	}

	public static void main (String[] args) {

	Temperature temp = new Temperature();
	temp.display();

	}

}
Advertisements

2 Comments

Leave a Comment
  1. EJP / Jun 9 2009 4:06 am

    Do get it right thanks, it is ‘Fahrenheit’ and ‘Celsius’.

    And why have you defined a manifest constant for Fahrenheit freezing point and still used 32.0 in the code?

  2. Narayan / Jun 9 2009 11:35 am

    it doesn’t matter at all.

    Ok then ! i’ll do it better

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: