CIS 180: Object-Oriented Programming I
Programming Project #3

Adding Machine

Due by October 28, 2003

Objectives

The objectives of this project are:

Problem Statement

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.

Design

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.)

  1. Think about what additonal instance variables will be needed. For example, what information does the AddingMachine need to have in order to update the display when the enter button is clicked? How will that information be stored?
  2. In order to handle button clicks you will have to implement the 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.
  3. Draw a UML class diagram of the AddingMachine class, showing all of the instance variables and methods, including their accessibility (public or private) and data types
  4. Develop a written specification for the AddingMachine class based on your UML diagram. This will become a comment at the beginning of your source file.
  5. Create an Eclipse project, and implement and test the specification you developed in the previous step.

Implementation

Test your program after each of the following steps!
  1. Add the initial comment block (purpose of your program, your name, lab section, etc) to the starting code.
  2. Add declarations of any additional instance variables
  3. Add the declaration that AddingMachine implements the actionListener interface, and add a stub for the method required by the interface.
  4. Add stubs for any addtional methods of your design.
  5. Add code to the constructor method to register as an event listener with each of the buttons.
  6. Add code to change the display when digit buttons are pressed. First, just update the display to show the last digit button pressed (one digit). When that is working, modify your code so that the user can enter multiple digit numbers on the display.
  7. Implement the Clear function of the adding machine.
  8. Implement the Enter function of the adding machine.

What to turn in

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.

You're done. Great work!