This assignment will require you to implement a generic interface (Queue) with an array based queue implementation class QueueArray. floating front design. Once you have implemented the class, the test program will run a client test program on the enqueue, dequeue, display and full methods. The test program will create queues of Integers as well as String objects. Enjoying the power of your generic implementation both will work with no additional changes. There are TODO based comments in the implementation file. Create a project and demonstrate that you can successfully create the required output file, shown below. Thr file includes:  Queue.java package queueHw; public interface Queue {     public boolean empty();     public boolean full();     public boolean enqueue(T data);     public T dequeue();     public void display(); } ... TestQueue.java package queueHw; public class TestQueue { public static void main(String[] args) { //TEST QueueArray with Int System.out.println("-- testing with QueueArray with INT-- "); Queue qi2 = new QueueArray<>(); TEST_QUEUE_WITH_INT(qi2); System.out.println("--------- "); System.out.println("--------- "); //TEST QueueArray with Int System.out.println("-- testing with QueueArray with STRING-- "); Queue qs2 = new QueueArray<>(); TEST_QUEUE_WITH_STRING(qs2); System.out.println("--------- "); System.out.println("--------- "); } public static void TEST_QUEUE_WITH_INT(Queue q) { q.enqueue(3); q.enqueue(4); q.enqueue(8); q.enqueue(0); q.enqueue(2); q.enqueue(9); q.display(); while (!q.empty()) { q.dequeue(); q.display(); } System.out.printf("%n"); for (int i = 2; i < 23; i += 2) q.enqueue(i); q.display(); while (!q.empty()) { q.dequeue(); q.display(); } System.out.printf("%n"); for (int i = 21; i < 60; i += 2) { q.enqueue(i); try { Thread.currentThread().sleep(125); } catch (InterruptedException ie) { } q.display(); } } public static void TEST_QUEUE_WITH_STRING(Queue qs) { String[] tokens = { "hello", "bye", "green", "house", "desk", "freedom", "element", "automobile" }; qs.display(); for (String str : tokens) { qs.enqueue(str); qs.display(); try { Thread.currentThread().sleep(125); } catch (InterruptedException ie) { } } System.out.println(); while (!qs.empty()) { qs.dequeue(); qs.display(); try { Thread.currentThread().sleep(125); } catch (InterruptedException ie) { } } }   } ... QueueArray.java public class QueueArray implements Queue { privateintfront; privateintrear; privateintMAX_SIZE = 100; privateT[] data; privateintnumElements = 0; publicQueueArray() { data = (T[]) newObject[MAX_SIZE]; front = 0; rear = 0; }   @Override publicbooleanempty() { // TODO remove the line below and replace with correct line of code returnfalse; } @Override publicbooleanfull() { // TODO remove the line below and replace with correct line of code returnfalse;   } @Override publicbooleanenqueue(Tel) { // TODO: // return false IF the queue is full     // TODO: // IF queue if empty // add new element into position 0 // both front and rear markers need to be updated to 0 // increment the numElements counter // return true       // TODO: // ELSE (we know the queue is not full or empty ) // bump the rear index // add the new element into the rear index // increment the numElements counter // return true     //TODO - delete this line returntrue; } @Override publicTdequeue() { // TODO // IF queue is empty // return null   // TODO // (queue not empty) // store the element at the front of the queue into a local variable of type T     // TODO // check if there is ONLY 1 element in the queue // IF only one element: // set front and rear to initial empty values // decrement the numElements counter     // TODO // ELSE (we know queue has 2+ elements) // bump the front marker // decrement the numElements counter       //TODO: return the local variable into which you removed the front element   //remove this line returnnull;   } @Override publicvoiddisplay() { // if queue is empty, print only [ ] if (empty()) System.out.println("[ ]"); // TODO: ELSE IF queue has 1 element in it only // print [ ]   // Else if (front == rear){ // System.out.print("[ " + data[front] + " ]"); // } elseif (front == rear) System.out.print("[ " + data[front] + " ]");   // ELSE // initialize int index to front // while (true) // print data[index] // if index == rear, break // otherwise bump the index continue the loop else { System.out.print("[ "); inti = front; while (true) { System.out.print(data[i] + " "); if (i == rear) break; i++;   if(numElements ==0) break; } System.out.print("] "); System.out.println(" "); } // print ] } privateintbump(intindex) { return (index + 1) % data.length; } // TODO: // this is a test program for the QueueArray class // AFTER this main can be run successfully, you may // remove it or comment it out, and run the REAL client program: TestQueue publicstaticvoidmain(String[] arg) { System.out.printf("Helo World%n"); Queue q = newQueueArray<>(); q.enqueue(5); q.enqueue(4); q.enqueue(55); q.enqueue(12); q.enqueue(7); q.display(); q.dequeue(); q.display(); q.enqueue(3); q.display(); } }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question
100%

This assignment will require you to implement a generic interface (Queue<T>) with an array based queue implementation class QueueArray<T>. floating front design.

Once you have implemented the class, the test program will run a client test program on the enqueue, dequeue, display and full methods. The test program will create queues of Integers as well as String objects. Enjoying the power of your generic implementation both will work with no additional changes.

There are TODO based comments in the implementation file. Create a project and demonstrate that you can successfully create the required output file, shown below.

Thr file includes: 

Queue.java

package queueHw;

public interface Queue <T>

{

    public boolean empty();

    public boolean full();

    public boolean enqueue(T data);

    public T dequeue();

    public void display();

}

...

TestQueue.java

package queueHw;

public class TestQueue {

public static void main(String[] args) {

//TEST QueueArray with Int

System.out.println("-- testing with QueueArray with INT-- ");

Queue<Integer> qi2 = new QueueArray<>();

TEST_QUEUE_WITH_INT(qi2);

System.out.println("--------- ");

System.out.println("--------- ");

//TEST QueueArray with Int

System.out.println("-- testing with QueueArray with STRING-- ");

Queue<String> qs2 = new QueueArray<>();

TEST_QUEUE_WITH_STRING(qs2);

System.out.println("--------- ");

System.out.println("--------- ");

}

public static void TEST_QUEUE_WITH_INT(Queue<Integer> q) {

q.enqueue(3);

q.enqueue(4);

q.enqueue(8);

q.enqueue(0);

q.enqueue(2);

q.enqueue(9);

q.display();

while (!q.empty()) {

q.dequeue();

q.display();

}

System.out.printf("%n");

for (int i = 2; i < 23; i += 2)

q.enqueue(i);

q.display();

while (!q.empty()) {

q.dequeue();

q.display();

}

System.out.printf("%n");

for (int i = 21; i < 60; i += 2) {

q.enqueue(i);

try {

Thread.currentThread().sleep(125);

} catch (InterruptedException ie) {

}

q.display();

}

}

public static void TEST_QUEUE_WITH_STRING(Queue<String> qs) {

String[] tokens = { "hello", "bye", "green", "house", "desk", "freedom", "element", "automobile" };

qs.display();

for (String str : tokens) {

qs.enqueue(str);

qs.display();

try {

Thread.currentThread().sleep(125);

} catch (InterruptedException ie) {

}

}

System.out.println();

while (!qs.empty()) {

qs.dequeue();

qs.display();

try {

Thread.currentThread().sleep(125);

} catch (InterruptedException ie) {

}

}

}

 

}

...

QueueArray.java

public class QueueArray<T> implements Queue<T> {
privateintfront;
privateintrear;
privateintMAX_SIZE = 100;
privateT[] data;
privateintnumElements = 0;

publicQueueArray() {
data = (T[]) newObject[MAX_SIZE];
front = 0;
rear = 0;
}
 
@Override
publicbooleanempty() {
// TODO remove the line below and replace with correct line of code
returnfalse;

}

@Override
publicbooleanfull() {
// TODO remove the line below and replace with correct line of code
returnfalse;
 
}

@Override
publicbooleanenqueue(Tel) {
// TODO:
// return false IF the queue is full
 
 
// TODO:
// IF queue if empty
// add new element into position 0
// both front and rear markers need to be updated to 0
// increment the numElements counter
// return true
 
 
 
// TODO:
// ELSE (we know the queue is not full or empty )
// bump the rear index
// add the new element into the rear index
// increment the numElements counter
// return true
 
 
//TODO - delete this line
returntrue;

}

@Override
publicTdequeue() {

// TODO
// IF queue is empty
// return null

 
// TODO
// (queue not empty)
// store the element at the front of the queue into a local variable of type T
 
 
// TODO
// check if there is ONLY 1 element in the queue
// IF only one element:
// set front and rear to initial empty values
// decrement the numElements counter
 
 
// TODO
// ELSE (we know queue has 2+ elements)
// bump the front marker
// decrement the numElements counter
 
 
 
//TODO: return the local variable into which you removed the front element
 
//remove this line
returnnull;
 

}

@Override
publicvoiddisplay() {
// if queue is empty, print only [ ]
if (empty())
System.out.println("[ ]");

// TODO: ELSE IF queue has 1 element in it only
// print [ <data front element> ]
 
// Else if (front == rear){
// System.out.print("[ " + data[front] + " ]");
// }

elseif (front == rear)
System.out.print("[ " + data[front] + " ]");


 
// ELSE
// initialize int index to front
// while (true)
// print data[index]
// if index == rear, break
// otherwise bump the index continue the loop

else {

System.out.print("[ ");

inti = front;
while (true) {
System.out.print(data[i] + " ");

if (i == rear)
break;
i++;
 
if(numElements ==0)
break;
}
System.out.print("] ");
System.out.println(" ");

}


// print ]

}

privateintbump(intindex) {
return (index + 1) % data.length;
}

// TODO:
// this is a test program for the QueueArray class
// AFTER this main can be run successfully, you may
// remove it or comment it out, and run the REAL client program: TestQueue

publicstaticvoidmain(String[] arg) {

System.out.printf("Helo World%n");

Queue<Integer> q = newQueueArray<>();
q.enqueue(5);
q.enqueue(4);
q.enqueue(55);
q.enqueue(12);
q.enqueue(7);
q.display();
q.dequeue();
q.display();
q.enqueue(3);
q.display();
}
}
Expert Solution
Step 1

Implementation of the QueueArray class that implements the Queue interface, with TODOs replaced with appropriate code:

public class QueueArray<T> implements Queue<T> {
    private int front;
    private int rear;
    private final int MAX_SIZE = 100;
    private T[] data;
    private int numElements = 0;

    public QueueArray() {
        data = (T[]) new Object[MAX_SIZE];
        front = 0;
        rear = -1;
    }

    @Override
    public boolean empty() {
        return numElements == 0;
    }

    @Override
    public boolean full() {
        return numElements == MAX_SIZE;
    }

    @Override
    public boolean enqueue(T el) {
        if (full()) {
            return false;
        }
        if (empty()) {
            front = 0;
        }
        rear = bump(rear);
        data[rear] = el;
        numElements++;
        return true;
    }

    @Override
    public T dequeue() {
        if (empty()) {
            return null;
        }
        T removedElement = data[front];
        if (numElements == 1) {
            front = 0;
            rear = -1;
        } else {
            front = bump(front);
        }
        numElements--;
        return removedElement;
    }

    @Override
    public void display() {
        if (empty()) {
            System.out.println("[ ]");
        } else if (numElements == 1) {
            System.out.println("[ " + data[front] + " ]");
        } else {
            System.out.print("[ ");
            int i = front;
            while (true) {
                System.out.print(data[i] + " ");
                if (i == rear) {
                    break;
                }
                i = bump(i);
            }
            System.out.print("] ");
            System.out.println(" ");
        }
    }

    private int bump(int index) {
        return (index + 1) % data.length;
    }
}

 

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning