Practice exercise -- Calculator
This is another practice exercise using Swing components, layouts, and event handling.
Note that this came from an assignment from a good while back, because this specification
references Windows XP further down. :-)
Task
First, build the GUI layout for a calculator, so that it looks like this:
Note that this calculator has not only the buttons, but also a text field at the very top,
where the numbers display when calculator buttons are pressed.
- The "display" area should be an uneditable display of information,
like on a real calculator. The display contents will change based on
response to events, but it will not be a box that can get the cursor focus
and be typed directly into. Suggestion: An uneditable text field would
work nicely
- There should be button components for each of the digits, the decimal
point, and the four basic arithmetic operations. (i.e. all the buttons in
the picture for exercise 11.9 should be present).
- There should also be a Clear button, with the label 'C', (like on a
real calculator).
Adding functionality to this layout
Events to handle
- Each button should trigger an action event, which will respond
appropriately to the button that was pressed
- Key events are also to be active, such that pressing the letter or
symbol corresponding to a button will trigger the same event that
pressing the button would. (For example, pressing the '5' key is the
same as clicking the '5' button)
- While pressing the '=' key should be the same as clicking the '='
button, also make the 'RETURN' key an event that activates this same
feature (the '=' button, which finalizes a pending operation in the
calculator).
Functionality
The idea is to make this work like a real calculator would, for the simple
arithmetic calculations (addition, subtraction, multiplication, division).
Some things to remember:
- When a number is typed, the digits will show up in the display as the
user types them. (e.g. if '1' is pressed, '1' shows up in the display.
Then, if '2' is pressed immediately after, "12" is now on the display.
etc).
- Decimal numbers need to be taken into account. However, if a decimal
is already part of the number being entered, pressing the '.' button again
would have no effect
- The '=' button is not the only one that causes a result to be shown in
the display area. For example, if the buttons are activated in this
sequence: 12+34+ , then on the last button pressed so far (the '+'), the
intermediate result 46 shows in the display area until new data is entered
for this string of operations.
- Some calculators take order of operations into account in a string of
continuous operations (cascaded operations with no = key). You do NOT
need to do this. Assume that any operation key (including = ) will
"enter" the previous operation and display the result.
- Example: If I enter 2 + 4 * 5 = , this will do 2+4 when I click the
* button, and display 6. Then, when the next operand is entered and
= is pressed, it will do 6*5 and display 30.
- Note: This matches how the calculator program built in to Windows XP
works, but some calculators are smarter than this, and will
handle order of operations on a cascaded entry like this.
- If an operator button is pressed multiple times in a row, this should
have no effect beyond the first one. (The only valid entry after one of
the 4 arithmetic operators will be another numerical operand).
- If the = button is pressed multiple times in a row, this should have
no effect on the display, other than the first time
- If an operator button is pressed after the = button, then the current
displayed result should become the first operand of the new operation
- Try out operations on the Windows calculator proram to see how the
display on a calculator is commonly adjusted.
- Here's an example, with intermediate results shown, of how yours
should update:
Button pressed |
Display showing |
1 | 1 |
2 | 12 |
+ | 12 |
5 | 5 |
. | 5. |
3 | 5.3 |
* | 17.3 |
2 | 2 |
- | 34.6 |
2 | 2 |
4 | 24 |
. | 24. |
9 | 24.9 |
5 | 24.95 |
= | 9.65 |
+ | 9.65 |
8 | 8 |
. | 8. |
5 | 8.5 |
= | 18.15 |
= | 18.15 |