SlideShare a Scribd company logo
1 of 7
Download to read offline
JAVA
need help with public Iterator iterator()
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 implements Iterable {
private Node first;
private Node last;
private int n;
private static class Node {
private Item item;
private Node 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 oldlast = last;
last = new Node();
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() {
Node temp = first;
Node prev = first;
Item minGid;
if (! isEmpty())
{
minGid = first.item;
}
else
{
return null;
}
while(temp!= null)
{
prev = temp;
temp = temp.next;
if (temp.item == minGid)
{
minGid = (Item) temp.item;
}
}
if (minGid == first.item)
{
first = first.next;
}
else
{
prev.next = prev.next.next;
}
n--;
return minGid;
}// 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 iterator() {
// TODO
// BONUS (20 points)
return null;
}
}
Solution
Implementation given here seems to have many logical errors.
Here is the code given for GroupQueue:
package groupqueue;
public class GroupQueue{
/**
* n is number of queues maintained. and queues is array of all queues maintained by
GroupQueue.
* here there are n groups . so maintain n queues and each queue represent one group.
*/
public int n;
public Queue[] queues;
public GroupQueue(int n){
this.n =n;
this.queues = new Queue[n]; //initialize array of queues with n number of groups
}
//is this GroupQueue empty ?
public boolean isEmpty() {
for(int i=0; i tempNode ;
for(int i=0; i newNode= new Node(item);
if(queues[gId].first== null){ //when queue is empty first and last point to same new node.
queues[gId].first= newNode;
queues[gId].last= newNode;
}
else{
queues[gId].last.next= newNode;
queues[gId].last= newNode;
}
}
public Item remove(){
if(isEmpty()) return null;
int i=0;
// find for a queue which has at leats one element.
while(queues[i].first== null){
i++;
}
// now ith queue will have at least one element
Node temp =queues[i].first; // return the first element from ith queue
queues[i].first= queues[i].first.next; //First variable will point to second element of queue now.
return temp.item;
}
// method to iterate through all elements of GroupQueue and prints them in ascending order of
group id.
// here for a one group , elements will be printed in FIFO manner of queue of that group .
// here no need to retun anything , as we are printing all elements in function itself.
public void iterator() {
Node temp;
for(int i=0 ; i{
public Item item;
public Node next;
public Node(Item item){
this.item= item ;
this.next= null;
}
}
public static class Item{
public int Itemdata;
public Item(int d){
this.Itemdata=d;
}
}
public static class Queue{
public Node first;
public Node last;
public Queue(){
first = last= null; //initially first and last pointer are null for each queue.
}
}
}

More Related Content

Similar to JAVAneed help with public IteratorItem iterator()import java.u.pdf

How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
mail931892
 
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 PriorityQueue.cs   Jim Mischel using System; using Sy.pdf PriorityQueue.cs   Jim Mischel using System; using Sy.pdf
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
rajat630669
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
EmptyCollectionException-java -- - Represents the situation in which.docx
EmptyCollectionException-java --  - Represents the situation in which.docxEmptyCollectionException-java --  - Represents the situation in which.docx
EmptyCollectionException-java -- - Represents the situation in which.docx
BlakeSGMHemmingss
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
gudduraza28
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
fcaindore
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
asarudheen07
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf
KUNALHARCHANDANI1
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
arihantpatna
 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdf
aioils
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdfI have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
JUSTSTYLISH3B2MOHALI
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdf
brijmote
 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdf
aggarwalopticalsco
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import  stdlib.;import java.util.Iterator;im.docxpackage algs13;import  stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
gerardkortney
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
angelsfashion1
 

Similar to JAVAneed help with public IteratorItem iterator()import java.u.pdf (20)

How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 PriorityQueue.cs   Jim Mischel using System; using Sy.pdf PriorityQueue.cs   Jim Mischel using System; using Sy.pdf
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
EmptyCollectionException-java -- - Represents the situation in which.docx
EmptyCollectionException-java --  - Represents the situation in which.docxEmptyCollectionException-java --  - Represents the situation in which.docx
EmptyCollectionException-java -- - Represents the situation in which.docx
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdf
 
I have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdfI have a stack in Java populated with integers. Im trying to compa.pdf
I have a stack in Java populated with integers. Im trying to compa.pdf
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdf
 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdf
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import  stdlib.;import java.util.Iterator;im.docxpackage algs13;import  stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
 

More from fcsondhiindia

in 4 paragraphs explain Profit Sharing in the US between Management .pdf
in 4 paragraphs explain Profit Sharing in the US between Management .pdfin 4 paragraphs explain Profit Sharing in the US between Management .pdf
in 4 paragraphs explain Profit Sharing in the US between Management .pdf
fcsondhiindia
 
I have the source code for three separate java programs (TestVending.pdf
I have the source code for three separate java programs (TestVending.pdfI have the source code for three separate java programs (TestVending.pdf
I have the source code for three separate java programs (TestVending.pdf
fcsondhiindia
 
Explain the common interest logic and the economic logic of .pdf
Explain the common interest logic and the economic logic of .pdfExplain the common interest logic and the economic logic of .pdf
Explain the common interest logic and the economic logic of .pdf
fcsondhiindia
 
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdfWrite 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
fcsondhiindia
 
Who are the people directly involved in the collective bargaining pr.pdf
Who are the people directly involved in the collective bargaining pr.pdfWho are the people directly involved in the collective bargaining pr.pdf
Who are the people directly involved in the collective bargaining pr.pdf
fcsondhiindia
 
Click to add title 2. Explain the concept of African Diaspora and dis.pdf
Click to add title 2. Explain the concept of African Diaspora and dis.pdfClick to add title 2. Explain the concept of African Diaspora and dis.pdf
Click to add title 2. Explain the concept of African Diaspora and dis.pdf
fcsondhiindia
 
ATP production during glycolysis;requires an enzymeoccurs in the.pdf
ATP production during glycolysis;requires an enzymeoccurs in the.pdfATP production during glycolysis;requires an enzymeoccurs in the.pdf
ATP production during glycolysis;requires an enzymeoccurs in the.pdf
fcsondhiindia
 

More from fcsondhiindia (20)

LUS (a) Prepare the jounal entry to recerd the proceeds of the note. .pdf
LUS (a) Prepare the jounal entry to recerd the proceeds of the note. .pdfLUS (a) Prepare the jounal entry to recerd the proceeds of the note. .pdf
LUS (a) Prepare the jounal entry to recerd the proceeds of the note. .pdf
 
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdf
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdfLet A be an n n matrix. Which of the following are TRUE I. If A i.pdf
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdf
 
How does PERT technique compare with estimating or forecasting techn.pdf
How does PERT technique compare with estimating or forecasting techn.pdfHow does PERT technique compare with estimating or forecasting techn.pdf
How does PERT technique compare with estimating or forecasting techn.pdf
 
in 4 paragraphs explain Profit Sharing in the US between Management .pdf
in 4 paragraphs explain Profit Sharing in the US between Management .pdfin 4 paragraphs explain Profit Sharing in the US between Management .pdf
in 4 paragraphs explain Profit Sharing in the US between Management .pdf
 
I have the source code for three separate java programs (TestVending.pdf
I have the source code for three separate java programs (TestVending.pdfI have the source code for three separate java programs (TestVending.pdf
I have the source code for three separate java programs (TestVending.pdf
 
How is the urinary system is involved in regulating the pH in bodily.pdf
How is the urinary system is involved in regulating the pH in bodily.pdfHow is the urinary system is involved in regulating the pH in bodily.pdf
How is the urinary system is involved in regulating the pH in bodily.pdf
 
Find a polynomial that describes the shaded area. SolutionArea.pdf
Find a polynomial that describes the shaded area.  SolutionArea.pdfFind a polynomial that describes the shaded area.  SolutionArea.pdf
Find a polynomial that describes the shaded area. SolutionArea.pdf
 
Explain the common interest logic and the economic logic of .pdf
Explain the common interest logic and the economic logic of .pdfExplain the common interest logic and the economic logic of .pdf
Explain the common interest logic and the economic logic of .pdf
 
Does privacy always guarantee integrity. Justify your answerSolu.pdf
Does privacy always guarantee integrity. Justify your answerSolu.pdfDoes privacy always guarantee integrity. Justify your answerSolu.pdf
Does privacy always guarantee integrity. Justify your answerSolu.pdf
 
Distinguish between the GAAP and IFRS standards for internal .pdf
Distinguish between the GAAP and IFRS standards for internal .pdfDistinguish between the GAAP and IFRS standards for internal .pdf
Distinguish between the GAAP and IFRS standards for internal .pdf
 
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdf
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdfDescribe how pH inhibit microbe growth.SolutionMost of the pat.pdf
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdf
 
Write the C code to create a data structure named window that con.pdf
Write the C code to create a data structure named window that con.pdfWrite the C code to create a data structure named window that con.pdf
Write the C code to create a data structure named window that con.pdf
 
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdfWrite 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
 
Who are the people directly involved in the collective bargaining pr.pdf
Who are the people directly involved in the collective bargaining pr.pdfWho are the people directly involved in the collective bargaining pr.pdf
Who are the people directly involved in the collective bargaining pr.pdf
 
Contracts or agreements can sometimes create financial contingencies.pdf
Contracts or agreements can sometimes create financial contingencies.pdfContracts or agreements can sometimes create financial contingencies.pdf
Contracts or agreements can sometimes create financial contingencies.pdf
 
Which of the following is an equation of the line that passes through.pdf
Which of the following is an equation of the line that passes through.pdfWhich of the following is an equation of the line that passes through.pdf
Which of the following is an equation of the line that passes through.pdf
 
When more than one structural arrangement of elements is possible fo.pdf
When more than one structural arrangement of elements is possible fo.pdfWhen more than one structural arrangement of elements is possible fo.pdf
When more than one structural arrangement of elements is possible fo.pdf
 
Click to add title 2. Explain the concept of African Diaspora and dis.pdf
Click to add title 2. Explain the concept of African Diaspora and dis.pdfClick to add title 2. Explain the concept of African Diaspora and dis.pdf
Click to add title 2. Explain the concept of African Diaspora and dis.pdf
 
Based on the following data, what is the quick ratio, rounded to one.pdf
Based on the following data, what is the quick ratio, rounded to one.pdfBased on the following data, what is the quick ratio, rounded to one.pdf
Based on the following data, what is the quick ratio, rounded to one.pdf
 
ATP production during glycolysis;requires an enzymeoccurs in the.pdf
ATP production during glycolysis;requires an enzymeoccurs in the.pdfATP production during glycolysis;requires an enzymeoccurs in the.pdf
ATP production during glycolysis;requires an enzymeoccurs in the.pdf
 

Recently uploaded

MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell Tolls
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 

JAVAneed help with public IteratorItem iterator()import java.u.pdf

  • 1. JAVA need help with public Iterator iterator() 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 implements Iterable { private Node first; private Node last; private int n; private static class Node { private Item item; private Node next; } public void Queue() {
  • 2. 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. */
  • 3. public void add(Item item, int gId) { Node oldlast = last; last = new Node(); 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() { Node temp = first; Node prev = first; Item minGid; if (! isEmpty()) { minGid = first.item; } else { return null; } while(temp!= null) { prev = temp; temp = temp.next; if (temp.item == minGid) {
  • 4. minGid = (Item) temp.item; } } if (minGid == first.item) { first = first.next; } else { prev.next = prev.next.next; } n--; return minGid; }// 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 iterator() { // TODO // BONUS (20 points) return null; } } Solution Implementation given here seems to have many logical errors. Here is the code given for GroupQueue: package groupqueue; public class GroupQueue{ /** * n is number of queues maintained. and queues is array of all queues maintained by
  • 5. GroupQueue. * here there are n groups . so maintain n queues and each queue represent one group. */ public int n; public Queue[] queues; public GroupQueue(int n){ this.n =n; this.queues = new Queue[n]; //initialize array of queues with n number of groups } //is this GroupQueue empty ? public boolean isEmpty() { for(int i=0; i tempNode ; for(int i=0; i newNode= new Node(item); if(queues[gId].first== null){ //when queue is empty first and last point to same new node. queues[gId].first= newNode; queues[gId].last= newNode; } else{ queues[gId].last.next= newNode; queues[gId].last= newNode; } } public Item remove(){ if(isEmpty()) return null; int i=0; // find for a queue which has at leats one element. while(queues[i].first== null){ i++; }
  • 6. // now ith queue will have at least one element Node temp =queues[i].first; // return the first element from ith queue queues[i].first= queues[i].first.next; //First variable will point to second element of queue now. return temp.item; } // method to iterate through all elements of GroupQueue and prints them in ascending order of group id. // here for a one group , elements will be printed in FIFO manner of queue of that group . // here no need to retun anything , as we are printing all elements in function itself. public void iterator() { Node temp; for(int i=0 ; i{ public Item item; public Node next; public Node(Item item){ this.item= item ; this.next= null; } } public static class Item{ public int Itemdata; public Item(int d){ this.Itemdata=d; } } public static class Queue{ public Node first; public Node last; public Queue(){
  • 7. first = last= null; //initially first and last pointer are null for each queue. } } }