Homework 11Components using Java Beans and Bound Properties |
Modified: |
Implement the calculator of Homework 6 using components rather a monolithic design.
A calculator as four separate components of a display, keypad, accumulator and a calculation unit is a more accurate model of an electronic calculator and is more loosely coupled, the components have no knowledge of the others. The functionality of the Calculator class is realized using components that connect listeners to data sources.
The calculator consists of the four components: *'ed are provided.
The function of Accumulator is to listen for and accumulate
String digits 0..9 as a double value, "1", "4", "2" would accumulate as:
142.0, for example. The Accumulator must notify listeners whenever the
accumulated value changes and must listen for a reset of the accumulated
value to 0.0
Calculate performs calculations much as in Homework 6 by maintaining an accumulator, operand and operation; applying the binary operation to the accumulator and operand whenever an = operation is received (i.e. entering 12+4= yields 16). The function of Calculate then is to listen for and process String operations =, +, -, etc. and listen for accumulator changes. Calculate must notify listeners whenever the accumulated value changes and notify listeners when to reset (e.g. notify the Accumulator object to reset to 0.0).
Defines the interface to be implemented by other classes requiring setOperation method.
| public interface OperationListener extends
java.util.EventListener { void setOperation(String operation); } |
Defines the interface to be implemented by other classes requiring resetAccumulator method.
| public interface ResetListener extends
java.util.EventListener { void resetAccumulator(); } |
The completed Calculator is below, the figure at right illustrates the dataflow between the components.
| import java.applet.*; import java.awt.*; import java.awt.event.*; public class Calculator extends Applet { public void init() { KeyPad keypad=new KeyPad(); Display display=new Display(); Accumulator accumulator = new Accumulator(); Calculate calculate=new Calculate(); // Connect listeners to sources keypad.addDigitListener( accumulator ); keypad.addOperationListener( calculate ); accumulator.addAccumulatorListener( display ); accumulator.addAccumulatorListener( calculate ); calculate.addCalculateListener( display ); calculate.addResetListener( accumulator ); setLayout(new BorderLayout()); add("North", display); add("Center", keypad); } } |
|
FILES - Download Homework 11 files.
GETTING STARTED
The provided files consist of completed example of a Calculator having only a KeyPad and Display; the only functionality is to display the digit clicked on the KeyPad to the Display. Note that all graphics is handled by the three classes.
To execute, assuming the path includes the Java bin directory:
javac -classpath . *.java appletviewer Calculator.htm
Use the Calculator.java file listed above along with KeyPad.java, Display.java and any other additional files for the completed assignment.
HINTS
- Cover sheet with your name, date, and Homework 11.
- Printouts of all files you created; at a minimum Accumulator and Calculate.
- Screenshot showing calculator operation.