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

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

     

  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();
    }


     

  7. *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);
  }
}
 

 

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

  1. Use System.out.println to output values to the command window for debugging.
  2. Start with Accumulator class, it is the simplest. It connects the KeyPad to the Display objects, useful to test your understanding of bound property communication.
  3. Remember the difference within a class definition between listening for a bound property change and notifying listeners of a bound property change. 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.

    For simplicity, pass String objects between components using firePropertyChange.

  4. OperationListener and ResetListener are similar, neither uses Java Beans bound properties. KeyPad OperationListener elements can serve as an example for the Calculate, ResetListener elements.
  5. As a OperationListener, Calculate must implement setOperation():

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

TURN IN