Class Operator
java.lang.Object
|
+--Operator
- Direct Known Subclasses:
- AdditionOperator, DivisionOperator, MultiplicationOperator, NegationOperator, SineOperator, SubtractionOperator
- public abstract class Operator
- extends java.lang.Object
Class representing the concept of an operator in an equation. For example, in
X + 3 = Y X, 3 and Y are operands representing values whereas + and = are operators
representing actions to be performed on the operands.
Field Summary |
int |
numberOperands
The number of operands that the operator acts on. |
int |
precedence
The precedence of the operator. |
boolean |
preceededByAnOperand
Flag allowing for the contextualization of operators. |
java.lang.String |
token
The token representing the operation in an equation. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
token
public java.lang.String token
- The token representing the operation in an equation.
precedence
public int precedence
- The precedence of the operator. In equations different operators can have
an order of operations defined; for instance in normal arithmetic operations
there are the basic operators + (addition), - (subtraction), * (multiplication),
and / (division). There is an order of operations which states that all
multiplication and division is performed before any addition or subtraction.
This is ignoring the concept of grouping symbols such as parenthesis.
All multipliaction and division is performed left to right and then all addition
and subtraction is performed left to right. This could be expanded to a more
operators; each time the highest order of precedence is performed left to
right until none of that operator remain, then the same is repeated for the next
lower precedence until eventually there is but a single value left.
numberOperands
public int numberOperands
- The number of operands that the operator acts on.
preceededByAnOperand
public boolean preceededByAnOperand
- Flag allowing for the contextualization of operators. It is the case sometimes
that in an equation the same symbol may represent more than one operator.
For instance in arithmetic the - represents both subtration 3 - 5 and negation
-2. Which action is intended by the writer is known by the reader of the equation
based upon the context where it appears. In 3 - 5 = -2 it is known that the first
- is a minus because it is preceeded by 3 and the second is negation becasue it
is preceeded by a =.
Operator
public Operator()
operate
public abstract int operate(int[] x)
- Abstact method that all operators must implement. This represents the operator
acting upon a series of operands.
- Returns:
- the new operand produced by the operation
isHigherPrecedence
public boolean isHigherPrecedence(Operator o)
- Parameters:
o
- an operator to be compared to this one- Returns:
true
if this operator is of higher precedence
isLowerPrecedence
public boolean isLowerPrecedence(Operator o)
- Parameters:
o
- an operator to be compared to this one- Returns:
true
if this operator is of lower precedence
toString
public java.lang.String toString()
- Returns:
- the token for the current operator
- Overrides:
- toString in class java.lang.Object