Example #1
0
 /**
  * this method removes the last element of the formula, and returns whether the element removed is
  * an operator or an input:
  *
  * @return true if it is an operator, false otherwise
  * @exception SyntaxErrorException TO COMMENT FURTHER
  */
 public boolean removeFromFormula() throws SyntaxErrorException {
   boolean isOperator = true;
   TreeCell currentCell;
   if (nextEntry == 0) // only one value
   {
     currentCell = opList.get(0);
   } else if ((nextEntry < opList.size())
       && (opList.get(nextEntry).getstatusCell() != TreeCell.NOT_COMPLETED)) {
     currentCell = opList.get(nextEntry);
   } else {
     currentCell = opList.get(nextEntry - 1); // get the current cell that we will be using
   }
   try {
     isOperator = currentCell.removeInCell();
     if (!isOperator) this.removeLeaveTotal();
     if (currentCell.getstatusCell() == TreeCell.NOT_COMPLETED) {
       if (nextEntry > 0) nextEntry--;
       if (nextEntry < opList.size()) opList.remove(currentCell);
     } else if ((currentCell.getstatusCell() == TreeCell.LEFT_COMPLETED))
       nextEntry = this.opList.size();
     else if (nextEntry < opList.size()) {
       nextEntry++;
     }
     return isOperator;
   } catch (SyntaxErrorException e) {
     throw e;
   }
 }
Example #2
0
 /**
  * Method exists to add an operator to an equation and keep the tree correct. If the cellValue has
  * multiplication or division, the method will call itself with an attempt to put in RightSon If
  * the cellValue has addition or subtraction and the rightSon has an operator, call again with
  * attempt to put in rightSon. If neither are true, create new TreeCell for rightSong, add the
  * operator to rightSon, and have the rightSon's leftSon be the previous rightSon's value
  *
  * @param op: operator to be added to the tree
  */
 public void addOpAndReform(char op) {
   if (cellValue.length() == 1) {
     if ((cellValue.charAt(0) == '*') || (cellValue.charAt(0) == '/')) {
       rightSon.addOpAndReform(op);
       status = LEFT_COMPLETED;
     } else {
       if (rightSon.cellValue.length() == 1) {
         rightSon.addOpAndReform(op);
         status = LEFT_COMPLETED;
       } else {
         String value = rightSon.cellValue;
         this.rightSon = new TreeCell();
         this.rightSon.cellValue = op + "";
         this.rightSon.leftSon = new TreeCell(value);
         this.rightSon.status = LEFT_COMPLETED;
         status = LEFT_COMPLETED;
       }
     }
   } else {
     String value = cellValue;
     this.leftSon = new TreeCell();
     this.leftSon.cellValue = value;
     this.leftSon.status = RIGHT_COMPLETED;
     this.cellValue = op + "";
     this.rightSon = null;
     status = LEFT_COMPLETED;
   }
 }
Example #3
0
 /**
  * Creates string res of leftSon's toString, its own value and status, then rightSon's toString
  *
  * @return res returns the entire mathematical equation and their status in the correct order
  */
 public String toString() {
   String res = "";
   if (this.leftSon != null) res += "{" + leftSon.toString() + "}";
   res += "{" + cellValue + ",status=" + status + "}";
   if (this.rightSon != null) res += "{" + rightSon.toString() + "}";
   return res;
 }
Example #4
0
 /**
  * Method exists to output the math equation String res has leftSon's toString, the current
  * cellValue, and the rightSon's toString
  *
  * @return res: the entire math equation in the correct order
  */
 public String turnToString() {
   String res = "";
   if (this.leftSon != null) {
     res += leftSon.turnToString();
   }
   res += " " + this.cellValue + " ";
   if (this.rightSon != null) {
     res += rightSon.turnToString();
   }
   return res;
 }
Example #5
0
  /**
   * Method that copies the content of a TreeCell given as parameter to the current formula
   *
   * @param cellCopy the main operator or first input to copy
   * @throws SyntaxErrorException the formula is not syntactically correct
   */
  public void copyTreeCell(TreeCell cellCopy) throws SyntaxErrorException {
    if (cellCopy != null) {
      if (cellCopy.getleftSon() != null) copyTreeCell(cellCopy.getleftSon());

      String cellValue = cellCopy.getcellValue(); // holds the value of the current cell

      if (cellValue != null)
        if (cellValue.length() > 1) addToFormula(cellValue);
        else addToFormula(cellValue.charAt(0));
      if (cellCopy.getrightSon() != null) copyTreeCell(cellCopy.getrightSon());
    }
  }
