Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA: a. (5 pts) Write a BadArrayQueue<E> class. Your class should implement

ID: 3806035 • Letter: I

Question

IN JAVA:

a. (5 pts) Write a BadArrayQueue<E> class. Your class should implement a queue using an array. However, it should fix the front of the queue at index 0 of the array, meaning that when a dequeue is performed, all remaining array elements need to be shifted down 1 index to the left.
b. (5 pts) Within a main method, simulate the logging in of 100,000 players by using your “bad” queue implementation as well as the “good” implementation that we wrote in class. Measure the time needed to enqueue all 100,000 players, and the time needed to remove them all from the queue. Remember that you can use the built-in method System.currentTimeMillis() to get the current system time in milliseconds. Use this as a “stopwatch” – call currentTimeMillis once to get the starting time, execute the code you want to time, and then call currentTimeMillis again to get the ending time. The elapsed time is simply the difference between the start and end times.

Explanation / Answer

Answer for A:

public class BadArrayQueue {
private static final int size = 100;
int array[] = new int[size];
int top = -1;
int rear = 0;

public void enqueue(int element) {
if (top < size - 1) {
top++;
array[top] = element;
System.out.println("Enqueud element: " + array[top]);
} else {
System.out.println("Queue full !");
}

}

public void dequeue() {
if (top >= rear) {
   System.out.println("Dequeud element: " + array[0]);
for (int i = 0; i < top; i++) //shifting all elements to left in array
{
array[i]= array[i+1];
  
}
top--;

} else {
System.out.println("Queue empty !");
}
}

public void display() {
if (top >= rear) {
System.out.println("Elements in Queue : ");
for (int i = rear; i <= top; i++) {
System.out.println(array[i]);
}
}
}

public static void main(String[] args) {
BadArrayQueue queueObj = new BadArrayQueue();
queueObj.enqueue(2); //testing the functions
queueObj.enqueue(5);
queueObj.enqueue(3);
queueObj.enqueue(1);
queueObj.dequeue();
queueObj.dequeue();
queueObj.dequeue();
queueObj.dequeue();
queueObj.dequeue();
}

}

Answer for B: Replace the main method in the above class with the below main menthod. It has the all the required criteria asked in the question b

public static void main(String[] args) {
BadArrayQueue queueObj = new BadArrayQueue();
  
long Time_before_enqueue = System.currentTimeMillis();
System.out.println("Time Before enquing: " + Time_before_enqueue );
for (int i = 0; i < size-1; i++) //shifting all elements to left in array
{
queueObj.enqueue(i);
  
}
long Time_after_enqueue = System.currentTimeMillis();
System.out.println("Time After enquing: " + Time_after_enqueue );

System.out.println("Total time taken for enquing is " + (Time_after_enqueue-Time_before_enqueue) + " Milli Secs");

long Time_before_dequeue = System.currentTimeMillis();
System.out.println("Time Before dequing: " + Time_before_dequeue );
for (int i = 0; i < size-1; i++) //shifting all elements to left in array
{
queueObj.dequeue();
  
}
long Time_after_dequeue = System.currentTimeMillis();
System.out.println("Time After dequing: " + Time_after_enqueue );

System.out.println("Total time taken for dequing is " + (Time_after_dequeue-Time_before_dequeue) + " Milli Secs");

}