Which tasks will be carried out by the constructor

This preview shows page 23 - 40 out of 101 pages.

Which tasks will be carried out by the constructor? What will be the initial value of the top index? 1. Let top designate the first empty cell of the array. 2. Let top designate the top element.
Image of page 23

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack elems top "bravo" "charlie" "delta" 3 s "alpha" 0 1 2 3 4 5 6 7
Image of page 24
Implementing a Stack using an array: ArrayStack elems top (or size) "bravo" "charlie" "delta" 4 s "alpha" 0 1 2 3 4 5 6 7
Image of page 25

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack 1. Let top designate the first empty cell of the array. What should be the initial value of top ? 2. Let top designate the top element. What should be the initial value of the index top ? Make sure to fully understand both strategies.
Image of page 26
Implementing a Stack using an array: ArrayStack elems top (or size) s 0 1 2 3 4 5 6 7
Image of page 27

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack elems top (or size) s 0 1 2 3 4 5 6 7 0
Image of page 28
Implementing a Stack using an array: ArrayStack elems top s 0 1 2 3 4 5 6 7
Image of page 29

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack elems top -1 s 0 1 2 3 4 5 6 7
Image of page 30
Implementing a Stack using an array: ArrayStack public class ArrayStack implements Stack { // Instance variables private Object[] elems; // used to store the elements private int top; // designates the first free cell! // Constructor public ArrayStack( int capacity ) { }
Image of page 31

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack elems top s 0
Image of page 32
Implementing a Stack using an array: ArrayStack public class ArrayStack implements Stack { // Instance variables private Object[] elems; // used to store the elements of this ArrayStack private int top; // designates the first free cell! // Constructor public ArrayStack( int capacity ) { elems = new Object[ capacity ]; top = 0; } // Returns true if this ArrayStack is empty public boolean isEmpty() { return top == 0; }
Image of page 33

Subscribe to view the full document.

Pitfall?! public class ArrayStack implements Stack { // Instance variables private Object[] elems; private int top; // Constructor public ArrayStack( int capacity ) { Object[] elems = new Object[ capacity ]; top = 0; } // Returns true if this ArrayStack is empty public boolean isEmpty() { return top == 0; }
Image of page 34
100 capacity elems this 0 elems top Activation Frame for ArrayStack ArrayStack object formal parameter(s) local variable(s)
Image of page 35

Subscribe to view the full document.

100 capacity elems this 0 elems top Activation Frame for ArrayStack ArrayStack object formal parameter(s) local variable(s)
Image of page 36
100 capacity elems this 0 elems top Activation Frame for ArrayStack ArrayStack object formal parameter(s) local variable(s)
Image of page 37

Subscribe to view the full document.

Implementing a Stack using an array: ArrayStack // Returns the top element of this ArrayStack without removing it public Object peek() { // pre-conditions: ! isEmpty() return elems[ top-1 ]; }
Image of page 38
ArrayStack (using Java 1.5) Do you remember the problem with the pre-Java 1.5 implementation of the class Pair ? Well, the same problem is occurring with ArrayStack . That’s right, using references of type Object (for the instance variables and parameters) allows us to write a single implementation that can be used in a variety of contexts (to store String , Time and Event objects, to name a few).
Image of page 39

Subscribe to view the full document.

Image of page 40
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern