Reference: Chapter 2 of the Swing Tutorial book
The assignment:
Make an enhanced temperature converter that converts either from
Celsius to Fahrenheit or from Fahrenheit to Celsius.
This can probably
be most easily done by modifying the SwingApplication code for
Example 6 of Chapter 2. It can be downloaded from:
VoteDialog.java.
The interface should look something like this:
------------------------ | | | ---------------- | | | <textField> | | | ---------------- | | | | ---------------- | | | (*) C to F | | | ---------------- | | | ( ) F to C | | | ---------------- | | ---------------- | | | Convert | | | ---------------- | | | | Fahr temp: 93 | | | ------------------------
Discussion:
The biggest change from VoteDialog is that you don't have
to worry about dialogs. This will make the code for
actionPerformed() much simpler.
The other changes would include:
(1)
replacing the title JLabel with a JTextField, which I
also had to declare above VoteDialog (to make it
"visible to the actionPerformed() function below).
(2) changing the radiobox from 4 buttons to 2 and changing their labels (you might also want to give the ActionCommands more meaningful names).
(3) changing the label on the "Vote" button to "Convert" (trivial).
(4)
changing what the actionPerformed() method does.
This can be done by modifying the existing code (ignoring all
the dialog stuff).
The default should be to convert from Celsius to Fahrenheit, and
the code can be very similar to that in the
CelsiusConverter program: get the current text
from the text field widget and convert its value to Fahrenheit
(using the conversion formula code in
CelsiusConverter), then setting the text of
the label at the bottom to something like:
Converted Temp: 93°F
The case for conversion from Fahrenheit to Celsius can be
handled similarly, recalling the formula for converting
Fahrenheit to Celsius:
tempCelsius = (tempFahr - 32)*5/9
(5) changing the names of the variables to be more meaningful for this application.
Extra Enhancements:
There may be a better or more natural way to lay out the interface
(maybe using a grid layout). Also wording on the labels on the
widgets could probably be improved.
What to turn in:
Turn in the following items from the
Computer Science Lab Report Format:
1. (1 point) The Basic Information (your name, class, section,
TA's name, assignment number, and date) can be on a separate cover sheet or as
(highlighted) comments at the top of your main program file.
11. (4 points) Include the program listing.
This listing should show good style,
be appropriately commented, have the important parts
highlighted, and include handwritten explanations if it helps the
reader's understanding of the code.
13. (5 points) The most important part.
Do demos of your modified program for the TA that demonstrate that
the program works correctly.