(Solved) : Package Hw5 Public Class Generalizedqueue Int N Number Items Currently Queue Item Contains Q41415932 . . .

package hw5;

public classGeneralizedQueue<Item> {

int N; // The number of items currently in thequeue

Item[] a; // Contains the items in the queue in slots 0 throughN-1

/**

* Creates an empty queue.

*/

public GeneralizedQueue() {

// TODO

}

/**

* Checks if the queue is empty

* @return <code>true</code> if thequeue is empty

* and <code>false</code> otherwise.

*/

public boolean isEmpty() {

// TODO

}

/**

* Add an item to the back of the queue.

* @param x the item to be addded to thequeue

*/

public void insert(Item x){

// TODO

}

/**

* delete and return the kth least recently inserted item

* (the kth item “in line”.)

* @param k indicates which position from thequeue to be removed.

* The first item is in position 1.

* @return the kth oldest item in the queue

* @throws NoSuchElementException if there arefewer than k items in the queue.

*/

public Item delete(int k){

// TODO

return null;

}

}

// CHECK AGAINST THE FOLLOWING TEST FILE

package hw5;

import static org.junit.Assert.*;

import java.util.NoSuchElementException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

public class HW5Test {

   @Rule
public Timeout globalTimeout = Timeout.seconds(1);
  
  
   @Test
   public void test() {
       Integer[] data = newInteger[10];
       for(int i = 0; i < data.length;i++)
           data[i] = newInteger(i);
       GeneralizedQueue<Integer> gq= new GeneralizedQueue<Integer>();
       for(int i = 0; i < data.length;i++) {
          gq.insert(data[i]);
       }
       // gq = 0, 1, 2, 3, 4, 5, 6, 7, 8,9
       assertEquals(data[1],gq.delete(2)); // deletes 2nd item
       // gq = 0, 2, 3, 4, 5, 6, 7, 8,9
       assertEquals(data[3],gq.delete(3)); // deletes 3rd item
       // gq = 0, 2, 4, 5, 6, 7, 8,9
       assertEquals(data[5],gq.delete(4)); // deletes 4th item
       // gq = 0, 2, 4, 6, 7, 8, 9
       try {
           gq.delete(8);   // deletes 8th item which doesn’t exist
           fail(“Shouldhave thrown NoSuchElementException”); // shouldn’t reach thisline
       } catch(NoSuchElementException e){
           // Excpected tobe caught here.
       }
       assertEquals(data[0],gq.delete(1)); // deletes 1st item
       // gq = 2, 4, 6, 7, 8, 9
   }

}
Restrictions: You may not add any fields to the GeneralizedQueueclass. Use the array a that is already declared inside the class tohold the items in the queue. Because typically not all the slots ofthe array are being used, use the field N (already declared in theclass) to keep track of how many items are in the queue.

Hints: • Perhaps the most intuitive way of doing this assignmentis maintaining the invariant that the items in the queue are alwaysat the beginning of the array, with the first item in slot 0, thenext item in slot 1, etc. This will require you to slide thecontents of the array around when you remove something fromanything but the last slot.

Expert Answer


Answer to package hw5; public class GeneralizedQueue { int N; // The number of items currently in the queue Item[] a; // Contains …