SUNY Geneseo Department of Computer Science


Alternative Design for Class Stack

{Date}

CSci 141, Spring 2005
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Second hour exam is Friday (April 1)

Questions?

Returning yellow tile count?

Walk through treasure hunt

    if robot is on white tile
        move forward
        int i = hunt()      // returns # yellow
        move forward
        return i

Is Stack Really a Subclass of List?

Class = set of (potential) objects

Subclass = subset

Are stacks conceptually a subset of lists?

Subclass as subset vs subclass for reusing code

Implementing Stacks without Subclassing List

    import geneseo.cs.sc.List;
    class Stack {
        private List items;      // holds items on stack
        public Stack() {
            items = new List();
        }
        public void push( Object x ) {
            items.addItems( x );
        }
        public Object pop() {
            return items.getAndRemove();
        }
        public boolean isEmpty() {
            return items.isEmpty();
        }
    }

Adaptor Pattern

[New Class "Adapts" Methods of Old to a New Interface]

Pre-Exam Questions?

Is every element of a list the same (Java)

    // In ExtendList subclass of List
    public boolean allSame() {
        if ( this.isEmpty() ) {
            return true;
        }
        else if ( this.getRest().isEmpty() ) {
            return true;
        }
        else {
            return this.getFirst().equals(this.getRest().getFirst()) && ((ExtendList)this.getRest()).allSame();
        }
    }

Next

(After exam)

Trees

Read Sections 13.1 and 13.2


Next Lecture