COMPUTER 2430 Object Oriented Programming and Data Structures I
Quiz 3 1. Evaluate the following expressions a) Postfix Answer: -1 b) Prefix Answer: 29
Quiz 3 3. Convert the following to prefix and postfix a) Prefix + A - * B / C D E b) Postfix A B C D / * E - +
Quiz 3: As a User public Object removeAndReturnBottomElement( Stack s, int size ) { Stack temp = new Stack(size); // Hint: use another stack! if (s.isEmpty()) return null; while (!s.isEmpty()) temp.push( s.pop() ); Object obj = temp.pop(); while (!temp.isEmpty()) s.push) temp.pop() ); return obj; }
!s.isEmpty() public Object removeAndReturnBottomElement( Stack s, int size ) { . . . if (s.isEmpty()) return null; // while (!s.isEmpty()) while (s.isEmpty() == false) temp.push( s.pop() ); Object obj = temp.pop(); while (temp.isEmpty() == false) s.push) temp.pop() ); return obj; }
Quiz 3: As a User public Object removeAndReturnBottomElement( Stack s, int size ) { Stack temp = new Stack(size); // Hint: use another stack! if (s.isEmpty()) return null; while (!s.isEmpty()) temp.push( s.pop() ); Object obj = temp.pop(); s = temp; // will it work? // NO! return obj; }
Quiz 3: As an Implementer public class Stack { private Object[] items; private int top; public Object getBottomElement() if (top == 0) return null; Object obj = items[0]; top --; for (int i = 0; i < top; i ++) items[i] = items[i + 1]; return obj; }
Quiz 3: As an Implementer public class Stack { . . . public Object getBottomElement() if (top == 0) return null; Object obj = items[0]; for (int i = 0; i < top - 1; i ++) items[i] = items[i + 1]; top --; return obj; }
public class Stack { private Object[] items; private int top = 0; public Stack( int size ) ... public void push( Object obj ) public Object pop() public boolean isEmpty() public boolean isFull() }
Example Stack personList = new Stack(100); Person p = new Person(); personList.push( p ); // Valid? // Yes. // public void push( Object obj ) // Can use child when parent is expected. ... p = personList.pop(); // Valid? // NO! // public Object pop() // Cannot use parent when child is expected. p = (Person) personList.pop(); // Must cast!
Stack of Person public class PersonStack { private Person[] items; private int top = 0; public Stack( int size ) ... public void push( Person obj ) public Person pop() public boolean isEmpty() public boolean isFull() }
Stack of Person public class PersonStack { private Person[] items; ... } PersonStack personList = new PersonStack(100); Person p; p = personList.pop(); // Valid? // Yes! No cast needed. Object obj = new Object(); personList.push( obj ); // Valid? // NO! // Cannot use parent when child is expected.
Stack for Each Class public class PersonStack { private Person[] items; ... } public class DateStack private Date[] items; public class TVectorStack private TVector[] items; The code is almost the same!
Generic Stack public class Stack<E> { private E[] items; ... } E represents a class The class Stack can be instantiated with a particular class E is a parameter E must be a class and cannot be a primitive type
public class Stack<E> { private E[] items; private int top = 0; public Stack( int size ) items = (E[])new Object[size]; // Java Warning possible } public boolean isEmpty() return top == 0; public boolean isFull() return top >= items.length; ...
public class Stack<E> { private E[] items; private int top = 0; . . . public void push ( E x ) items[top ++] = x; } public E pop() return items[-- top];
Example Stack<Person> personStack = new Stack<Person>(100); Stack<Date> dateStack = new Stack<Date>(100); Person p; Date d; ... p = personStack.pop(); d = dateStack.pop(); // No cast needed! d = new Date(); dateStack.push( d ); // Valid? // Yes personStack.push( d ); // Valid? // NO!
Generic Stack RPN Evaluation Prog 4 Generic Stack RPN Evaluation
Prog 4 Class TVector Java class Vector 4-dimensional Example: (-7,21,5,-33) Java class Vector Generic class Methods: size() get(i) add(x)
Other Generic Classes Generic Queue More in CS 2630
Prog 3 & Prog 4