CASE STUDY
414
The clear button has a different function than the other buttons. When it is clicked,
the register is set to zero; no value is input from the text field, and no arithmetic opera-
tion is performed. It makes sense to use one button handler for the four arithmetic but-
tons, but a separate one for the clear button.
Most of the objects are instances of classes provided in the Java library. Thus the only
new classes are the two event handlers andCalculator. The main responsibility of themain
method will be to set up the frame and populate it with all of the user interface objects.
Responsibility Algorithms:The first step is to identify the fields of the Calculatorclass.
They are mainly the interface variables. Declaring these variables requires us to make a
list of the components in the window. We need a frame and content pane to hold every-
thing, three labels (one for input and two for the register), a field for entering a data
value, four buttons for the user to click to indicate the operation when a value is ready
for input, and a button for the user to click to clear the register. This same list also tells
us what we need to add to the frame. This portion of the responsibility algorithms is
shown below. All of the variables should be class fields, so they are static. We can give
them package access, rather than making them explicitly private.
Class Name: ClearHandler Superclass: Subclasses:
Responsibilities Collaborations
Action performed
.
.
.
JTextField, ActionEvent
Class Name: NumericHandler Superclass: Subclasses:
Responsibilities Collaborations
Action performed
.
.
.
JTextField, ActionEvent