com.numobjects.mathbeans
Class CalcSet

com.numobjects.mathbeans.CalcSet

public class CalcSet
implements java.io.Serializable, java.awt.event.ActionListener

Class CalcSet (Calculator set bean) provides the following functionality: based solely on a string representing a set of mathematical expressions with any number of variables, automatically constructs and displays a mini-calculator panel for these expressions. The panel will have named input fields for all variables in the expressions and similar fields for the results. The user enters any numbers into the input fields and presses a button or just Enter in any of these fields. The result is immediately computed and displayed in the result fields. The user can set parameters that control the format of the output.

This bean provides a unique possibility of creating various calculators, static or dynamic, avoiding the hassle of setting up GUI, adding logic, dispatching events, etc. The user's only input is the formula itself, all the rest is figured out by the bean.

The formulas must have the form

Result1=expression1;...ResultN=expressionN

Here "ResultX" is any string providing the name for the result, e.g. "Total amount". The "expressionX" must be a valid non-constant mathematical expression (i.e. must contain variables). Variable names in the expression cannot contain spaces; if there is a need for the displayed screen name to consist of several words, enclose the name in double quotes. Here is one simple example:

"Total tax"=Quantity*"Item price"*"Tax rate"/100;
"Total amount"=Quantity*"Item price"*(1+"Tax rate"/100)+Shipping

The resulting calculator set would contain a parameter panel with fields labeled "Quantity", "Item price", "Tax rate", "Shipping", and a result panel with "Total tax" and "Total amount". As soon as one enters values in the first four fields and presses Enter, the result fields will display the computed values. Missing values are treated as zero.

Here is the code that you have to include in your program:

import com.numobjects.mathbeans.CalcSet;
...
String formula = "Total tax=Quantity*\"Item price\"*\"Tax rate\"/100;Total amount=Quantity*\"Item price\"*(1+\"Tax rate\"/100)+Shipping";
CalcSet calcSet = new CalcSet ();
calcSet.setFormulae (formula);

The calculator set is ready to be added to your container.

See Expression syntax and available functions for a full description of syntax, available functions and procedures.

See Also:
CalcSolveSet, CalcGraphSet, Serialized Form

Constructor Summary
CalcSet()
          Constructs new CalcPanel
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Responds to action events
 void addActionListener(java.awt.event.ActionListener l)
          Adds action listener
 com.numobjects.beans.ControlPanel getControl()
          Gets control panel
 java.lang.String getFormulae()
          Gets the formula set for the calc set
 NamedFieldPanel getParameterPanel()
          Gets the parameter panel
 int getParametersCount()
          Gets the number of parameters of a set of formulas
 int getParametersDisplayCount()
          Gets the number of parameters of a set of formulas
 java.lang.String[] getParameterTitles()
          Gets panel titles for the parameter panel
 java.lang.String getParameterTitles(int i)
          Gets panel title for the parameter panel
 int getPrecision()
          Gets precision
 NamedFieldPanel getResultPanel()
          Gets the result panel
 int getResultsCount()
          Gets the number of formulas in a set
 int getResultsDisplayCount()
          Gets the number of formulas in a set
 java.lang.String[] getResultTitles()
          Gets panel titles for the result panel
 java.lang.String getResultTitles(int i)
          Gets panel title for the result panel
 java.lang.String getResultValue(int index)
          Gets value of a formula
 int getRounding()
          Gets rounding
 java.lang.String getValue(java.lang.String parameterName)
          Gets value of a parameter.
 void removeActionListener(java.awt.event.ActionListener l)
          Removes action listener
 void setBackground(java.awt.Color c)
          Sets background color
 void setControl(com.numobjects.beans.ControlPanel control)
          Sets control panel
 void setFormulae(java.lang.String formulaSet)
          Sets formulae
 void setParameterError(java.lang.String parameterName)
          Sets parameter error for a parameter
 void setParameterError(java.lang.String parameterName, boolean flag)
          Sets parameter error for a parameter
 void setParameterPanel(NamedFieldPanel panel)
          Sets the parameter panel
 void setParameterTitles(int i, java.lang.String title)
          Sets panel title for the parameter panel
 void setParameterTitles(java.lang.String[] titles)
          Sets panel titles for the parameter panel
 void setPrecision(int value)
          Sets precision (the number of significant digits in the calculation); range 1 - 15, default precision is 6
 void setResultPanel(NamedFieldPanel panel)
          Sets the result panel
 void setResultTitles(int i, java.lang.String title)
          Sets panel title for the result panel
 void setResultTitles(java.lang.String[] titles)
          Sets panel titles for the result panel
 void setResultValue(int index, java.lang.String result)
          Sets result value for a given formula in a set
 void setRounding(int value)
          Sets rounding (the number of digits displayed after the decimal dot); range -1 - 15, default rounding is -1 (no rounding)
 void updateSet()
           
 

