Java Reference
In-Depth Information
Table 12.1
Arithmetic Notations
Notation
Description
Examples
infix
operator between operands
2.3 + 4.7
2.6 * 3.7
(3.4 + 7.9) * 18.6 + 2.3 / 4.7
prefix
operator before operands
+ 2.3 4.7
(functional notation)
* 2.6 3.7
+ * + 3.4 7.9 18.6 / 2.3 4.7
postfix
operator after operands
2.3 4.7 +
(reverse Polish notation)
2.6 3.7 *
3.4 7.9 + 18.6 * 2.3 4.7 / +
from a Scanner and computes its value. Our method will look like this:
// pre : input contains a legal prefix expression
// post: expression is consumed and the result is returned
public static double evaluate(Scanner input) {
...
}
Before we can begin writing the method, we have to consider the kind of input that
we are going to get. As the precondition indicates, we will assume that the Scanner con-
tains a legal prefix expression. The simplest possible expression would be a number like
38.9
There isn't much to evaluate in this case—we can simply read and return the num-
ber. More complex prefix expressions will involve one or more operators. Remember
that the operator goes in front of the operands in a prefix expression. A slightly more
complex expression would be two numbers as operands with an operator in front:
+ 2.6 3.7
This expression could itself be an operand in a larger expression. For example, we
might ask for
* + 2.6 3.7 + 5.2 18.7
At the outermost level, we have a multiplication operator with two operands:
* + 2.6 3.7 + 5.2 18.7
operator
operand #1
operand #2
 
Search WWH ::




Custom Search