Given the following ArrayStack class with tester file. Create an ArrayQueue clas
ID: 651225 • Letter: G
Question
Given the following ArrayStack class with tester file. Create an ArrayQueue class for Strings with following methods:
capacity to determine number of elements
add to add elements
remove to remove elements
peek to determine the front of the queue
empty to determine if the queue has no more elements on it
Use this as the attribute members and the constructor
private String[] q;
private int front;
private int rear;
private int size;
ArrayQueue(int capacity)
{
q=new String[capacity];
front=0;
rear=0;
size=0;
}
help! JAVA
Explanation / Answer
import java.io.*;
import java.util.NoSuchElementException;
public class Queueq
{
private Object[] theArray;
private int N;
private int front;
private int rear;
static final int DEFAULT_CAPACITY = 10;
public QueueAr()
{
this(DEFAULT_CAPACITY);
}
public QueueAr(int capacity)
{
N = capacity;
theArray = new Object[N];
front = 0;
rear = 0;
}
public int size()
{
return (N - front + rear) % N;
}
public boolean isEmpty()
{
return (front == rear);
}
public boolean isFull()
{
return (this.size() == (N-1));
}
public void enqueue(Object x)
{
if(isFull())
throw new Ex("enqueing a full queue");
theArray[rear] = x;
rear = (rear + 1) % N;
}
public Object dequeue()
{
if(isEmpty())
throw new Ex("dequeing an empty queue");
Object frontItem = theArray[front];
theArray[front] = null;
front = (front + 1) % N;
return frontItem;
}
public static void main(String[] args)
{
QueueAr q = new QueueAr();
for(int i = 1;i < 10; i++) {
q.enqueue(new Integer(i));
}
while(!q.isEmpty()) {
System.out.println((Integer)q.dequeue());
}
}
}
class Ex extends RuntimeException
{
public Ex(String s)
{
System.out.println(s +" error");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.