will be studying container classes, which are classes that contains objects of other classes • We want the container class to be flexible as possible, so we can store any type (class) of data • A class can have a generic type by using the type (class) as a parameter
uses text replacement to fill in the name of the actual class • In the class definition, we use the generic type (class) • We use the specific type (class) when we instantiate the container object that uses the particular type
class • Step #1: Use <T> after the name of the class in class definition • public class TwoThings<T, C>{. . .} • Can actually use any letter • In this case, T stands for “Type” • You might use C for “Class”, E for “Element”, etc.
argument after the name of the generic class for object instantiation • TwoThings<String, Integer> object1 = new TwoThings<String, Integer>("Nami", 25); • TwoThings<Name, Fraction> object2 = new TwoThings<Name, Fraction> (name, years);
TwoThings.java on class web page • Generic class TwoThings can store objects of any two classes • In the main() method, object1 stores a String and an Integer, while object2 stores a Name and a Fraction
a Last-In, First-Out (LIFO) property • The last object pushed (placed) in the top of the stack is the first object popped (taken) out of the stack • Daily life example of a stack is a stack of trays in the school cafeteria
Stack method prototypes • See class ArrayStack.java for the class definition which includes the method definitions as well as a client program in the main() method
• T peek() throws EmptyStackException • Retrieve (does NOT remove) an object’s address from top of stack • If no elements on stack, then will throw an EmptyStackException
returns an object from top of stack • If no elements on stack, then will throw an EmptyStackException • void push(T item) • Add an object to the top of stack
as an array • Push and pop to the end of the array • See class ArrayStack.java on the class web page • The main() method of this class has a driver program that tests all of the methods of the ArrayStack
the “box and balloon” model to visualize an ArrayStack object • Rectangle represents a variable • Arrow represents the address of an object • Oval represents the space where an object’s data is stored
and balloon” model for this code, which creates a stack of Strings • We begin with an empty stack StackInterface<String> stack2 = new ArrayStack<String>();
stack • This increments the top variable, so it always stores the index of the top element on the stack stack2.push("A"); stack2.push("B"); stack2.push("C"); stack2.push("D");
off the stack, this only changes the top variable, as the three (3) strings are still stored in the array String letter = stack2.pop(); //letter: "D" letter = stack2.pop(); //letter: "C" letter = stack2.pop(); //letter: "B"
store objects of any class, such as objects of class ThreeNames StackInterface<ThreeNames> stack3 = new ArrayStack<ThreeNames>(); ThreeNames name = new ThreeNames("A","B","C"); stack3.push(name); name = new ThreeNames("X", "Y", "Z"); stack3.push(name);
little complicated • In the worst case, Big-O is O(n) because System.arraycopy() copies all n elements into a new array • However, method push() takes time proportional to n at most once every n times, so on average (amortized) Big-O is O(1)
first-out) property, we can use a stack to print out a string backwards • See program BackwardLines.java, which takes an input file from the command line and prints each line backwards to the output window
before others 1. ( ): parentheses have highest precedence 2. *, /, %: multiplication, division, and modulus are next 3. +, -: addition and subtraction have the lowest precedence
the same precedence, the equation is evaluated from left to right • 3+4*5 is evaluated (3+(4*5)) • Because * has higher precedence than + • 3+4-5 is evaluated ((3+4)-5) • Because + and - have the same precedence
must take two steps to evaluate an algebraic expression • Step #1: use a stack to convert infix expression to a postfix expression (this class) • Step #2: use a stack to evaluate the postfix expression (next class)
to postfix expression • To simply the algorithm, we will assume that the expression is fully parenthesized, has only single digit integers, and has only the 5 integer operations (*, /, %, +, -)
contains the infix integer expression 2. Loop from the 0th position to the last position in the input string a. Isolate a single character string from the input string b. If the character string is a left parenthesis "(", push it on the stack
a right parenthesis ")", pop each operator off the stack, and add each operator to the end of the output string, until you pop a left parenthesis "(" off the stack 3. After looping, the output string is the postfix expression
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture