• Java 2 Platform Standard Edition 5.0 API specification
• OpenJDK source code repository for library classes
• Source code from Data structures and problem-solving using Java, third edition
The assignment is to implement, and test a simple calculator program with a graphical user interface written in Swing.
From the user's point of view, the program should consist of two panels,
one above the other. The first panel should contain three widgets
in a vertical column; the top two should be JTextField objects, into
which the user can type numbers for the calculator to operate on, and the
third should be a JLabel that displays the most recently computed
result. The second panel should contain one button for each operation that
the calculator supports: addition, subtraction, multiplication, division,
copying the current result into either of the JTextField objects,
and clearing either of the JTextField objects. These buttons should
be arranged in a grid, two rows of four buttons each. A single button in a
third row should quit the application.
The user should be able to change the contents of the JTextField
objects by clicking in the text field and editing its contents, erasing or
deleting unwanted digits and typing in the numeral that denotes the desired
value. The JTextField class itself should provide all this
functionality, but you should check to make sure that it works as expected.
The JLabel should be blank initially. As soon as any of the four
arithmetic operations has been performed even once, the JLabel
should always contain the result of the most recent such operation.
When the user clicks on the addition button, the calculator should add
together the numbers displayed in the two JTextField objects and
change the text of the JLabel to their sum. All the arithmetic
should be done in the double data type, even if the numerals that
the user types in don't have decimal points. If either JTextField
contains a string of characters that doesn't express a value of the double type, the text in the JLabel should change to an appropriate
error message.
Similarly, when the user clicks on the subtraction button, the calculator
should subtract the number in the second JTextField from the number
in the first one and display the difference in the JLabel, and
similarly again for multiplication and division.
When the user clicks on a button that copies the current result into one of
the operand positions, the contents of that JTextField should be
replaced with the text of the JLabel, unless that text is an error
message, in which case the text of the JLabel should change to
another appropriate error message.
When the user clicks on a button that clears one of the operand positions,
the contents of the JTextField should disappear.
You should be able to demonstrate the normal operation of your program by
using it to compute, say, ((38 * 71) - 29) / 162.
Once your program is complete, try to crash it by giving it bad inputs or
ones that might somehow break the underlying arithmetic model. What
happens when someone tries to divide by zero? What happens when the user
clicks on the addition key and both of the JTextField objects are
blank? What happens if the numbers get too big for the double type?
This assignment will be due on Wednesday, March 12.