Ask your own question, for FREE!
Computer Science 77 Online
OpenStudy (ajprincess):

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.

OpenStudy (ajprincess):

7.Write a method to print the number of elements in the queue.

OpenStudy (ajprincess):

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==maxSize-1) 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(" "); } }

OpenStudy (ajprincess):

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

OpenStudy (ajprincess):

@KonradZuse. if u dnt mind can u please help me?

OpenStudy (konradzuse):

Seems like your issue is removing, re-adding, etc. You also ask for an array size, then number size.

OpenStudy (ajprincess):

ya

OpenStudy (ajprincess):

If u dnt mind can u pls tell me whr I have made the mistake in my code?

OpenStudy (konradzuse):

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.

OpenStudy (ajprincess):

Sorry. Actually they want it that way. that is y I did so. bt I am nt getting that output.:(

OpenStudy (konradzuse):

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.

OpenStudy (ajprincess):

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.

OpenStudy (konradzuse):

it looks like it's looping to me... I guess you do the front, since tha'ts a queue but you cand elete anywhere...

OpenStudy (ajprincess):

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

OpenStudy (ajprincess):

@KonradZuse

OpenStudy (konradzuse):

IT IS looping...

OpenStudy (konradzuse):

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); }

OpenStudy (konradzuse):

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

OpenStudy (ajprincess):

but y it is not looping for me:(

OpenStudy (konradzuse):

it is, why are you saying it isn't...? Am I mising something? It does everything it should imo.

OpenStudy (ajprincess):

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.:(

OpenStudy (ajprincess):

@KonradZuse

OpenStudy (konradzuse):

hmm Let me take a look at the code in my IDE later for you.

OpenStudy (konradzuse):

That while loop makes no sense at the bottom.

OpenStudy (ajprincess):

@eSpex

OpenStudy (espex):

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.

OpenStudy (espex):

http://pastebin.com/wz7crjvD

OpenStudy (ajprincess):

Thanxxxx a lot @eSpex nd thax to u too @KonradZuse

OpenStudy (konradzuse):

I kept looking at that BufferReader but wasn't sure if it was another way to do it. What is /r and /n?

OpenStudy (espex):

\r is the return key and \n is the newline character.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
arrivh: whats 8 times 99
10 seconds ago 6 Replies 1 Medal
Sowny17: pls help
29 minutes ago 83 Replies 1 Medal
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!