- Version
- Download 69
- File Size 2.26 KB
- File Count 1
- Create Date May 12, 2016
- Last Updated May 12, 2016
Celsius to Fahrenheit converter in Java
Celsius to Fahrenheit converter in Java
Same with the previous program, the user has the option to either convert Celsius to Fahrenheit or Fahrenheit to Celsius.
This is a GUI program in java that uses swing library and JOptionPane message and input dialog for accepting input and displaying the output.
Source code:
import javax.swing.*;
public class CelsiusToFahrenheit
{
public static void main(String args[]) {
String input1;
double celsiusToConvert=0, fahrenheitToConvert=0;
double result=0;
String cont="n";
String option;
do
{
option=JOptionPane.showInputDialog(null, "Please select an operation:" + "
" + "1 for celsius to fahrenheit" + "
" + "2 for fahrenheit to celsius" );
if (option.matches("1")){
input1 = JOptionPane.showInputDialog(null,"Enter value of degree celsius: ");
celsiusToConvert=Double.parseDouble(input1);
result = ((celsiusToConvert * 9 / 5) + 32) ;
JOptionPane.showMessageDialog(null, celsiusToConvert + " degree celsius " + " is equal to " + result + " fahrenheit");
} else if (option.matches("2")){
input1 = JOptionPane.showInputDialog(null,"Enter value of degree fahrenheit: ");
fahrenheitToConvert=Double.parseDouble(input1);
result = ((fahrenheitToConvert - 32) * 5 / 9) ;
JOptionPane.showMessageDialog(null, fahrenheitToConvert + " degree fahrenheit " + " is equal to " + result + " celsius");
}
cont = JOptionPane.showInputDialog(null,"Try Again? (press y or n) ");
}while (cont.matches("y"));
JOptionPane.showMessageDialog(null,"Program closing" );
}
}