CS241: Spring 2015 -- Stacks and interfaces

  1. The present: current lab - note the requirement to get credit. Issues?
  2. converting infix to postfix expressions (without parentheses!):
    while (more input) {
       next = nextToken()
       if (next is operand) 
          emit next
       else {
          while (!stack.isEmpty() && (precedence(top()) >= precedence(next))) {  
             emit(pop());
          } // while
          push(next)
       } // else
    }
    
    while !stack.isEmpty()
       emit(pop())
    
    E.g. 1+(2*3)-4... -> postfix -> 3
  3. parentheses!
    1. Insight: parentheses promote the precedence of everything inside them above everything -- thus, must emit all intraparenthetic operators before any following operators
    2. Treat ('s as operators, except you never emit them, with the following changes
    3. "(" as the lowest precedence than any other operator (so any operator may be pushed on top of it)
    4. ")" never goes on the stack, so if you also give it the lowest precedence of anything, the while loop (in the pseudocode above) will pop and emit everything on the stack! You must modify that loop to stop when a "(" is popped (then discard it and the close paren)
  4. Editing the templates in NetBeans
  5. The future: implementing and timing various sorts
  6.     long getTheTime() {
            return System.currentTimeMillis();
        }
  7. Interfaces
    1. Specify the signatures of methods
    2. To implement an interface, a class must implement each of those methods
    3. Why use/write an interface?
      1. To collaborate. Agree on and publish the interface, then both sides can work independently
      2. To reduce cognitive overhead (if all you can see are the signatures, there's no detail to deal with)
      3. To allow variables (including parameters) to have the type of the interface, not any particular class