Example #6
0
  /**
   * This method transforms the Formula object into a String, with a similar aspect to what is
   * produced by the calculation panel when user writes in the jTextFieldArea
   */
  public String FormulaToString() {

    String returningString = "";
    if (opList != null) {
      for (int i = 0; i < opList.size(); i++) {
        TreeCell currentOp = opList.get(i);
        returningString += currentOp.turnToString();
      }
      String formulaS = returningString.replaceAll(" ", " ");
      while (formulaS.contains("  ")) formulaS = formulaS.replaceAll("  ", " ");
      returningString = formulaS;
    }
    return returningString;
  }
Example #7
0
 /**
  * This method exists to make it easier to see when and where classes are changing the formula
  * This also serves another purpose by adding spaces and formatting when needed, something that
  * the normal setFormula() method cannot do.
  *
  * @param op
  */
 public void addToFormula(char op) throws SyntaxErrorException {
   // if the cell to add the operator in does not exist yet, create it
   if ((opList == null) || (nextEntry >= opList.size())) opList.add(new TreeCell());
   TreeCell currentCell = opList.get(nextEntry); // get the current cell that we will be using
   try {
     if (((op == '/') || (op == '*')) && (nextEntry > 0)) {
       if ((opList.get(nextEntry).getleftSon() == null)
           && (opList.get(nextEntry - 1).getcellValue().length() == 1)) {
         opList.remove(nextEntry);
         nextEntry--;
         currentCell = opList.get(nextEntry);
         currentCell.addOpAndReform(op);
       } else if ((opList.get(nextEntry).getcellValue() == null)
           && (opList.get(nextEntry - 1).getcellValue().length() >= 1)) {
         // this is the first operator to be entered, the first entry being an input.
         opList.remove(nextEntry);
         nextEntry--;
         currentCell = opList.get(nextEntry);
         currentCell.setleftSon(new TreeCell(currentCell.getcellValue()));
         currentCell.setrightSon(null);
         currentCell.setcellValue(op + "");
         currentCell.setstatusCell(TreeCell.LEFT_COMPLETED);
       }
     } else {
       currentCell.addOpInCell(op);
     }
   } catch (SyntaxErrorException e) {
     throw e;
   }
 }
Example #8
0
 /**
  * Method exists to check if a passed operator is equal to the operator in the cell
  *
  * @param operator: compared to the value in the cell
  * @return true if operator is equal to the currentCell's operator, false if they are not or if
  *     cellValue is not an operator
  */
 boolean operatorInCell(char operator) {
   boolean res = false;
   if (this.cellValue.length() == 1) {
     if (cellValue.charAt(0) == operator) return true;
     else {
       res = false;
       if (this.leftSon != null) {
         res = leftSon.operatorInCell(operator);
       }
       if (!res && (this.rightSon != null)) {
         return (rightSon.operatorInCell(operator));
       }
       return res;
     }
   } else {
     return false;
   }
 }
Example #9
0
 /**
  * Method exists to find the correct TreeCell that the operation should be assigned
  *
  * @param op set to its correct location in tree, current location if a math symbol, left if no
  *     value entered, and right if one value already entered
  * @exception SyntaxErrorException : Exception thrown if -char not passed -multiplication or
  *     division occurs and there is no left value entered -addition or subtraction and there is
  *     already a left value entered
  */
 void addOpInCell(char op) throws SyntaxErrorException {
   SyntaxErrorException e = null;
   if (this.getcellValue() == null) { // if the operator is null (default)
     // fill in the operator
     this.setcellValue(op + "");
     if ((op == '/') || (op == '*')) {
       if (this.getleftSon() == null) {
         Logger.getLogger()
             .concatOut(
                 amt.log.Logger.DEBUG,
                 "Formula",
                 "SyntaxErrorException: addition of operator "
                     + op
                     + " where the left input has not been given");
         e.fillInStackTrace();
         throw e;
       } else {
         this.status = LEFT_COMPLETED;
       }
     } else {
       if (this.getleftSon() != null) {
         Logger.getLogger()
             .concatOut(
                 amt.log.Logger.DEBUG,
                 "Formula",
                 "SyntaxErrorException: addition of operator "
                     + op
                     + " where the left subtree already has something in it");
         e.fillInStackTrace();
         throw e;
       } else this.status = LEFT_COMPLETED;
     }
   } else {
     if (status == NOT_COMPLETED) leftSon.addOpInCell(op);
     else if (status == LEFT_COMPLETED) rightSon.addOpInCell(op);
     else {
       // this cell becomes the left part of the new cell
       this.setleftSon(new TreeCell(this.cellValue));
       this.status = LEFT_COMPLETED;
       this.setcellValue(op + "");
     }
   }
 }
Example #10
0
 private void swapWithInputs() {
   if (rightSon != null) {
     rightSon.swapWithInputs();
     if (rightSon.status == RIGHT_COMPLETED) {
       this.status = RIGHT_COMPLETED;
     }
   } else {
     cellValue = leftSon.cellValue;
     leftSon = null;
     rightSon = null;
     status = RIGHT_COMPLETED;
   }
 }
Example #11
0
  /**
   * This method exists to make it easier to see when and where classes are changing the formula
   * This also serves another purpose by adding spaces and formatting when needed, something that
   * the normal setFormula() method cannot do.
   *
   * @param toAdd the information to add to the Formula : either operator or inputs
   * @exception SyntaxErrorException: this exception is thrown if the method is called trying to add
   *     an input where the last entry in the formula is an input already, or an operator following
   *     another operator. it also throws the exception if the first operator added is * or / and no
   *     inputs have been entered before. those are all errors that are dealt with in the AMT code,
   *     and the method should never be called in those instances.
   */
  public void addToFormula(String toAdd) throws SyntaxErrorException {
    if (toAdd.length() == 1) {
      addToFormula(toAdd.charAt(0));
      return;
    }
    // if the cell to add the operator in does not exist yet, create it
    if ((opList == null) || (nextEntry >= opList.size())) opList.add(new TreeCell());

    TreeCell currentCell = opList.get(nextEntry); // get the current cell that we will be using
    try {
      currentCell.addInputInCell(toAdd);
      if ((currentCell.getstatusCell() == TreeCell.RIGHT_COMPLETED)
          && ((currentCell.getcellValue().length() > 1))) nextEntry++;
      if ((currentCell.getstatusCell() == TreeCell.RIGHT_COMPLETED)
          && ((currentCell.getcellValue().length() == 1))
          && (currentCell.getrightSon() != null)) nextEntry++;
      this.addLeaveTotal();
    } catch (SyntaxErrorException e) {
      throw e;
    }
  }
Example #12
0
  /**
   * Method exists to remove the value in the Cell
   *
   * @exception SytaxErrorException: thrown if -the cell's status is NOT_COMPLETED -the cell is
   *     RIGHT_COMPLETED, the right is null, and the cellValue is a math operator
   * @returns true if a value can be removed, or false if a math operator is in the cell
   */
  public boolean removeInCell() throws SyntaxErrorException {

    SyntaxErrorException e = null;

    switch (status) {
      case NOT_COMPLETED:
        Logger.getLogger()
            .concatOut(
                amt.log.Logger.DEBUG,
                "Formula",
                "SyntaxErrorException: try to remove a cell which is void, method fails");
        e.fillInStackTrace();
        throw e;
      case LEFT_COMPLETED:
        if (this.getleftSon() == null) {
          // we are in a cell that only has either an operator + or -, or an input
          if (this.cellValue.length() > 1) {
            // the element to remove is an input
            this.cellValue = null;
            status = NOT_COMPLETED;
            return false;
          } else if ((this.cellValue.charAt(0) != '*') && (this.cellValue.charAt(0) != '/')) {
            // there might be something else in the right son that needs to be removed.
            if (rightSon == null) {
              // the element to remove is an operator
              this.cellValue = null;
              status = NOT_COMPLETED;
              return true;
            } else {
              TreeCell currentCell = rightSon;
              boolean res = currentCell.removeInCell();
              if (currentCell.getstatusCell() == NOT_COMPLETED) {
                rightSon = null;
                this.status = LEFT_COMPLETED;
              }
              return res;
            }
          } else {
            this.cellValue = null;
            status = NOT_COMPLETED;
            return true;
          }
        } else if (this.rightSon != null) {
          swapWithInputs();
          return true;
        } else {
          // the element to remove is an operator, and there is something in the left-son, the cell
          // is taken away and the content of the son is put one up
          swapWithLeftSon();
          return true;
        }
      case RIGHT_COMPLETED:
        if (this.getrightSon() == null) {
          // this should be a leaf, containning an input only
          if (this.cellValue.length() > 1) {
            // the element to remove is an input
            this.cellValue = null;
            status = NOT_COMPLETED;
            return false;
          } else {
            Logger.getLogger()
                .concatOut(
                    amt.log.Logger.DEBUG,
                    "Formula",
                    "SyntaxErrorException: try to remove an input in an operator "
                        + this.cellValue
                        + " cell");
            e.fillInStackTrace();
            throw e;
          }
        } else {
          boolean res = this.rightSon.removeInCell();
          if (rightSon.status == NOT_COMPLETED) {
            rightSon = null;
          }
          this.status = LEFT_COMPLETED;
          return res;
        }
    }
    return false;
  }
