1 / 23

COMPUTER 2430 Object Oriented Programming and Data Structures I

Learn how to implement a stack in Java using object-oriented programming and data structures. Explore operations like push, pop, peek, isEmpty, and isFull. Understand passing object references and the differences between user and implementer perspectives.

karenwest
Download Presentation

COMPUTER 2430 Object Oriented Programming and Data Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPUTER 2430Object Oriented Programming andData 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. 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 Implementer • Get count of items • Search • Insert item at any position • Remove item from any position • … As User Call the public methods!

  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 / 5 1 3 + * - ------------ ------------ 3 5 4 * - ------------------------ 3 20 - -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 5 7 4 6 + - 8 3 / * 9 * + 7 4 6 + - 8 3 / * 9 * + 4 6 + - 8 3 / * 9 * + 6 + - 8 3 / * 9 * + + - 8 3 / * 9 * + • 8 3 / * 9 * + 8 3 / * 9 * + / * 9 * + * 9 * + 9 * + * + + -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 • 4 0 / 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 1Add a Golfer to an Empty League

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

More Related