Java Program. Use the information in the given code below to perform the followi
ID: 3716724 • Letter: J
Question
Java Program. Use the information in the given code below to perform the following:
Assume myQueue is an object of the class ArrayQueue.
You may call any of the public methods of ArrayQueue.
You may also declare additional queue objects.
Make a copy of myQueue, leaving myQueue unchanged.
You only need to write a segment that makes a copy of myQueue and leaves myQueue unchanged.
Use the code below for reference.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//public
interface QueueInterface
{
public void enqueue(Object item);
// Effect: Adds item to the rear of this queue.
// Precondition: This queue is not full.
// Postcondition: item is at the rear of this queue.
public Object dequeue();
// Effect: Removes front element from this queue and returns it.
// Precondition: This queue is not empty.
// Postconditions: Front element has been removed from this queue.
// Return value = (the removed element)
public boolean isEmpty();
// Effect: Determines whether this queue is empty.
// Postcondition: Return value = (this queue is empty)
public boolean isFull();
// Effect: Determines whether this queue is full.
// Postcondition: Return value = (queue is full)
}
//----------------------------------------------------------------------------
// ArrayQueue.java by Dale/Joyce/Weems Chapter 4
//
// Implements QueueInterface using an array to hold the queue items
//----------------------------------------------------------------------------
//package ch04.queues;
//public
class ArrayQueue1 implements QueueInterface
{
private Object[] queue; // Array that holds queue elements
private int capacity; // size of the array (capacity of the queue)
private int numItems = 0; // number of items on the queue
// private int front = -1; // index of front of queue
// private int rear = 0; // index of rear of queue
// Constructors
public ArrayQueue1()
{
queue = new Object[100];
capacity = 100;
}
public ArrayQueue1(int maxSize)
{
queue = new Object[maxSize];
capacity = maxSize;
}
public void enqueue(Object item)
// Adds an element to the front of this queue
{
if(!isFull())
{ queue[numItems] = item;
numItems = numItems + 1;
}
}
public Object dequeue()
// Removes an element from the rear of this queue
{
Object toReturn = null;
if(!isEmpty())
{
toReturn = queue[0];
for(int i = 0; i <= numItems - 2; i++)
queue[i] = queue[i + 1];
queue[numItems - 1] = null;
numItems = numItems - 1;
}
return toReturn;
}
public boolean isEmpty()
// Checks if this queue is empty
{
return (numItems == 0);
}
public boolean isFull()
// Checks if this queue is full
{
return (numItems == capacity);
}
}
Explanation / Answer
Please find the code below to create a copy of myQueue called "copy_myQueue"
ArrayQueue1 myQueue = new ArrayQueue1();
ArrayQueue1 copy_myQueue = new ArrayQueue1();
while(!myQueue.isEmpty()) {
Object item = myQueue.dequeue();
copy_myQueue.enqueue(item);
myQueue.enqueue(item);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.