/* * ============================================================================ * Celsius.java : Windows version of Celsius to Farenheit Converter Program * * Written By : David Chancogne September 1997 * ============================================================================ */ import java.lang.Math; import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; class TempConversion extends Frame { TextField CelText; TextField FarenText; Button CelButton; Button FarenButton; Button QuitButton; // The main method just creates an instance of the class public static void main( String args[] ) { System.out.println("Celsius to Farenheit Converter Program"); TempConversion p = new TempConversion(); } // The constructor creates the window. public TempConversion() { setTitle("Temperatures Converter"); CelButton = new Button("Celsius"); FarenButton = new Button("Farenheit"); QuitButton = new Button("Quit"); CelText = new TextField(7); FarenText = new TextField(7); setLayout(new GridLayout(3,2)); add(CelButton); add(CelText); add(FarenButton); add(FarenText); add(QuitButton); // Add listeners for the various components QuitButton.addMouseListener(new QuitListener()); CelText.addKeyListener(new CelListener(CelText, FarenText)); FarenText.addKeyListener(new FarenListener(CelText, FarenText)); // Set the window size and display it. setSize(450,150); show(); } // The temperature conversion methods. public static float CelsiusToFarenheit(float fCel) { return (float)(9.0/5.0 * fCel + 32.0); } public static float FarenheitToCelsius(float fFahrn) { return (float)(5.0/9.0 * (fFahrn - 32.0) ); } } /* * =================================================================== * The following classes handle the events. * * -- 1 class extends the MouseAdapter class and is attached to * the "Quit" button. * * -- 2 classes extend KeyAdapter and are listening for input from * the TextFields. * =================================================================== */ class QuitListener extends MouseAdapter { public void mouseReleased(MouseEvent e) { System.out.println("Thank you !"); System.exit(0); } } class CelListener extends KeyAdapter { TextField CelText; TextField FarenText; public CelListener( TextField c, TextField f ) { CelText = c; FarenText = f; } public void keyReleased(KeyEvent e) { // Keyboard event is release of the 'return' key if ( e.getKeyCode() == KeyEvent.VK_ENTER ) { float f = 0 ; // The method are 'static' so we can call // them without an instance try { f = TempConversion.CelsiusToFarenheit( Float.valueOf(CelText.getText()).floatValue() ); } catch (Exception ex) { System.out.println("Malformated number !"); } FarenText.setText(Float.toString(f)); } } } class FarenListener extends KeyAdapter { TextField CelText; TextField FarenText; public FarenListener( TextField c, TextField f ) { CelText = c; FarenText = f; } public void keyReleased(KeyEvent e) { // Keyboard event is release of the 'return' key if ( e.getKeyCode() == KeyEvent.VK_ENTER ) { float f = 0; // The method are 'static' so we can call // them without an instance try { f = TempConversion.FarenheitToCelsius( Float.valueOf(FarenText.getText()).floatValue() ); } catch (Exception ex) { System.out.println("Malformated number !"); } CelText.setText(Float.toString(f)); } } }