Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
ajprincess
Group Title
Please help:)
Implement the following operation in Queue using array based implementation in java
You are required to create multiple classes and methods to perform the following with queue.
1.insert the following letters into the queue using keyboard input.
{a, b, c, d, e, f, g, h, i}
2.Remove some of the letters from the queue.
3.Insert some more letters into the queue
4.Print the remaining letters in the queue in order
5.write a method to check whether the queue is empty.
6.write a method to check whether the queue is full.
 one year ago
 one year ago
ajprincess Group Title
Please help:) Implement the following operation in Queue using array based implementation in java You are required to create multiple classes and methods to perform the following with queue. 1.insert the following letters into the queue using keyboard input. {a, b, c, d, e, f, g, h, i} 2.Remove some of the letters from the queue. 3.Insert some more letters into the queue 4.Print the remaining letters in the queue in order 5.write a method to check whether the queue is empty. 6.write a method to check whether the queue is full.
 one year ago
 one year ago

This Question is Closed

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
7.Write a method to print the number of elements in the queue.
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
import java.io.*; class Queue3 { private int maxSize; private char[] queueArr; private int front; private int rear; private int nItems; public Queue3(int s) { maxSize=s; queueArr=new char[maxSize]; front=0; rear=1; nItems=0; } public void insert(char letter) { if(rear==maxSize1) rear=1; queueArr[++rear]=letter; nItems++; } public char remove() { char temp=queueArr[front++]; if(front==maxSize) front=0; nItems; return temp; } public char peekFront() { return queueArr[front]; } public boolean isEmpty() { return (nItems==0); } public boolean isFull() { return (nItems==maxSize); } public int size() { return nItems; } } class QueueApp3 { public static void main(String [] args) throws IOException { BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in)); int a; int b; char c; int d; System.out.print("Enter the size of the array:"); a=Integer.parseInt(keyboard.readLine()); Queue3 theQueue=new Queue3(a); System.out.print("Enter the number of items to be inserted:"); b=Integer.parseInt(keyboard.readLine()); for(int i=1;i<=b;i++) { System.out.print("Enter the item:"); c=(char)keyboard.read(); theQueue.insert(c); } System.out.print("Enter the number of items to be removed:"); d=Integer.parseInt(keyboard.readLine()); for(int k=1;k<=d;k++) { theQueue.remove(); } System.out.print("Enter the number of items to be inserted:"); b=Integer.parseInt(keyboard.readLine()); for(int i=1;i<=b;i++) { System.out.print("Enter the item:"); c=(char)keyboard.read(); theQueue.insert(c); } while(!theQueue.isEmpty()) { char n=theQueue.remove(); System.out.print(n); System.out.print(" "); } System.out.println(" "); } }
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
My output has to be Enter the size of the array: 20 Enter the number of items to be added: 9 Enter the item: a Enter the item: b Enter the item: c Enter the item: d Enter the item: e Enter the item: f Enter the item: g Enter the item: h Enter the item: i Enter the number of items to be removed: 4 Enter the number of items to be added: 6 Enter the item: j Enter the item: k Enter the item: l Enter the item: m Enter the item: n Enter the item: o e f g h i j k l m n o
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
@KonradZuse. if u dnt mind can u please help me?
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
Seems like your issue is removing, readding, etc. You also ask for an array size, then number size.
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
If u dnt mind can u pls tell me whr I have made the mistake in my code?
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
your second class. Edit it... Instead of doing enter total, then enter total amount, then enter each #, then remove, then enter, try to fix that. Ask them how many numbers, then what they are. Next I would have a method to remove data randomly. Not at any set point. So you might need to say, do you want to enter, or remove. Also not sure if the additions /removals arejust at the end.
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
Sorry. Actually they want it that way. that is y I did so. bt I am nt getting that output.:(
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
They want you to add, remove, then add again? It seemed like add, and remove whenever.... Right not yout code asks for 20 spots, then 9 sports. IT then deletes the first 4 spots as you mentioned above, then added in the new letters at the end. Change where the letters are being deleted I guess if it's at the end and not random.
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
in a queue the letters will be deleted from front right? What i am nt getting is c=(char)keyboard.read(); theQueue.insert(c);. it is not looping.
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
it looks like it's looping to me... I guess you do the front, since tha'ts a queue but you cand elete anywhere...
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
ya. bt hw can we delete from anywhere. Really sorry for troubling u. I dnt get t. In fact I tried evrywhere chckng why it is nt looping. bt I was nt unable to find any solution. Please help
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
@KonradZuse
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
IT IS looping...
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
System.out.print("Enter the number of items to be inserted:"); b=Integer.parseInt(keyboard.readLine()); for(int i=1;i<=b;i++) { System.out.print("Enter the item:"); c=(char)keyboard.read(); theQueue.insert(c); }
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
Enter the item: j Enter the item: k Enter the item: l Enter the item: m Enter the item: n Enter the item: o e f g h i j k l m n o
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
but y it is not looping for me:(
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
it is, why are you saying it isn't...? Am I mising something? It does everything it should imo.
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
Enter the size of the array: 20 Enter the number of items to be added: 9 Enter the item: a Enter the item: Enter the item: Enter the item: Enter the item: Enter the item: Enter the item: Enter the item: Enter the item: Enter the number of items to be removed: 2 Enter the number of items to be added: 6 Enter the item: j Enter the item: Enter the item: Enter the item: Enter the item: nEnter the item: This is the output I am getting.:(
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
@KonradZuse
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
hmm Let me take a look at the code in my IDE later for you.
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
That while loop makes no sense at the bottom.
 one year ago

eSpeX Group TitleBest ResponseYou've already chosen the best response.1
The problem you are having is a result of your "keyboard.read()". The first time through it asks you to enter the item you type "a", then you hit enter, what gets passed to your insert function is 'a'. The next time through you type "b", then hit enter, what gets passed to your insert function is '\r'. The next time through you type "c", then hit enter, what gets passed to your insert function is '\n'. This can be fixed by changing your keyboard from a BufferedReader to a Scanner, also, since your method can throw an exception, you need to handle it with a try/catch block.
 one year ago

ajprincess Group TitleBest ResponseYou've already chosen the best response.0
Thanxxxx a lot @eSpex nd thax to u too @KonradZuse
 one year ago

KonradZuse Group TitleBest ResponseYou've already chosen the best response.0
I kept looking at that BufferReader but wasn't sure if it was another way to do it. What is /r and /n?
 one year ago

eSpeX Group TitleBest ResponseYou've already chosen the best response.1
\r is the return key and \n is the newline character.
 one year ago
See more questions >>>
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.