1- public class ArrayQueue implements Queue { private static final int CAPACITY = 30; private E[] data; private int front, rear, count; // HELPER FUNCTIONS:
1- public class ArrayQueue
// HELPER FUNCTIONS:
// Postcondition: Returns the next index in a circular array // private int nextIndex(int i) { return (i+1) % data.length; }
// CONSTRUCTORS:
// DEFAULT CONSTRUCTOR // Postcondition: The queue has been initialized as an empty queue. // public ArrayQueue() { data = (E[]) new Object[CAPACITY]; count = 0; front = 0; rear = CAPACITY - 1; }
// PARAMETERED CONSTRUCTOR // Postcondition: The queue has been initialized as an empty queue. // public ArrayQueue(int capacity) { data = (E[]) new Object[capacity]; count = 0; front = 0; rear = capacity - 1; }
// MUTATOR METHODS:
// Precondition: size() < CAPACITY. // Postcondition: A copy of entry has been inserted at the rear of the queue. // public void enqueue(E entry) { assert count < data.length; rear = nextIndex(rear); data[rear] = entry; count++; }
// Precondition: size() > 0. // Postcondition: The front item of the queue has been removed and returned. // public E serve() { E entry;
assert count > 0; entry = data[front]; front = nextIndex(front); count--; return entry; }
// Postcondition: The queue has been emptied. // public void clear() { count = 0; front = 0; rear = data.length-1; }
// OBSERVER METHODS:
// Precondition: size() > 0. // Postcondition: The return value is the front item of the queue // but the queue is unchanged. // public E peek() { assert count > 0; return data[front]; }
// Postcondition: The return value is true if the queue is empty. // public boolean isEmpty() { return (count == 0); }
// Postcondition: The return value is true if the queue is full. // public boolean isFull() { return (count == data.length); }
// Postcondition: The return value is the total number of items in the queue. // public int size() { return count; } }
2-
public class ArrayStack
// CONSTRUCTORS:
// DEFAULT CONSTRUCTOR: // Postcondition: The stack has been initialized as an empty stack. // public ArrayStack() { data = (E[]) new Object[CAPACITY]; top = 0; }
// PARAMETERED CONSTRUCTOR: // Postcondition: The stack has been initialized as an empty stack. // public ArrayStack(int capacity) { data = (E[]) new Object[capacity]; top = 0; }
// MUTATOR METHODS:
// Precondition: size() < CAPACITY. // Postcondition: A new copy of entry has been pushed onto the stack. public void push(E entry) { assert top < data.length; data[top] = entry; top++; }
// Precondition: size() > 0. // Postcondition: The top item of the stack has been removed and returned. // public E pop() { assert top > 0; top--; return data[top]; }
// Postcondition: The stack has been emptied. // public void clear( ) { top = 0; }
// OBSERVER MEMBERS:
// Precondition: size() > 0. // Postcondition: The return value is the top item of the stack but the // stack is unchanged. This differs slightly from the STL stack (where // the top function returns a reference to the item on top of the stack). // public E peek() { assert top > 0; return data[top-1]; }
// Postcondition: Return value is true if the stack is empty. // public boolean isEmpty() { return (top == 0); }
// Postcondition: Return value is true if the stack is full. // public boolean isFull() { return (top == data.length); }
// Postcondition: Return value is the total number of items in the stack. // public int size() { return top; } }
3-
public interface Stack
E pop();
void clear( );
E peek();
boolean isEmpty();
boolean isFull();
int size();
}
4-
public interface Queue
Q . In this part, you need to create new class StackUsingQueue.
Public class StackUsingQueue
The class have to implement the Stack interface using the methods in the ArrayQueue. Write a test application that can be used to test its functionality. You should get the following output
push 5 to the stack
push 7 to the stack
push 1 to the stack
push 9 to the stack
the size of the stack:4
pop from the stack:
9.0
pop from the stack:
1.0
the top of the stack:
7.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started