Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I

2 public class Stack { private Object[] items; private int top; public void push ( Object obj ) . . . public Object pop() public boolean isEmpty() public boolean isFull() } Method peek?

3 User Method peek Given a Stack with the methods isEmpty isFull push
pop How can a user have a look at the element before calling pop? Can NOT access private data! User peek method

4 // User method outside Stack class
public Object peek ( Stack s ) Stack myStack = new Stack( 1000 ); ... Object x = peek( myStack ); if (x == null) . . . else if (x instanceof FixedPoint) else if (x instanceof Golfer) else

5 public Object peek ( Stack s )
{ if ( s.isEmpty() ) return null; // Don’t change the stack Object obj = s.pop(); s.push( obj ); // peek the stack return obj; }

6 Passing Object Reference
public Object peek ( Stack s ) // Pass by value or pass by reference? Java has only passing by value No passing reference Each class variable is a reference The value of s is the address of a Stack object

7 Passing Object Reference
Stack myStack; myStack = new Stack( 1000 ); ... Object x = peek( myStack ); // public Object peek ( Stack s ) // passing by value myStack Activation record return point s

8 Passing Object Reference
Object x = peek( myStack ); Can the function change the object referenced by myStack? Yes! myStack stores the reference to the stack object. All objects are passed by reference in Java! Can the function modify myStack to reference a different stack? No! Passing by value! myStack Activation record return point s

9 User cannot access private data.
Peek by a User public Object peek ( Stack s ) { if ( s.isEmpty() ) return null; Object x = s.pop(); s.push( x ); return x; } It works. Has to pop and push! User cannot access private data.

10 User versus Implementer
Implementer can provide a peek method More efficient Implementer has access to all private data Upgrade

11 public class Stack { private Object[] items; private int top = 0; ... public Object peek() if ( top == 0 ) return null; return items[top - 1]; // Do not modify the stack (top). // return items[top--]; // top is modified! // top = top – 1; }

12 What can you do with a Stack?
As User Call the public methods! As Implementer Get count of items Search Insert item at any position Remove item from any position

13 Binary Arithmetic Operations
Conversion programs Infix  Prefix, Postfix Prefix  Infix, Postfix Postfix  Infix, Prefix Evaluation programs Using operator stack Using operand stack Using both Easiest program Evaluate postfix expressions Operand stack

14 Evaluating RPN Using Stack
4 / * * -17 Process tokens in order If the token is an operand Push it If the token is an operator Pop two operands Perform the operation Push the result Stop if invalid at any time

15 Evaluating RPN Stack: LIFO Just what we need here! 6 4 7 5 10 7 5 -3 5
/ * 9 * + / * 9 * + / * 9 * + / * 9 * + / * 9 * + 8 3 / * 9 * + / * 9 * + * 9 * + 9 * + * + + -49 6 4 7 5 10 7 5 -3 5 3 8 -3 5 2 -3 5 -6 5 9 -6 5 -54 5 -49 4 + 6 7 - 10 8 / 3 -3 * 2 -6 * 9 5 + (-54) Stack: LIFO Just what we need here!

16 Initialize an operand stack, s Set valid  true While valid and more tokens to process read token if token is operand s.push(token) else if token is operator if s empty then valid  false else op2  s.pop() if s empty then valid  false else op1  s.pop() if divide by 0 then valid  false else s.push( op1 <operator> op2 ) else valid  false If s empty then valid  false Else Answer  s.pop() if s not empty then valid  false

17 Invalid Expression 5 + Second pop fails – check for empty before calling pop + First pop fails – check for empty before before calling pop / Divide by 0 3 4 Stack not empty at the end – check for empty at the end 3 4 B Bad token Empty expression Popping answer fails – check for empty at the end

18 Invalid Expression What about an expression consisting of a single operand? 243 It is valid! An expression can be put on the right hand side of an assignment. X = 243; // valid! X = ; // Invalid!

19 Prog 3 Prog 2

20 Monday Class Lab 206


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google