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

JAVA Having trouble with the constructor for GroupsQueue() import java.util.Iter

ID: 3793154 • Letter: J

Question

JAVA

Having trouble with the constructor for GroupsQueue()

import java.util.Iterator;

/*
* GroupsQueue class supporting addition and removal of items
* with respect to a given number of priorities and with
* respect to the FIFO (first-in first-out) order for items
* with the same priority.
*
* An example, where GroupsQueue would be useful is the airline
* boarding process: every passenger gets assigned a priority,
* usually a number, e.g., group 1, group 2, group 3, etc. and
* the passengers line up at the end of the queue of their groups
* in a FIFO fashion; and, when removing from the GroupsQueue,
* i.e., when the passengers are boarding the plane, they get
* removed first by their group priorities and then by their FIFO
* ordering within that particular priority group.
*
* Your homework is to implement the below GroupsQueue data
* structure, so that it functions in the way described above.
*
* You may use the provided Queue class for implementing the
* GroupsQueue class.
*/

public class GroupsQueue<Item> implements Iterable<Item> {
   private Node<Item> first;
      private Node<Item> last;
   private int n;

   private static class Node<Item> {
   private Item item;
   private Node<Item> next;
   }
  
   public void Queue() {
   first = null;
   last = null;
   n = 0;
   }
   // TODO : implement the data structure (20 points)

   /**
   * Initializes an empty GroupsQueue with g groups
   * @return
   */
   public GroupsQueue(int g) {
       // TODO : implement the constructor (20 points)
   }
  
   /**
   * Is this GroupsQueue empty?
   */

Explanation / Answer

Hi I have implemented required constructor.

Please let me know in case of any issue.

import java.util.Iterator;

public class GroupsQueue<Item> implements Iterable<Item> {

   private Node<Item> first;

   private Node<Item> last;

   private int n;

   private static class Node<Item> {

       private Item item;

       private Node<Item> next;

   }

   public void Queue() {

       first = null;

       last = null;

       n = 0;

   }

   // TODO : implement the data structure (20 points)

   /**

   * Initializes an empty GroupsQueue with g groups

   * @return

   */

   public GroupsQueue(int g) {

      

       // creating first node and initializing first and last

       first = new Node<>();

       last = first;

       // Now creating 19 others empty group

       for(int i=1; i<=19; i++){

           last.next = new Node<>();

           last = last.next;

       }

   }

   @Override

   public Iterator<Item> iterator() {

       // TODO Auto-generated method stub

       return null;

   }

   /**

   * Is this GroupsQueue empty?

   */

}