Question

JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator;...

JAVA

Have not gotten the public Iterator<Item> iterator() . Can someone explain it please?

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) {

   first = new Node<>();

   last = first;

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

   last.next = new Node<>();

last = last.next;

   }

   }

       // 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() {

       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 (20 points)

       return null;

   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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) {

   first = new Node<>();

   last = first;

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

   last.next = new Node<>();

last = last.next;

   }

   }

       // 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() {

       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 (20 points)

       return null;

   }

Add a comment
Know the answer?
Add Answer to:
JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements...

    Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements DictionaryInterface<K, V> { private int numEntries; private static final int DEFAULT_CAPACITY = 5; private static final int MAX_CAPACITY = 10000; private TableEntry<K, V>[] table; private double loadFactor; private static final double DEFAULT_LOAD_FACTOR = 0.75; public HashTableOpenAddressing() { this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) { numEntries = 0; if (loadFactorIn <= 0 || initialCapacity <= 0) { throw new IllegalArgumentException("Initial capacity and load...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • Java: Return an array of booleans in a directed graph. Please complete the TODO section in...

    Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private...

    JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private Link prev; public CircularList() { // implement: set both current and prev to null } public boolean isEmpty() { // implement return true; } public void insert(int id) { // implement: insert the new node behind the current node } public Link delete() { // implement: delete the node referred by current return null; } public Link delete(int id) { // implement: delete the...

  • //MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>>...

    //MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> {    private static final long serialVersionUID = -6229569372944782075L;       public void add(K k, V v) { // Problem 1 method        // empty linked list, with key=k         if (!containsKey(k)) {               put(k, new LinkedList<V>());         }         // adding v to the linked list associated with key k         get(k).add(v);    }    public V removeFirst(K k)...

  • package algs24; import stdlib.StdIn; import stdlib.StdOut; /** * The <tt>PtrHeap</tt> class is the priorityQ class from...

    package algs24; import stdlib.StdIn; import stdlib.StdOut; /** * The <tt>PtrHeap</tt> class is the priorityQ class from Question 2.4.24. * It represents a priority queue of generic keys. *   * It supports the usual <em>insert</em> and <em>delete-the-maximum</em> * operations, along with methods for peeking at the maximum key, * testing if the priority queue is empty, and iterating through * the keys. * For additional documentation, see <a href="http://algs4.cs.princeton.edu/24pq">Section 2.4</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne....

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT