/** * 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; } }
/** * 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; } }
/** * 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; }
/** * 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()); }
/* * 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; }