Homework 11

Components using Java Beans and Bound Properties

Modified

Assignment

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.

  1. *public class Display extends Panel implements PropertyChangeListener
  2. *public class KeyPad extends Panel
  3. public class Accumulator implements PropertyChangeListener, ResetListener

    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
     

  4. public class Calculate implements PropertyChangeListener, OperationListener

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

     

  5.  interface OperationListener

    Defines the interface to be implemented by other classes requiring setOperation method.

     public interface OperationListener extends java.util.EventListener {
        void setOperation(String operation);
    }

     

  6. interface ResetListener

    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.

Calculator.java
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

  1. Can use the OperationListener and ResetListener as listed above.
  2. Use System.out.println to output values to the command window for debugging.
  3. Start with Accumulator class, it is the simplest. It connects the KeyPad to the Display objects, useful to test your understanding of bean communication.
  4. Remember the difference within a class definition between listening for a property change and notifying listeners of a property change.
  5. firePropertyChange has no effect when old and new values are the same. Print out the old and new values of firePropertyChange to be certain that the values differ.
  6. For simplicity, pass String objects between components using firePropertyChange.

TURN IN