Package sunlabs.brazil.util
Class Calculator
- java.lang.Object
-
- sunlabs.brazil.util.Calculator
-
public class Calculator extends java.lang.ObjectCalculator implements a simple arithmetic expression evaluator. It can evaluate typical expressions with the "normal" operators and precedence. Formally, the BNF for the supported grammar is:A <letter> is defined as a Java<stmt> ::= <var> = <expr> | <expr> <expr> ::= <rexpr> | <expr> <bool op> <rexpr> <bool op> ::= && | <or> <or> ::= || <rexpr> ::= <aexpr> | <rexpr> <rel op> <aexpr> <rel op> ::= < | <= | > | >= | == | != <aexpr> ::= <term> | <aexpr> <add op> <term> <add op> ::= + | - <term> ::= <factor> | <term> <mult op> <factor> <mult op> ::= * | / | % <factor> ::= <var> | <num> | ! <factor> | ( <expr> ) <var> ::= <letter> | <var> <var2> <var2> ::= <letterordigit> | . | _ <num> ::= <unum> | + <unum> | - <unum> <unum> ::= <int> | <int> . | <int> . <int> | . <int> <int> ::= <digit> | <int> <digit>
charfor whichChar.isLetter(char)istrue. A <letterordigit> is defined as a Javacharfor whichChar.isLetterOrDigit(char)istrue. A digit is defined as a Javacharfor whichChar.isDigit(char)istrue.Values for
<var>are looked up in the suppliedDictionary. If<var>can not be found, it is assumed to have the value zero. If the value found is "true" or "yes" (case insensitive), it is assumed to be one. Similarly, if the value found is "false" or "no", it is assumed to be zero. Assignment to<var>stores the computed value in the sameDictionary.The period in
<unum>, if there is one, must be immediately adjacent to surrounding<int>s.- Version:
- 2.4
- Author:
- Steve Drach <drach@sun.com>
-
-
Field Summary
Fields Modifier and Type Field Description booleandebuggingSettruefor debug output.
-
Constructor Summary
Constructors Constructor Description Calculator()The no argument constructor will create an internalHashtablein which it looks up and stores values associated with variables.Calculator(java.util.Dictionary d)This constructor will use theDictionaryparameter to lookup and store values associated with variables.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetValue(java.lang.String stmt)Computes the value of the statement passed in the parameter string and returns a string representation of the result.java.lang.StringgetValue(java.lang.String stmt, java.util.Dictionary d)Computes the value of the statement passed in the parameter string and returns a string representation of the result.static voidmain(java.lang.String[] args)A test driver for the calculator.voidstringsValid(boolean allStringsValid)Normally, variables whose values are "on", "yes", or "true" and converted to "1.0", while the values "off", "no", and "false" are converted to "0.0".
-
-
-
Constructor Detail
-
Calculator
public Calculator()
The no argument constructor will create an internalHashtablein which it looks up and stores values associated with variables.- See Also:
Hashtable
-
Calculator
public Calculator(java.util.Dictionary d)
This constructor will use theDictionaryparameter to lookup and store values associated with variables.- Parameters:
d- theDictionaryobject that serves as a symbol table- See Also:
Dictionary
-
-
Method Detail
-
stringsValid
public void stringsValid(boolean allStringsValid)
Normally, variables whose values are "on", "yes", or "true" and converted to "1.0", while the values "off", "no", and "false" are converted to "0.0". All other values are considered an error. By passing "true", all normally invalid strings are given a value of "1.0".
-
getValue
public java.lang.String getValue(java.lang.String stmt) throws java.lang.ArithmeticExceptionComputes the value of the statement passed in the parameter string and returns a string representation of the result. If the input statement consists only of a variable name and the result of the computation is zero,nullis returned.- Parameters:
stmt- a string representation of an arithmetic expression or assignment- Returns:
- a string representation of
the computed result or
null - Throws:
java.lang.ArithmeticException- occurs when a result is improper (e.g. infinity) or when the input statement can not be parsed
-
getValue
public java.lang.String getValue(java.lang.String stmt, java.util.Dictionary d) throws java.lang.ArithmeticExceptionComputes the value of the statement passed in the parameter string and returns a string representation of the result. If the input statement consists only of a variable name and the result of the computation is zero,nullis returned. The second parameter is used as a symbol table for the duration of this method call. Note this method is not thread safe!- Parameters:
stmt- a string representation of an arithmetic expression or assignmentd- the temporary symbol table- Returns:
- a string representation of
the computed result or
null - Throws:
java.lang.ArithmeticException- occurs when a result is improper (e.g. infinity) or when the input statement can not be parsed
-
main
public static void main(java.lang.String[] args)
A test driver for the calculator. Type in arithmetic expressions or assignments and see the results. Use "dump" to see contents of all assigned variables.- Parameters:
args- required signature formainmethod, not used
-
-