/**************************************************************************/
/* Program Name: Lab# 3
*/
/*
*/
/* Student Name: John Q. Programmer
*/
/* Semester: Spring, 2014
*/
/* Class & Section: CoSc 10403 - 15 */
/* Instructor: Dr. Hallie Pena
*/
/* Due Date: February 23, 2014
*/
/*
*/
/* Program Overview:
*/
/* This applet
uses a text field to accept user input representing */
/* a Fahrenheit
temperature. When the user presses
the <enter key>, */
/* the temperature
is converted to Celsius. the
result is displayed */
/* in a label and,
in addition, in a text area that serves as an */
/* ongoing log of
all conversions that have been performed.
The */
/* user can
continue to perform terperature conversions until the */
/* applet is
terminated.
*/
/*
*/
/* Input:
*/
/* Input data must
be the integer Fahrenheit value that is to be */
/* converted to
Celsius.
*/
/*
*/
/* Output:
*/
/* The corresponding
Celsius value for the given input data.
Past */
/* conversions are
displayed in a scrollable text area at the bottom */
/* of the display
window.
*/
/*
*/
/* Program Limitations:
*/
/* (1) only
integer Fahrenheit values may be input for conversion. */
/* (2) care should
be taken when inputing values to be converted as */
/* the previous
input value remains in the input textfield until */
/* erased.
*/
/*
*/
/* Significant Program Variables:
*/
/* title
- holds the title that is displayed at the */
/*
top of the display window.
*/
/* question
- holds the string that is displayed as */
/*
the user prompt.
*/
/* answer
- holds the string that is displayed to */
/*
identify the convered value. */
/* fahrenheit
- the name of the text field in which */
/*
the Fahrenheit value is stored. */
/* celsius
- the name of the label field that will */
/*
display
the resulting Celsius value. */
/* log
- text area that records past conversions. */
/*
fahrenheit_temperature - the iinteger Fahrenheit input value. */
/*
celsius_temperature
- the resulting Celsius value.
*/
/*
*/
/* References:
*/
/* (1) this
program originally appeared in: "Java Software */
/*
Solutions", John Lewis and William Loftus, Addison-Wesley, */
/* 1998, pp.
375-377.
*/
/**************************************************************************/
import
java.awt.*;
import
java.applet.Applet;
import
java.awt.event.*;
/**************************************************************************/
/* Fahrenheit Class - Creates the GUI
container and converts input */
/*
temperatures from Fahrenheit to Celsius. */
/**************************************************************************/
public
class Fahrenheit extends Applet
{ //start
of class Fahrenheit
// Set up text
strings and GUI components
private Label
title = new Label ("Fahrenheit to Celsius Converter");
private Label
question = new Label ("Enter Fahrenheit:");
private Label
answer = new Label ("Temperature in Celsius:");
private TextField
fahrenheit = new TextField (5);
private Label
celsius = new Label ("N/A");
private TextArea
log = new TextArea (5,20);
private
Fahrenheit_Listener listener = new Fahrenheit_Listener (this);
/**********************************************************************/
/* Init Method - place GUI components into
the screen container.
*/
/**********************************************************************/
public void
init()
{ //start of
method init
fahrenheit.addActionListener
(listener);
log.setEditable
(false);
add
(title);
add
(question);
add
(fahrenheit);
add
(answer);
add
(celsius);
add
(log);
resize
(200,200);
} //end of method
init
/**********************************************************************/
/* Convert Method - input Fahrenheit value
is converted to Celsius */
/* when
directed to do so by the action listener. */
/**********************************************************************/
public void
convert()
{ //start of
method convert
int
fahrenheit_temperature, celsius_temperature;
String
text = fahrenheit.getText();
fahrenheit_temperature
= Integer.parseInt (text);
celsius_temperature
= (fahrenheit_temperature - 32) * 5 / 9;
celsius.setText
(Integer.toString (celsius_temperature));
log.append
(text + " converts to " + celsius.getText() + "\n");
} // end of
method convert
} // end
of class Fahrenheit
/**************************************************************************/
/* Listener Class - listens for an action
event associated with entering */
/*
a value into the Fahrenheit input field.
*/
/**************************************************************************/
class
Fahrenheit_Listener implements ActionListener
{ //
start of class Fahrenheit_Listener
private
Fahrenheit applet;
public Fahrenheit_Listener
(Fahrenheit listening_applet)
{ // start of
constructor Fahrenheit_Listener
applet
= listening_applet;
} // end of
constructor Fahrenheit_Listener
public void
actionPerformed(ActionEvent action)
{ // start of
method actionPerformed
applet.convert();
} // end of
method actionPerformed
} // end
of class Fahrenheit_Listener