Class MBracket


public class MBracket extends MOperator
  • Field Details

    • name

      private String name
      the name of the bracket i.e "(" or ")"
    • index

      private int index
      The index of the bracket in the ArrayList containing the scanned function
    • complement

      private MBracket complement
      objects of this class keep a record of their counterpart or complementing bracket.
    • evaluated

      private boolean evaluated
      Return true if the contents of the bracket have been evaluated
  • Constructor Details

    • MBracket

      public MBracket(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

      public static MBracket createTwinBracket(MBracket 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. 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 MBracket objects. If we create another MBracket array, say array moreBracs and copy the objects in bracs into moreBracs.Now, both bracs and moreBracs will hold references to these MBracket 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 MBracket object that manifests exactly the same attributes as brac but is a distinct object from brac.
    • createTwinBracket

      public MBracket createTwinBracket()
      non-static version of the above method. This one creates a twin for this MBracket object. The one above creates a twin for the specified bracket object.
      Returns:
      a MBracket 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 MBracket object in a scanned function
    • setIndex

      public void setIndex(int index)
      Parameters:
      index - the ne w index to assign to this MBracket object in a scanned Function
    • getName

      public String getName()
      Overrides:
      getName in class MOperator
      Returns:
      the name of this MBracket either ( or )
    • setName

      public void setName(String name)
      Overrides:
      setName in class MOperator
      Parameters:
      name - sets the name of this bracket to either ( or )
    • getComplement

      public MBracket getComplement()
      Returns:
      the MBracket object which is the complement of this MBracket object
    • setComplement

      public void setComplement(MBracket complement)
      Parameters:
      complement - sets the MBracket object which is to be the complement to this one in the scanned Function
    • isComplement

      public boolean isComplement(MBracket brac)
      checks if the MBracket object argument below is the sane as the complement to this MBracket object.
      Parameters:
      brac - The MBracket object whose identity is to be checked whether or not it complements this MBracket object.
      Returns:
      true if the parameter is the complement to this one.
    • encloses

      public boolean encloses(MBracket brac)
      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

      public int getNumberOfInternalBrackets(ArrayList<MBracket> brac)
      Parameters:
      brac - an ArrayList object containing all brackets found in a function
      Returns:
      the number of bracket pairs contained between this MBracket object and its complement
    • getComplementIndex

      public static int getComplementIndex(boolean isOpenBracket, int start, ArrayList<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
    • isOpenBracket

      public static boolean isOpenBracket(String bracket)
      Parameters:
      bracket - The String object.
      Returns:
      true if the String object represents an open bracket
    • isCloseBracket

      public static boolean isCloseBracket(String bracket)
      Parameters:
      bracket - The String object.
      Returns:
      true if the String object represents a close bracket
    • getBracketDomainContents

      public ArrayList<String> getBracketDomainContents(ArrayList<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.