import java.util.ArrayList; // Queue using an ArrayList -- note that the public methods // change only by using java.lang.Object instead of int public class QueueV2 { // Constant specifying the maximum queue size public static final int DEFAULT_SIZE = 1000; // Internal (hidden) representation is an ArrayList private ArrayList q; // Maximum size of this queue private int maxSize; // Construct a queue of default size public QueueV2() { this( DEFAULT_SIZE ); } // Construct a queue public QueueV2( int size ) { this.q = new ArrayList( size ); this.maxSize = size; } // Determine if this queue is empty public boolean isEmpty() { return this.q.isEmpty(); } // Add an element to the end of this queue public void add( Object x ) { if ( this.q.size() < this.maxSize ) { this.q.add( x ); System.out.println( "ADDED " + x ); } else { // ERROR System.out.println( "FULL - CANNOT ADD " + x ); } } // Remove an element from the head of this queue public Object remove() { if ( this.q.isEmpty() ) { // ERROR System.out.println( "EMPTY!" ); return -1; } else { Object x = this.q.remove( 0 ); // index 0 System.out.println( "REMOVED " + x ); return x; } } // The main() code does not change public static void main(String[] args) { System.out.println( "Default queue size is " + QueueV2.DEFAULT_SIZE ); QueueV2 q1 = new QueueV2(3); if ( q1.isEmpty() ) { q1.add( 12.5 ); q1.add( "HELLO" ); Object x = q1.remove(); q1.add( 36 ); q1.add( 17 ); q1.add( 14 ); x = q1.remove(); x = q1.remove(); x = q1.remove(); x = q1.remove(); } } }