CS401 Lab 8:
A Stack of Objects and a Queue of Objects


Introduction

In this lab you will use inheritance to define a Stack class using the functionality of the Vector class described in the Java APIs. Look at Vector in the Java APIs.


Stack Operations

A Stack implements these operations:


Assignment

Implement the stack operations in the class Stack in the file Stack.java. Since you are going to define your class in terms of the ElasticArray class, your file should begin with:

           public class Stack { ...

When you are finished, the following program:

  public static void main(String[] args) {
    Stack s = new Stack() ;
    s.push("objects") ;  s.push("on") ;
    s.push("a") ; s.push("stack") ;
    while ( !s.isEmpty() ) { 
      System.out.println( s.pop() ) ;
    }
  }

prints:

  stack
  a
  on
  objects

Queue Operations

A Queue implements these operations:


  public static void main(String[] args) {
    Queue q = new Queue() ;
    q.enque("objects") ;  q.enque("on") ;
    q.enque("a") ; q.enque("queue") ;
    while ( !q.isEmpty() ) { 
      System.out.println( q.front() ) ;
      q.deque();
    }
  }

prints:

  objects
  on
  a
  queue

 

Submission Instructions

Demo your Stack.java and Queue.java files for your TA at the next lab meeting.