Temperature Conversion Program
This program uses the switch statement to convert one temperature degree to another and also uses the JOptionPane input/output dialog box from the javax.swing. package to display the information.
we also have to note that when inputting digits in java applications, it is entered as strings which can then be converted to corresponding data types like integers and floating point numbers.
the program below illustrates temperature conversion program.
the program below illustrates temperature conversion program.
import javax.swing.JOptionPane;
public class Temperature{
public static void main(String args[])
{
String from;
String to;
String value;
int number1;
int number2;
double temp;
JOptionPane.showMessageDialog(null, "Temperature Conversion");
from = JOptionPane.showInputDialog("Enter no of unit to convert from: 1.Celcius 2. Farenheit 3. Kelvin");
to = JOptionPane.showInputDialog("Enter no of unit to convert to: 1. Celcius 2. Farenheit 3. Kelvin");
value = JOptionPane.showInputDialog("Enter the value to convert: ");
number1 = Integer.parseInt(from);
number2 = Integer.parseInt(to);
temp = Double.parseDouble(value);
switch(number1)
{
case 1:
temp = temp + 273.15; break;
case 2:
temp = (temp + 459.67)*5/9; break;
case 3: break;
default : break;
}
switch (number2)
{
case 1:
temp = temp - 273.15; break;
case 2:
temp = temp * 9/5 - 459.67 ; break;
case 3: break;
default : break;
}
JOptionPane.showMessageDialog(null,"Converted Value is: " + temp , "Temperature Conversion", JOptionPane.PLAIN_MESSAGE );
System.exit(0);
}
}
Comments
Post a Comment
Please feel free to comments if you have any ideas, questions and subjection concerning our post and programs