A community for students.
Here's the question you clicked on:
 0 viewing
ajprincess
 4 years ago
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.
ajprincess
 4 years ago
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.

This Question is Closed

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.07.Write a method to print the number of elements in the queue.

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.0import 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(" "); } }

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.0My 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

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.0@KonradZuse. if u dnt mind can u please help me?

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

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

KonradZuse
 4 years ago
Best ResponseYou've already chosen the best response.0your 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.

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

KonradZuse
 4 years ago
Best ResponseYou've already chosen the best response.0They 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.

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.0in 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.

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

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.0ya. 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

KonradZuse
 4 years ago
Best ResponseYou've already chosen the best response.0System.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); }

KonradZuse
 4 years ago
Best ResponseYou've already chosen the best response.0Enter 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

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.0but y it is not looping for me:(

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

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.0Enter 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.:(

KonradZuse
 4 years ago
Best ResponseYou've already chosen the best response.0hmm Let me take a look at the code in my IDE later for you.

KonradZuse
 4 years ago
Best ResponseYou've already chosen the best response.0That while loop makes no sense at the bottom.

eSpeX
 4 years ago
Best ResponseYou've already chosen the best response.1The 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.

ajprincess
 4 years ago
Best ResponseYou've already chosen the best response.0Thanxxxx a lot @eSpex nd thax to u too @KonradZuse

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

eSpeX
 4 years ago
Best ResponseYou've already chosen the best response.1\r is the return key and \n is the newline character.
Ask your own question
Sign UpFind more explanations on OpenStudy
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.