public static Node randomlyAddParenthesis(Expression ex,int minNumParens,int maxNumParens){
  Vector<Node> terms=new Vector<>();
  if (ex.getOperator() instanceof Operator.Addition) {
    terms=ex.splitOnAddition();
    int numParensToAdd=randomInt(minNumParens,maxNumParens,true);
    int unusedTermsLeft=terms.size();
    int tempIndex;
    Expression tempEx;
    while (unusedTermsLeft > 2 && numParensToAdd > 0) {
      tempIndex=randomInt(0,terms.size() - 2,false);
      tempEx=new Expression(new Operator.Addition(),terms.remove(tempIndex),terms.remove(tempIndex));
      tempEx.setDisplayParentheses(true);
      terms.add(tempEx);
      unusedTermsLeft--;
      numParensToAdd--;
    }
    return Expression.staggerAddition(terms);
  }
 else   if (ex.getOperator() instanceof Operator.Multiplication) {
    terms=ex.splitOnMultiplication();
    int numParensToAdd=randomInt(minNumParens,maxNumParens,true);
    int unusedTermsLeft=terms.size();
    int tempIndex;
    Expression tempEx;
    while (unusedTermsLeft > 2 && numParensToAdd > 0) {
      tempIndex=randomInt(0,terms.size() - 2,false);
      tempEx=new Expression(new Operator.Multiplication(),terms.remove(tempIndex),terms.remove(tempIndex));
      tempEx.setDisplayParentheses(true);
      terms.add(tempEx);
      unusedTermsLeft--;
      numParensToAdd--;
    }
    return Expression.staggerMultiplication(terms);
  }
  return ex;
}

public static Node randomlyAddParenthesis ( Expression ex , int minNumParens , int maxNumParens ) { Vector < Node > terms = new Vector < > ( ) ; if ( ex . getOperator ( ) instanceof Operator . Addition ) { terms = ex . splitOnAddition ( ) ; int numParensToAdd = randomInt ( minNumParens , maxNumParens , true ) ; int unusedTermsLeft = terms . size ( ) ; int tempIndex ; Expression tempEx ; while ( unusedTermsLeft > 2 && numParensToAdd > 0 ) { tempIndex = randomInt ( 0 , terms . size ( ) - 2 , false ) ; tempEx = new Expression ( new Operator . Addition ( ) , terms . remove ( tempIndex ) , terms . remove ( tempIndex ) ) ; tempEx . setDisplayParentheses ( true ) ; terms . add ( tempEx ) ; unusedTermsLeft -- ; numParensToAdd -- ; } return Expression . staggerAddition ( terms ) ; } else if ( ex . getOperator ( ) instanceof Operator . Multiplication ) { terms = ex . splitOnMultiplication ( ) ; int numParensToAdd = randomInt ( minNumParens , maxNumParens , true ) ; int unusedTermsLeft = terms . size ( ) ; int tempIndex ; Expression tempEx ; while ( unusedTermsLeft > 2 && numParensToAdd > 0 ) { tempIndex = randomInt ( 0 , terms . size ( ) - 2 , false ) ; tempEx = new Expression ( new Operator . Multiplication ( ) , terms . remove ( tempIndex ) , terms . remove ( tempIndex ) ) ; tempEx . setDisplayParentheses ( true ) ; terms . add ( tempEx ) ; unusedTermsLeft -- ; numParensToAdd -- ; } return Expression . staggerMultiplication ( terms ) ; } return ex ; }
randomly adds parenthesis to an expression composed of additions or multiplications . the result of the operation does not change , due to the associative property of both of the operators , but this method is useful for creating exercises to teach those concepts .
