Package parser
Class Bracket
java.lang.Object
parser.Operator
parser.Bracket
public class Bracket extends Operator
- Author:
- GBENRO
-
Field Summary
Fields inherited from class parser.Operator
AND, ASSIGN, AT, CLOSE_CIRC_BRAC, CLOSE_SQUARE_BRAC, COLON, COMBINATION, COMMA, CONST, CUBE, CUBE_ROOT, DIVIDE, EN_DASH, EQUALS, EXIT, FACTORIAL, GREATER_OR_EQUALS, GREATER_THAN, INVERSE, LESS_OR_EQUALS, LESS_THAN, MINUS, MULTIPLY, OPEN_CIRC_BRAC, OPEN_SQUARE_BRAC, operators, OR, PERMUTATION, PLUS, POWER, REMAINDER, ROOT, SEMI_COLON, SPACE, SQUARE, STORE -
Constructor Summary
Constructors Constructor Description Bracket(java.lang.String op)Constructor of this class for creating its objects and initializing their names with either a ( or a ) and initial -
Method Summary
Modifier and Type Method Description static booleancheckBracketStructure(java.util.List<java.lang.String> list, int start, int end)BracketcreateTwinBracket()non-static version of the above method.static BracketcreateTwinBracket(Bracket brac)Used to create similar objects that are not equal The object created by this class is similar to the parameter because it contains the same data as the parameter.java.lang.StringdomainTokenAt(java.util.List<java.lang.String> scanner, int index)booleanencloses(Bracket brac)java.util.List<java.lang.String>getBracketDomainContents(java.util.ArrayList<java.lang.String> scan)returns a List containing the contents of a bracket pair,including the bracket pair itself.BracketgetComplement()static intgetComplementIndex(boolean isOpenBracket, int start, java.lang.String expr)static intgetComplementIndex(boolean isOpenBracket, int start, java.util.ArrayList<java.lang.String> scan)static intgetComplementIndex(boolean isOpenBracket, int start, java.util.List<java.lang.String> scan)java.lang.StringgetDomainContents(java.util.ArrayList<java.lang.String> scan)intgetIndex()java.lang.StringgetName()intgetNumberOfInternalBrackets(java.util.ArrayList<Bracket> brac)static booleanhasBracketsInRange(java.util.List<java.lang.String> list, int start, int end)static booleanisCloseBracket(java.lang.String bracket)booleanisComplement(Bracket brac)checks if the Bracket object argument below is the sane as the complement to this Bracket object.booleanisEvaluated()static booleanisOpenBracket(java.lang.String bracket)booleanisSBP(java.util.ArrayList<java.lang.String> scan)voidmultiplyContentsByMinusOne(java.util.List<java.lang.String> scanner)voidsetComplement(Bracket complement)voidsetEvaluated(boolean evaluated)voidsetIndex(int index)voidsetName(java.lang.String name)booleansimpleBracketPairHasVariables(java.util.ArrayList<java.lang.String> scan)Methods inherited from class parser.Operator
getPrecedence, isAssignmentOperator, isAtOperator, isBinaryOperator, isBracket, isClosingBrace, isClosingBracket, isColon, isComma, isConstantStoreCommand, isCube, isCubeRoot, isEqualsOperator, isExitCommand, isFactorial, isInverse, isLogicOperator, isMulOrDiv, isMulOrDivOrRemOrPermOrCombOrPow, isOpeningBrace, isOpeningBracket, isOperatorString, isPermOrComb, isPlusOrMinus, isPower, isRemainder, isSemiColon, isSquare, isSquareRoot, isStoreCommand, isUnaryPostOperator, isUnaryPreOperator, validateAll
-
Constructor Details
-
Bracket
public Bracket(java.lang.String op)Constructor of this class for creating its objects and initializing their names with either a ( or a ) and initial- Parameters:
op-
-
-
Method Details
-
setEvaluated
public void setEvaluated(boolean evaluated)- Parameters:
evaluated- set whether or not this bracket's contents have been evaluated
-
isEvaluated
public boolean isEvaluated()- Returns:
- true if this bracket's contents have been evaluated
-
createTwinBracket
Used to create similar objects that are not equal The object created by this class is similar to the parameter because it contains the same data as the parameter. However,its address in memory is different because it refers to an entirely different object of the same class,but having similar attributes. How can this method be of any use? Imagine an Array of Brackets say array bracs filled with Bracket objects. If we create another Bracket array, say array moreBracs and copy the objects in bracs into moreBracs.Now, both bracs and moreBracs will hold references to these Bracket objects in memory.Java will not create new, similar objects at another address in memory and store in the new array. The command was most likely moreBracs=bracs; or in a loop, it would look like: for(int i=0;i<bracs.length;i++){ moreBracs=bracs[i]; } These statements will only ensure that both arrays will hold a reference to the same objects in memory,i.e RAM. Hence whenever an unsuspecting coder modifies the contents of bracs, thinking He/She has a backup in moreBracs,Java is effecting the modification on the objects referred to by moreBracs, too.This can cause a serious logical error in applications. To stop this, we use this method in this way: for(int i=0;i<bracs.length;i++){ moreBracs[i]=createTwinBracket(bracs[i]); } Note that this can be applied to all storage objects too e.g Collection objects and so on.- Parameters:
brac- The object whose twin we wish to create.- Returns:
- a Bracket object that manifests exactly the same attributes as brac but is a distinct object from brac.
-
createTwinBracket
non-static version of the above method. This one creates a twin for this Bracket object. The one above creates a twin for the specified bracket object.- Returns:
- a Bracket object that manifests exactly the same attributes as brac but is a distinct object from brac.
-
getIndex
public int getIndex()- Returns:
- the index of this Bracket object in a scanned function
-
setIndex
public void setIndex(int index)- Parameters:
index- the ne w index to assign to this Bracket object in a scanned Function
-
getName
public java.lang.String getName() -
setName
public void setName(java.lang.String name) -
getComplement
- Returns:
- the Bracket object which is the complement of this Bracket object
-
setComplement
- Parameters:
complement- sets the Bracket object which is to be the complement to this one in the scanned Function
-
isComplement
checks if the Bracket object argument below is the sane as the complement to this Bracket object.- Parameters:
brac- The Bracket object whose identity is to be checked whether or not it complements this Bracket object.- Returns:
- true if the parameter is the complement to this one.
-
encloses
- Parameters:
brac- the bracket to be checked if or not it is enclosed by this bracket and its complement.- Returns:
- true if the bracket is enclosed by this bracket and its counterpart.
-
getNumberOfInternalBrackets
- Parameters:
brac- an ArrayList object containing all brackets found in a function- Returns:
- the number of bracket pairs contained between this Bracket object and its complement
-
isSBP
public boolean isSBP(java.util.ArrayList<java.lang.String> scan)- Parameters:
scan- The ArrayList object containing the scanned function.- Returns:
- true if this Bracket object forms with its complement, a single bracket pair that is a bracket pair containing no other bracket pairs.
-
getComplementIndex
public static int getComplementIndex(boolean isOpenBracket, int start, java.util.ArrayList<java.lang.String> scan)- Parameters:
isOpenBracket- boolean variable that should be true if this bracket object whose complement we seek is an opening bracket i.e (, and should be set to false if this bracket object whose complement we seek is a closing bracket i.e )start- the index of the given bracket.scan- the ArrayList containing the scanned function.- Returns:
- the index of the enclosing or complement bracket of this bracket object
-
getComplementIndex
public static int getComplementIndex(boolean isOpenBracket, int start, java.util.List<java.lang.String> scan)- Parameters:
isOpenBracket- boolean variable that should be true if this bracket object whose complement we seek is an opening bracket i.e (, and should be set to false if this bracket object whose complement we seek is a closing bracket i.e )start- the index of the given bracket.scan- the ArrayList containing the scanned function.- Returns:
- the index of the enclosing or complement bracket of this bracket object
-
getComplementIndex
public static int getComplementIndex(boolean isOpenBracket, int start, java.lang.String expr)- Parameters:
isOpenBracket- boolean variable that should be true if this bracket object whose complement we seek is an opening bracket i.e (, and should be set to false if this bracket object whose complement we seek is a closing bracket i.e )start- the index of the given bracket.expr- the function string containing the brackets.- Returns:
- the index of the enclosing or complement bracket of this bracket object
-
checkBracketStructure
public static boolean checkBracketStructure(java.util.List<java.lang.String> list, int start, int end)- Parameters:
list- The list containing the scanned math expression.start- The point in the list where this algorithm should start checking the bracket syntax.(inclusive)end- The point in the list where this algorithm should stop checking the bracket syntax.(inclusive)- Returns:
- true if the bracket syntax of the scanned expression in the given range is valid or the expression in the given range is devoid of brackets.
-
hasBracketsInRange
public static boolean hasBracketsInRange(java.util.List<java.lang.String> list, int start, int end)- Parameters:
list- The list containing the scanned math expression.start- The point in the list where this algorithm should start checking for brackets.(inclusive)end- The point in the list where this algorithm should stop checking for brackets.(inclusive)- Returns:
- true if the scanned expression contains no brackets in the given range.
-
isOpenBracket
public static boolean isOpenBracket(java.lang.String bracket)- Parameters:
bracket- The String object.- Returns:
- true if the String object represents an open bracket
-
isCloseBracket
public static boolean isCloseBracket(java.lang.String bracket)- Parameters:
bracket- The String object.- Returns:
- true if the String object represents a close bracket
-
simpleBracketPairHasVariables
public boolean simpleBracketPairHasVariables(java.util.ArrayList<java.lang.String> scan)- Parameters:
scan- The ArrayList containing the scanned function inside which this Bracket exists.- Returns:
- true if between this Bracket and its complement, a Variable object is found.
-
getDomainContents
public java.lang.String getDomainContents(java.util.ArrayList<java.lang.String> scan)- Parameters:
scan- The ArrayList object containing the scanned function.- Returns:
- The contents of this bracket and its complement as a string, the bracket and its complement are also returned. e.g in 5+(2+3-sin2).. This method will return (2+3-sin2).
-
getBracketDomainContents
public java.util.List<java.lang.String> getBracketDomainContents(java.util.ArrayList<java.lang.String> scan)returns a List containing the contents of a bracket pair,including the bracket pair itself.- Parameters:
scan- the ArrayList containing the scanner output for a Function- Returns:
- the bracket pair and its contents.
-
multiplyContentsByMinusOne
public void multiplyContentsByMinusOne(java.util.List<java.lang.String> scanner)- Parameters:
scanner- The ArrayList containing the scanner output for a Function Multiplies the contents of this List by -1.
-
domainTokenAt
public java.lang.String domainTokenAt(java.util.List<java.lang.String> scanner, int index)- Parameters:
scanner- The ArrayList containing the scanner output for a Functionindex- The index at which the token is to be retrieved. The first and elements are compulsorily always an open bracket and a close bracket respectively.
-