COMPUTER 2430 Object Oriented Programming and Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I 1
2
Quiz 3 1. Evaluate the following expressions – a) Postfix – Answer: -1 – b) Prefix – Answer: 29 3
Quiz 3 3. Convert the following to prefix and postfix a) Prefix +A-*B/CDE b) Postfix ABCD/*E-+ 4
Quiz 3: As a User public Object remove. And. Return. Bottom. Element( Stack s, int size ) { Stack temp = new Stack(size); // Hint: use another stack! if (s. is. Empty()) return null; while (!s. is. Empty()) temp. push( s. pop() ); Object obj = temp. pop(); while (!temp. is. Empty()) s. push) temp. pop() ); return obj; } 5
!s. is. Empty() public Object remove. And. Return. Bottom. Element( Stack s, int size ) {. . . if (s. is. Empty()) return null; // while (!s. is. Empty()) while (s. is. Empty() == false) temp. push( s. pop() ); Object obj = temp. pop(); // while (!s. is. Empty()) while (temp. is. Empty() == false) s. push) temp. pop() ); return obj; } 6
Quiz 3: As a User public Object remove. And. Return. Bottom. Element( Stack s, int size ) { Stack temp = new Stack(size); // Hint: use another stack! if (s. is. Empty()) return null; while (!s. is. Empty()) temp. push( s. pop() ); Object obj = temp. pop(); s = temp; // NO! // will it work? return obj; } 7
Quiz 3: As an Implementer public class Stack { private Object[] items; private int top; public Object get. Bottom. Element() { if (top == 0) return null; Object obj = items[0]; top --; for (int i = 0; i < top; i ++) items[i] = items[i + 1]; return obj; } } 8
Quiz 3: As an Implementer public class Stack {. . . public Object get. Bottom. Element() { 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; } } 9
public class Stack { private Object[] items; private int top = 0; public Stack( int size ). . . public. . . void push( Object obj ) Object pop() boolean is. Empty() boolean is. Full() } 10
Example Stack person. List = new Stack(100); Person p = new Person(); person. List. push( p ); // Valid? // Yes. // public void push( Object obj ) // Can use child when parent is expected. . p = person. List. pop(); // Valid? // NO! // public Object pop() // Cannot use parent when child is expected. p = (Person) person. List. pop(); // Must cast! 11
Stack of Person public class Person. Stack { private Person[] items; private int top = 0; public Stack( int size ). . . public. . . void push( Person obj ) Person pop() boolean is. Empty() boolean is. Full() } 12
Stack of Person public class Person. Stack { private Person[] items; . . . } Person. Stack person. List = new Person. Stack(100); Person p; . . . p = person. List. pop(); // Valid? // Yes! No cast needed. Object obj = new Object(); person. List. push( obj ); // Valid? // NO! // Cannot use parent when child is expected. 13
Stack for Each Class public class Person. Stack { private Person[] items; . . . } public class Date. Stack { private Date[] items; . . . } public class TVector. Stack { private TVector[] items; . . . }. . . The code is almost the same! 14
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 15
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 is. Empty() { return top == 0; } public boolean is. Full() { return top >= items. length; }. . . } 16
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]; } } 17
Example Stack<Person> person. Stack = new Stack<Person>(100); Stack<Date> date. Stack = new Stack<Date>(100); Person p; Date d; . . . p = person. Stack. pop(); d = date. Stack. pop(); // No cast needed! d = new Date(); date. Stack. push( d ); // Valid? // Yes person. Stack. push( d ); // Valid? // NO! 18
Prog 4 Generic Stack RPN Evaluation 19
Prog 4 • Class TVector – 4 -dimensional – Example: (-7, 21, 5, -33) • Java class Vector – Generic class – Methods: • size() • get(i) • add(x) 20
Other Generic Classes • Generic Queue • More in CS 2630 21
Prog 3 & Prog 4 22
- Slides: 22