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 Stack (int size) . . . public void push ( Object obj ) public Object pop() public boolean isEmpty() public boolean isFull() }

3 Additional Operations?
Peek? As an implementer As a user

4 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

5 // 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

6 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; }

7 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

8 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

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

10 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.

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

12 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; }

13 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

14 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

15 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

16 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!

17 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

18 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

19 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!

20 Quiz 3 Monday, October 22

21 Lab 6 Can assume the commands you need
Specify input and the corresponding output No assert!

22 Test Case 1 Add a Golfer to an Empty League
Description Command Expected result / output 1.1 Add a golfer to empty golf league Print Add Frank Regular 75 All members of the League: Regular Member Frank was added Regular Member Frank: 0,75,75,75,75,75 Test Case Description Command Expected result / output 1.2 Add a senior golfer to empty golf league . . . Test Case Description Command Expected result / output 1.3 Add a youth golfer to empty golf league . . .

23 Lab 6 Submission UserName_Lab6.doc(x) To make corrections,
Only second submission accepted K:\Courses\CSSE\yangq\cs2430\1Dropbox


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

Similar presentations


Ads by Google