Search This Blog

Temperature Converter Program Using Java - Celsius, Kelvin, Fahrenheit, Reaumur and Rankine

free java temperature converter program. java temperature conversion using java program unit converter celsius kelvin fahrenheit and other different temperature scales
Java Temperature Converter Program
A responsive and accurate temperature converter program can be made using java. In this post, we
will see a good temperature converter code. This java temperature conversion application converts temperature between Celsius, Kelvin, Fahrenheit, Reaumur and Rankine scales. The java program for this temperature consists of methods for:
  • Celsius to Fahrenheit Conversion
  • Celsius to Kelvin Conversion
  • Celsius to Reaumur Conversion
  • Celsius to Rankine Conversion
  • Fahrenheit to Celsius Conversion
  • Fahrenheit to Kelvin Conversion
  • Fahrenheit to Reaumur Conversion
  • Fahrenheit to Rankine Conversion
  • Kelvin to Celsius Conversion
  • Kelvin to Fahrenheit Conversion
  • Kelvin to Reaumur Conversion
  • Kelvin to Rankine Conversion
  • Reaumur to Celsius conversion
  • Reaumur to Kelvin conversion
  • Reaumur to Fahrenheit conversion
  • Reaumur to Rankine conversion
  • Rankine to Celsius conversion
  • Rankine to Kelvin conversion
  • Rankine to Fahrenheit conversion
  • Rankine to Reaumur conversion

All the temperature conversions between these different temperature scales can be handled fast and easily by the following java program. The java code for the temperature converter is as follows:

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;


public class TemperatureConv extends JFrame{
int callCount=0;
JTextField celsius=new JTextField();
JTextField fahren=new JTextField();
JTextField kelvin=new JTextField();
JTextField reaumur=new JTextField();
JTextField rankine=new JTextField();

DocumentListener dl=new DocumentListener() {
public void insertUpdate(DocumentEvent e) {Convert(e);}
public void removeUpdate(DocumentEvent e) {}
public void changedUpdate(DocumentEvent e) {}
};

public TemperatureConv()
{

setSize(200,200);
setLayout(new GridLayout(5,2));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel l_celsius=new JLabel("Celsius");
JLabel l_fahren=new JLabel("Fahrenheit");
JLabel l_kelvin=new JLabel("Kelvin");
JLabel l_reaumur=new JLabel("Reaumur");
JLabel l_rankine=new JLabel("Rankine");
Container cp=getContentPane();
cp.add(l_celsius);
cp.add(celsius);
cp.add(l_fahren);
cp.add(fahren);
cp.add(l_kelvin);
cp.add(kelvin);
cp.add(l_reaumur);
cp.add(reaumur);
cp.add(l_rankine);
cp.add(rankine);
celsius.getDocument().putProperty("owner",(Object)celsius);
fahren.getDocument().putProperty("owner",(Object)fahren);
kelvin.getDocument().putProperty("owner",(Object)kelvin);
reaumur.getDocument().putProperty("owner",(Object)reaumur);
rankine.getDocument().putProperty("owner",(Object)rankine);
celsius.getDocument().addDocumentListener(dl);
fahren.getDocument().addDocumentListener(dl);
kelvin.getDocument().addDocumentListener(dl);
reaumur.getDocument().addDocumentListener(dl);
rankine.getDocument().addDocumentListener(dl);
}

public void Convert(DocumentEvent de)
{
if(callCount!=0)
    {
    callCount=(callCount+1)%5;
    return;
    }
callCount=(callCount+1)%5;
JTextField txt=(JTextField)de.getDocument().getProperty("owner");
try{
if(txt.getText().equals("")==false&&txt.getText()!=null)
    {
    float src=Float.parseFloat(txt.getText());
    if(txt.equals(celsius))
        {
        CtoF(src);
        CtoK(src);
        CtoRa(src);
        CtoRe(src);
        }
    else if(txt.equals(fahren))
        {
        FtoC(src);
        FtoK(src);
        FtoRa(src);
        FtoRe(src);
        }
    else if(txt.equals(kelvin))
        {
        KtoC(src);
        KtoF(src);
        KtoRa(src);
        KtoRe(src);
        }
    else if(txt.equals(reaumur))
        {
        RetoC(src);
        RetoF(src);
        RetoK(src);
        RetoRa(src);
        }
    else if(txt.equals(rankine))
        {
        RatoC(src);
        RatoF(src);
        RatoK(src);
        RatoRe(src);
        }
    }
}catch(Exception ex){JOptionPane.showMessageDialog(this,"Bad input");}
}

public void CtoF(float c)
{
float f=c*9.0f/5.0f+32;
fahren.setText(Float.toString(f));
}

public void CtoK(float c)
{
float k=c+273.15f;
kelvin.setText(Float.toString(k));
}

public void CtoRe(float c)
{
float Re=c*4.0f/5.0f;
reaumur.setText(Float.toString(Re));
}

public void CtoRa(float c)
{
float ra=(c+273.15f)*9f/5f;
rankine.setText(Float.toString(ra));
}

public void FtoC(float f)
{
float c=(f-32)*5f/9f;
celsius.setText(Float.toString(c));
}

public void FtoK(float f)
{
float k=(f+459.67f)*5f/9f;
kelvin.setText(Float.toString(k));
}

public void FtoRe(float f)
{
float re=(f-32)*4f/9f;
reaumur.setText(Float.toString(re));
}

public void FtoRa(float f)
{
float ra=f+459.67f;
rankine.setText(Float.toString(ra));
}

public void KtoC(float k)
{
float c=k-273.15f;
celsius.setText(Float.toString(c));
}

public void KtoF(float k)
{
float f=k*9f/5f-459.67f;
fahren.setText(Float.toString(f));
}

public void KtoRe(float k)
{
float re=(k-273.15f)*4f/5f;
reaumur.setText(Float.toString(re));
}

public void KtoRa(float k)
{
float ra=k*9f/5f;
rankine.setText(Float.toString(ra));
}

public void RetoC(float re)
{
float c=re*5f/4f;
celsius.setText(Float.toString(c));
}

public void RetoF(float re)
{
float f=re*9f/4f+32;
fahren.setText(Float.toString(f));
}

public void RetoK(float re)
{
float k=re*5f/4f+273.15f;
kelvin.setText(Float.toString(k));
}

public void RetoRa(float re)
{
float ra=re*9f/4f+491.67f;
rankine.setText(Float.toString(ra));
}

public void RatoC(float ra)
{
float c=(ra-491.67f)*5f/9f;
celsius.setText(Float.toString(c));
}

public void RatoF(float ra)
{
float f=ra-459.67f;
fahren.setText(Float.toString(f));
}

public void RatoK(float ra)
{
float k=ra*5f/9f;
kelvin.setText(Float.toString(k));
}

public void RatoRe(float ra)
{
float re=(ra-491.67f)*4f/9f;
reaumur.setText(Float.toString(re));
}

public static void main(String args[])
{
new TemperatureConv().setVisible(true);
}

}

No comments:

Post a Comment