Fahrenheit 2 Celsius

| Posted in , | Posted on 11:53 PM

This is considerably simpler than my banking interest calculation but then again it's late, a weekend, and I just wanted to get some practice code in even if it's nothing spectacular. It's clean and I'm happy with that.

It's a small, short, and sweet program that takes a temperature (Fahrenheit) and converts it to Celsius.


/*
* This is a simple program meant to convert a fahrenheit
* temperature to celsius. It's drawn from Chap 2, Ex 1
*/

package fahrenheit_2_celcius;

// pull in a javax class for gui message boxes
import javax.swing.JOptionPane;

/**
* @author Neil
*/
public class Main {

public static void main(String[] args) {

// prompt user for a fahrenheit temperature and store it in variable
String stringFahrenheit = JOptionPane.showInputDialog (null,
"Enter your temperature" + " in fahrenheit", "Fahrenheit 2 "
+ "Celsius Calculator", JOptionPane.QUESTION_MESSAGE);

// convert stringFahrenheit into double variable
double fahrenheit = Double.parseDouble(stringFahrenheit);

// convert fahrenheit into celcius and store it in variable
double celsius = (5.0/9.0)*(fahrenheit-32.0);

// display converted temperature

JOptionPane.showMessageDialog(null, fahrenheit + " degrees fahrenheit"
+ " is equal to " + celsius + " degrees celsius.", "Fahrenheit 2 "
+ "Celsius Calculator", JOptionPane.QUESTION_MESSAGE);

}

}

Comments Posted (0)

Post a Comment