Class Operation
java.lang.Object
calculator.operations.Operation
- All Implemented Interfaces:
Expression
Operation is an abstract class that represents arithmetic operations,
which are a special kind of Expressions, just like numbers are.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe list of expressions passed as an argument to the arithmetic operationprotected intThe neutral element of the operation (e.g. 1 for *, 0 for +)protected StringThe character used to represent the arithmetic operation (e.g. "+", "*") -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedOperation(List<Expression> elist) To construct an operation with a list of expressions as arguments, as well as the Notation used to represent the operation. -
Method Summary
Modifier and TypeMethodDescriptionvoidAccept method to implement the visitor design pattern to traverse arithmetic expressions.voidaddMoreParams(List<Expression> params) Add more parameters to the existing list of parametersbooleanTwo operation objects are equal if their list of arguments is equal and they correspond to the same operation.getArgs()getter method to return the number of arguments of an arithmetic operation.getter method to return the symbol of the arithmetic operation.inthashCode()The method hashCode needs to be overridden it the equals method is overridden; otherwise there may be problems when you use your object in hashed collections such as HashMap, HashSet, LinkedHashSet.abstract ComplexAbstract method representing the actual binary arithmetic operation to compute on Complexesabstract Atomop(IntegerAtom i1, IntegerAtom i2) Abstract method representing the actual binary arithmetic operation to compute on Integerabstract AtomAbstract method representing the actual binary arithmetic operation to compute on Rationnalsabstract RealAbstract method representing the actual binary arithmetic operation to compute on Realsfinal StringtoString()Convert the arithmetic operation into a String to allow it to be printed, using the default printerfinal StringAppeal to the visitor to convert the arithmetic operation into a String to allow it to be printed, using the notation n (prefix, infix or postfix) that is specified as a parameter.
-
Field Details
-
args
The list of expressions passed as an argument to the arithmetic operation -
symbol
The character used to represent the arithmetic operation (e.g. "+", "*") -
neutral
protected int neutralThe neutral element of the operation (e.g. 1 for *, 0 for +)
-
-
Constructor Details
-
Operation
To construct an operation with a list of expressions as arguments, as well as the Notation used to represent the operation.- Parameters:
elist- The list of expressions passed as argument to the arithmetic operation- Throws:
IllegalConstruction- Exception thrown if a null list of expressions is passed as argument
-
-
Method Details
-
getArgs
getter method to return the number of arguments of an arithmetic operation.- Returns:
- The number of arguments of the arithmetic operation.
-
getSymbol
getter method to return the symbol of the arithmetic operation.- Returns:
- The symbol of the arithmetic operation (e.g. "+", "-", "*", "/").
-
op
-
op
-
op
Abstract method representing the actual binary arithmetic operation to compute on Integer- Parameters:
i1- first Integer of the binary operationi2- second Integer of the binary operation- Returns:
- result of computing the binary operation
-
op
-
addMoreParams
Add more parameters to the existing list of parameters- Parameters:
params- The list of parameters to be added
-
accept
Accept method to implement the visitor design pattern to traverse arithmetic expressions. Each operation will delegate the visitor to each of its arguments expressions, and will then pass itself to the visitor object to get processed by the visitor object.- Specified by:
acceptin interfaceExpression- Parameters:
v- The visitor object
-
toString
-
toString
Appeal to the visitor to convert the arithmetic operation into a String to allow it to be printed, using the notation n (prefix, infix or postfix) that is specified as a parameter.- Parameters:
n- The notation to be used for representing the operation (prefix, infix or postfix)- Returns:
- The String that is the result of the conversion.
-
equals
-
hashCode
-