Example #13
0
 /**
  * method will set the current cell to all the values of the leftSon, causing the leftSon to
  * replace the currentCell
  */
 public void swapWithLeftSon() {
   setrightSon(leftSon.getrightSon());
   setcellValue(leftSon.getcellValue());
   setstatusCell(leftSon.getstatusCell());
   setleftSon(leftSon.getleftSon());
 }
Example #14
0
  /*
   * Checks if formula made matches a passed formula
   * @param correctFormula: The formula to be compared
   */
  public boolean checkFormulaCorrect(Formula correctFormula) {
    Formula goodF, userF;
    try {
      goodF = new Formula(correctFormula);
      userF = new Formula(this);
      if (goodF.opList.size() > 0) {
        TreeCell firstCell = goodF.opList.get(0);
        if ((firstCell.getcellValue().length() > 1)
            || (firstCell.getcellValue().charAt(0) == '*')
            || (firstCell.getcellValue().charAt(0) == '/')) {

          TreeCell topCell = new TreeCell("+");
          topCell.setleftSon(null);
          topCell.setrightSon(firstCell);
          if (firstCell.getstatusCell() == TreeCell.RIGHT_COMPLETED)
            topCell.setstatusCell(TreeCell.RIGHT_COMPLETED);
          else topCell.setstatusCell(TreeCell.LEFT_COMPLETED);
          goodF.opList.set(0, topCell);
        }
      }

      if (userF.opList.size() > 0) {
        TreeCell firstCell = userF.opList.get(0);
        if ((firstCell.getcellValue().length() > 1)
            || (firstCell.getcellValue().charAt(0) == '*')
            || (firstCell.getcellValue().charAt(0) == '/')) {

          TreeCell topCell = new TreeCell("+");
          topCell.setleftSon(null);
          topCell.setrightSon(firstCell);
          if (firstCell.getstatusCell() == TreeCell.RIGHT_COMPLETED)
            topCell.setstatusCell(TreeCell.RIGHT_COMPLETED);
          else topCell.setstatusCell(TreeCell.LEFT_COMPLETED);
          userF.opList.set(0, topCell);
        }
      }
      /*System.out.println("the formulas after init");
      System.out.println(userF.FormulaToString());
      System.out.println(goodF.FormulaToString());*/
      boolean opChecked[] = new boolean[userF.nextEntry];

      boolean opCheckedCorrect = false;
      // compare the number of leaves: if not identical we know it is not good.
      if (userF.nb_leaves != goodF.nb_leaves) {
        // System.out.println("nbLeaves not equal");
        return false;
      }
      // if the two formulas do not have the same number of top operators, we know it is not good.
      else if (userF.opList.size() != goodF.opList.size()) {
        // System.out.println("size of opList not equal");
        return false;
      } else {
        // System.out.println("first tests passed");

        for (int i = 0; i < goodF.opList.size(); i++) {
          String correctvalue = goodF.opList.get(i).getcellValue();
          // System.out.println("test of operator: "+ correctvalue);
          if (correctvalue.length() == 1) {
            opCheckedCorrect = false;
            for (int j = 0; j < userF.opList.size(); j++) {
              if ((!opCheckedCorrect) && (!opChecked[j])) {
                String currentValue = userF.opList.get(j).getcellValue();
                // System.out.println("test of user operator: "+ currentValue+ "" + j);

                if (correctvalue.equals(currentValue)) {
                  // System.out.println("test of the cells:");
                  // System.out.println("user"+ userF.opList.get(j).turnToString());
                  // System.out.println("user"+ goodF.opList.get(i).turnToString());
                  if (userF.opList.get(j).cellCorrect(goodF.opList.get(i))) {
                    // System.out.println("this cell is correct");
                    opChecked[j] = true;
                    opCheckedCorrect = true;
                  }
                }
              }
            }
            if (!opCheckedCorrect) return false;
          }
        }
        return true;
      }

    } catch (SyntaxErrorException ex) {
      java.util.logging.Logger.getLogger(Formula.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
  }