/** * title: GoodDocs.java * description: An example of javadoc use and proper code documentation * date: November 11, 2001 * @author Richard S. Huntrods * @version 1.0 * @copyright 2001-2003 Richard S. Huntrods */ /** * DOCUMENTATION... */ /** * *
* An application that converts temperature from degrees Fahrenheit to degrees Celcius. *
** This program uses a Graphical User Interface (GUI) based on Frames to interact with the user. * The program includes a text field, and numerous labels to present the user interface. * User input is processed when the user presses the ENTER key while the focus is in the * input temperature text field. *
** This program uses the Java Swing API for GUI programming, and requires the * Sun Java SDK version 1.3 or better. *
* *
* public class GoodDocs extends JFrame {
* This is the main public class for this application. It extends (inherits from) JFrame
* to provide access to Java Swing Frame methods for the GUI.
*
private class TextFieldHandler implements ActionListener {
Class TextFieldHandler implements ActionListner to provide the event handling
for this application. It is a private inner class, which gives it access to
all of the GoodDocs instance variables
*
public GoodDocs() {
Constructor for GoodDocs class - this method is used to set up the GUI
*
public static void main(String args[]) {
This method is used to execute the application
*
private String convertFtoC(String input) {
This method performs the actual conversion from Fahrenheit to Celcius. The method
takes input as a String, and returns a String to provide better coordination with
GUI components, which return or require Strings
*
* public void actionPerformed(ActionEvent event) (
* This method is required for an ActionListener to process the events
*
*
* private JTextField degreesF; - JTextField to hold input degrees F. * private JLabel degreesC; - JLabel to hold output results, degrees C. *
*/ /** * ** 1. Run the application. * EXPECTED: * GUI frame should display the following: * title = "Temperature Conversion" * label = "Temperature Conversion Program" * label = "Input Temperature" * textbox for receiving input temperature. * label for input temperature = "Degrees F." * label for output temperature = "Output Temperature: ... Degrees C." * label to display output temperature * overall size should be 300 by 100 pixels * ACTUAL: * GUI frame displays as expected. *
** 2. Good data cases: * EXPECTED: * Run the following test cases by typing the input value and pressing enter: * 0 -> output should be -17.777777... * 32 -> output should be 0 * 212 -> output should be 100 * -40 -> output should be -40 * ACTUAL: * Results displayed as expected. *
** 3. Bad data cases: * EXPECTED: * Run the following test cases by typing the input value and pressing enter: * 'q' -> output should be -17.777777... * (note - this will be true for any non-numeric input) * ACTUAL: * Results displayed as expected. *
*/ /** * CODE... */ /** Java core packages */ import java.awt.*; import java.awt.event.*; /** Java extension packages to support Swing */ import javax.swing.*; /** primary (public) class for GoodDocs */ public class GoodDocs extends JFrame { /** JTextField to hold input degrees F. */ private JTextField degreesF; /** JLabel to hold output results, degrees C. */ private JLabel degreesC; /** Constructor for GoodDocs class - this method is used to set up the GUI */ public GoodDocs() { // call the JFrame parent class with the window title super("Temperature Conversion"); // we need the container for JFrames in order to add components Container container = getContentPane(); // default layout is BorderLayout - we want a FlowLayout container.setLayout(new FlowLayout()); // private label to store the heading JLabel title = new JLabel("Temperature Conversion Program"); container.add(title); // private label to store prompt text JLabel prompt = new JLabel("Input Temperature: "); container.add(prompt); // create an instance of the JTextField for a specific number of columns degreesF = new JTextField(5); container.add(degreesF); // private label for the input units JLabel units = new JLabel("Degrees F."); container.add(units); // create an instance of the JLabel holding the output result degreesC = new JLabel("Output Temperature: ... Degrees C."); container.add(degreesC); // register the TextField event handler - this is an inner class TextFieldHandler handler = new TextFieldHandler(); degreesF.addActionListener(handler); // set the applications size and make it visible setSize(300, 100); setVisible(true); } /** This method is used to execute the application */ public static void main(String args[]) { // create an instance of our application GoodDocs application = new GoodDocs(); // set the JFrame properties to respond to the window closing event application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } /** This method performs the actual conversion from Fahrenheit to Celcius. The method takes input as a String, and returns a String to provide better coordination with GUI components, which return or require Strings */ private String convertFtoC(String input) { // variables to store the real values of the Strings double tempF = 0.0; double tempC = 0.0; // use a try-catch block to contain data conversion exceptions try { // use the Double wrapper class to parse the string to a double tempF = Double.parseDouble(input); } catch (NumberFormatException nfe) { // no need to do anything here - errors will leave tempF = 0.0 } // perform the conversion tempC = 5.0 / 9.0 * (tempF - 32.0); // return the result as a String return Double.toString(tempC); } /** Class TextFieldHandler implements ActionListner to provide the event handling for this application. It is a private inner class, which gives it access to all of the GoodDocs instance variables */ private class TextFieldHandler implements ActionListener { /** This method is required for an ActionListener to process the events */ public void actionPerformed(ActionEvent event) { String string = ""; // The user pressed Enter in the JTextField textField1 if(event.getSource() == degreesF) // retrieve the input as a String string = convertFtoC(event.getActionCommand()); // convert input to output and display degreesC.setText("Output Temperature: " + string + " Degrees C."); } } // end of the private inner class TextFieldHandler } // end of public class GoodDocs