For this project you are asked to complete the design and implementation of an adding machine application. The adding machine is like a calculator application, but only does addition. The adding machine should function as follows:
Initially the number 0 is displayed. The display is updated as the user presses digit buttons to enter numbers. When the enter button is pressed, the number on the display is added to the previous sum, and the result is displayed. If the enter button is pressed two or more times in a row, only the first press will have any effect. Pressing the clear button resets the adding machine to its initial state.
Click the button below to run the sample solution.
Some of the work has already been done for you. Since we are writing an
application (instead of an applet) the AddingMachine class extends the
Frame
class instead of the Applet
class. A
Frame
is a top level application window. The frame contains
several other user interface objects. There are twelve Buttons: one for
each digit 0-9, a "Clear" button, and an "Enter" button. Also, there is
a Label that is used for the AddingMachine's display.
The starting code
you are given creates all of the user interface objects and arranges them
within the frame. Your job is to complete the design and implementation so
that the application functions properly when the buttons are clicked. You
have been given two methods to make your job easier. The method
displayNumber
takes an int
parameter and sets
the text of the calculator display, accordingly. The method
getButtonNumber
takes a Button
parameter
and returns an int
to indicate the value of a digit button.
(If the button is not labeled with an integer, there will be an error.)
actionListener
interface. What method is required by
that interface? Can you use additional methods to further decompose and
simplify the problem? Hint: There are three types of buttons
that can be clicked -- digits, enter, and clear. You might write a method
to handle each of these types of button clicks.AddingMachine
class, showing all of the instance variables and methods, including their
accessibility (public or private) and data typesAddingMachine
class based on your UML diagram. This will become
a comment at the beginning of your source file.AddingMachine
implements
the actionListener
interface, and add a stub for the
method required by the interface.When your assignment is complete, add some comments to the beginning of the java source code explaining any problems you encountered in completing the assignment and describing any bugs in your solution. Undocumented bugs are worse than documented bugs. If you have undocumented bugs we will assume that you did an inadequate job of testing your code, and you will lose additional points.
To submit your homework, follow the link from the homework page to the homework submission form.
There will be a 10% penalty for assignments received after the due date. Assignments will not be accepted more than one week past the due date.