Calculator.java

  • Converts the string expression to arraylist of tokens
  • Determines if the character is an operator or a separator
  • If no, appends character
  • If yes and length is greater than 0, adds the token
  • Reverse Polish notation in which operators follow the numbers
  • rpnToResult: if character is operator, number is added to stack and if it's not, two numbers are removed and result is calculated with operand. Result is then added to stack
  • Push things to front of the stack
  • Code pushes all the numbers first and then the operator second (case does that)

What I added

Each operator and what it returns

Tester Method

Result

Unbalanced Paranthesis check

Power operator

Test on Postman

2019 FRQ Collegeboard

Part A

  • creates arraylist
  • access elements in array tokens
  • compares strings in tokens with instances of the variable
  • adds delimeters (beginning or end of string) into ArrayList in original order
public ArrayList<String> getDelimitersList(String[] tokens){
    ArrayList<String> delims = new ArrayList<String>(); // store delimiters in tokens
    for(String token: tokens){
        if(token.equals(this.openDel) || token.equals(this.closeDel)){ // check if delimeter
            delims.add(token);
        }
    }
    return delims;
}

Part B

  • initializes accumulators
  • access elements in arraylist
  • compares strings in delimiters with instances of variables
  • identifies and returns boolean value to implement one rule
  • identifies and return boolean value for all cases
public boolean isBalanced(ArrayList<String> delimiters){
    int openCount = 0;
    int closeCount = 0;
    for(String delim: delimiters){
        if(delim.equals(this.openDel)){
            openCount++;
        }
        else{
            closeCount++;
            if(openCount >= closeCount){
                continue;
            }
            else{
                return false;
            }
        }
    }
    if(openCount == closeCount){
        return true;
    }
    return false;
}