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 source events.
The Calculator application implementation follows a Model-View-Controller architecture/pattern, similar to that discussed in class.
The Calculator application consists of the four components, two interfaces and the calculator: *'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 internal 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 property changes. Calculate must notify listeners whenever the internal accumulator value changes and notify listeners when to reset (e.g. notifying the Accumulator object resets it 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(); } |
| 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); } } |
|
The MVC pattern and components correspond as:
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
For simplicity, pass String objects between components using firePropertyChange.
public void setOperation(String operation) {
System.out.println("Calculate " + operation);
}
can serve as a starting point.
As a ResetListener, the Accumulator class will need to implement resetAccumulator().
- 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.