JAVA Can someone explain the public Iterator iterator to me

JAVA

Can someone explain the public Iterator<Item> iterator() to me.

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?
   */
   public boolean isEmpty() {
       return size() == 0;
   }
  
   /**
   * Returns the number of items in the GroupsQueue.
   */
   public int size() {
       return n; // TODO (20 points)
   }
  
   /**
   * Adds the item to this GroupsQueue with group id = gId.
   */
   public void add(Item item, int gId) {
       Node<Item> oldlast = last;
last = new Node<Item>();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
n++;
}
       // TODO (20 points)
  
   /**
   * Removes and returns the item with the lowest group id
   */
   public Item remove() {
} // TODO (20 points)

/**
* Returns an iterator that iterates over the items in this GroupsQueue
* in group id order (lowest id first) and in FIFO order in each group.
*/
   public Iterator<Item> iterator() {
       // TODO
// BONUS
       return null;
   }
}

Solution

********Sorry for re-writing your code *********

But what I would like to tell you is something is missing in the above code. So modified accordingly please see below

package groupqueue;

import java.util.ArrayList;
import java.util.Iterator;

public class GroupsQueue<Item> implements Iterable<Item> {
   private ArrayList<Group> groups;
  
   //Defining Groups in GroupQueue
   private class Group{
   private Node<Item> first;
   private Node<Item> last;
   private int n;
     
   //Nodes in each group
   private class Node<Item> {
       private Item item;
   private Node<Item> next;
     
   public Node(Item data) {
   this.item = data;
   }
     
       public Node(Item data, Node next) {
   this(data);
   this.next = next;
   }
   }
  
   public void Queue() {
       first = null;
   last = null;
   n = 0;
   }
     
   //Add node to the existiting node
   public void add(Item data) {
   last = new Node(data, last);
   n++;
   }
     
   //removes the item which has lowest group id and came in the first
   public Item dequeue() {
   Item temp = first.item;
   first = first.next;
   n--;
   return temp;
   }
   public int size(){
       return n;
   }
   }
   // TODO : implement the data structure (20 points)
   /**
   * Initializes an empty GroupsQueue with g groups
   * @return
   */
   public GroupsQueue(int g) {
      
       //creating g groups
       groups = new ArrayList<Group>(g);
       for(int i=0;i<g;i++){
           groups.add(new Group());
       }
   // TODO : implement the constructor (20 points)
      
   }
  
   /**
   * Is this GroupsQueue empty?
   */
   public boolean isEmpty() {
   return size() == 0;
   }
  
   /**
   * Returns the number of items in the GroupsQueue.
   */
   public int size() {
       int num = 0;
      
       //Calculating all the items in all groups
   for(int i = 0; i < groups.size(); i++){
   num += groups.get(i).size();
   }
   return num; // TODO (20 points)
   }
  
   /**
   * Adds the item to this GroupsQueue with group id = gId.
   */
   public void add(Item item, int gId) {
      
       //add item to given gid
       Group tempGroup = groups.get(gId);
       tempGroup.add(item);
   }
   // TODO (20 points)
   /**
   * Removes and returns the item with the lowest group id
   */
   public Item remove() {
      
       //removing item which has lowest gid and came last
   for(int i = 0; i < groups.size(); i++){
   if(groups.get(i).size()!=0){
       return groups.get(i).dequeue();
   }
   }
           return null;
   } // TODO (20 points)
   /**
   * Returns an iterator that iterates over the items in this GroupsQueue
   * in group id order (lowest id first) and in FIFO order in each group.
   */
   public Iterator<Item> iterator() {
   // TODO
   // BONUS
       Iterator<Item> temp = (Iterator<Item>) groups.iterator();
   return temp;
   }
   }

GroupQueue will have objects of Groups and each group may have any number of items.

public Iterator<Item> iterator() will iterate throgh all the group that are available in GroupQueue based on lowestGid and FIFO order.

Hope this helps!

JAVA Can someone explain the public Iterator<Item> iterator() to me. import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of
JAVA Can someone explain the public Iterator<Item> iterator() to me. import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of
JAVA Can someone explain the public Iterator<Item> iterator() to me. import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of
JAVA Can someone explain the public Iterator<Item> iterator() to me. import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site