Constructor Detail

CalcSet

public CalcSet()
Constructs new CalcPanel
Method Detail

getParameterTitles

public java.lang.String[] getParameterTitles()
Gets panel titles for the parameter panel
Returns:
titles

getParameterTitles

public java.lang.String getParameterTitles(int i)
Gets panel title for the parameter panel
Parameters:
i - title index
Returns:
titles

setParameterTitles

public void setParameterTitles(int i,
                               java.lang.String title)
Sets panel title for the parameter panel
Parameters:
i - title index
title - title (string)

setParameterTitles

public void setParameterTitles(java.lang.String[] titles)
Sets panel titles for the parameter panel
Parameters:
titles - titles

getResultTitles

public java.lang.String[] getResultTitles()
Gets panel titles for the result panel
Returns:
titles

getResultTitles

public java.lang.String getResultTitles(int i)
Gets panel title for the result panel
Parameters:
i - title index
Returns:
titles

setResultTitles

public void setResultTitles(int i,
                            java.lang.String title)
Sets panel title for the result panel
Parameters:
i - title index
title - title (string)

setResultTitles

public void setResultTitles(java.lang.String[] titles)
Sets panel titles for the result panel
Parameters:
titles - titles

setBackground

public void setBackground(java.awt.Color c)
Sets background color
Parameters:
c - color

setPrecision

public void setPrecision(int value)
Sets precision (the number of significant digits in the calculation); range 1 - 15, default precision is 6
Parameters:
value - precision to set

getPrecision

public int getPrecision()
Gets precision
Returns:
precision value (the number of significant digits in the calculation)

setRounding

public void setRounding(int value)
Sets rounding (the number of digits displayed after the decimal dot); range -1 - 15, default rounding is -1 (no rounding)
Parameters:
value - rounding

getRounding

public int getRounding()
Gets rounding
Returns:
rounding value (the number of digits displayed after the decimal dot)

getParametersCount

public int getParametersCount()
Gets the number of parameters of a set of formulas
Returns:
number of parameters

getParametersDisplayCount

public int getParametersDisplayCount()
Gets the number of parameters of a set of formulas
Returns:
number of parameters

getResultsCount

public int getResultsCount()
Gets the number of formulas in a set
Returns:
number of formulas

getResultsDisplayCount

public int getResultsDisplayCount()
Gets the number of formulas in a set
Returns:
number of formulas

updateSet

public void updateSet()

setFormulae

public void setFormulae(java.lang.String formulaSet)
Sets formulae
Parameters:
formulaSet - a string representing a set of formulae (semicolon delimited)

getFormulae

public java.lang.String getFormulae()
Gets the formula set for the calc set
Returns:
formula set string

addActionListener

public void addActionListener(java.awt.event.ActionListener l)
Adds action listener
Parameters:
l - ActionListener

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)
Removes action listener
Parameters:
l - ActionListener

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Responds to action events
Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - ActionEvent

getValue

public java.lang.String getValue(java.lang.String parameterName)
Gets value of a parameter. If the value is not numeric, raises an error
Parameters:
parameterName - parameter name
Returns:
parameter value (represented as a string)

getResultValue

public java.lang.String getResultValue(int index)
Gets value of a formula
Parameters:
index - formula index in a set
Returns:
value (represented as a string)

setResultValue

public void setResultValue(int index,
                           java.lang.String result)
Sets result value for a given formula in a set
Parameters:
index - formula index
result - string value to set

setParameterError

public void setParameterError(java.lang.String parameterName)
Sets parameter error for a parameter
Parameters:
parameterName - parameter name
flag - error flag

setParameterError

public void setParameterError(java.lang.String parameterName,
                              boolean flag)
Sets parameter error for a parameter
Parameters:
parameterName - parameter name
flag - error flag

getParameterPanel

public NamedFieldPanel getParameterPanel()
Gets the parameter panel
Returns:
parameter panel

setParameterPanel

public void setParameterPanel(NamedFieldPanel panel)
Sets the parameter panel
Parameters:
panel - parameter panel

setResultPanel

public void setResultPanel(NamedFieldPanel panel)
Sets the result panel
Parameters:
panel - result panel

getResultPanel

public NamedFieldPanel getResultPanel()
Gets the result panel
Returns:
result panel

getControl

public com.numobjects.beans.ControlPanel getControl()
Gets control panel
Returns:
control panel

setControl

public void setControl(com.numobjects.beans.ControlPanel control)
Sets control panel
Parameters:
control - control panel