VB icon

Temperature Converter

Email
Submitted on: 1/2/2015 7:47:00 PM
By: Randy McCleary (from psc cd)  
Level: Intermediate
User Rating: By 9 Users
Compatibility: Java (JDK 1.2)
Views: 4252
 
     This is just a simple java applet that will convert Degrees Fahrenheit to Degrees Celesius. Very simple and can be modified to convert back.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Temperature Converter
// Description:This is just a simple java applet that will convert Degrees Fahrenheit to Degrees Celesius. Very simple and can be modified to convert back.
// By: Randy McCleary (from psc cd)
//**************************************

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class temperature extends JApplet implements ActionListener
{
JLabel lblFahrenheit;
JLabel lblCelsius;
JLabel outputCelsius;
JTextField txtFahrenheit;
double degreesFahrenheit;
FlowLayout layout;
public void init()
{
 Container c = getContentPane();
 
 layout = new FlowLayout();
 layout.setAlignment( FlowLayout.LEFT);
 c.setLayout(layout);
 // instantiate JLabel object for Degrees Fahrenheit
 lblFahrenheit = new JLabel("Enter Fahrenheit and press enter: ");
 // instantiate JTextField object for the degrees fahrenheit
 txtFahrenheit = new JTextField(10);
 // instantiate JLabel object for Degrees Celesius
 lblCelsius = new JLabel("Degrees Celesius: ");
 // JLabel to display the equivalent degrees Celsius
 outputCelsius = new JLabel("");
 c.add(lblFahrenheit);
 //CALL the addActionListener() method on the JTextField object
 // for the degrees Fahrenheit
 txtFahrenheit.addActionListener(this);
 // Add the textbox the celsius label and output label
 c.add(txtFahrenheit);
 c.add(lblCelsius);
 c.add(outputCelsius);
}
public void actionPerformed(ActionEvent e)
{
 // Create a new decimal format to one digit
 DecimalFormat oneDigit = new DecimalFormat("0.0");
 // Get the input
 double Fahrenheit = Double.parseDouble(e.getActionCommand());
 //ASSIGN the result of CALL Celsius function
 double celsius = Celsius(Fahrenheit);
 //Display the results in the label
 outputCelsius.setText(oneDigit.format(celsius));
}
public double Celsius(double Fahrenheit)
{
 double celsius;
 //Formulat to convert Farhrenheit to celsius
 celsius = 5.0 / 9.0 * ( Fahrenheit - 32);
 return celsius;
}
}


Other 8 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.