Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ajprincess

  • 2 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
  1. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  2. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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==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(" "); } }

  3. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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

  4. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  5. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  6. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ya

  7. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  8. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  9. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  10. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  11. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  12. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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...

  13. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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

  14. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @KonradZuse

  15. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    IT IS looping...

  16. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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); }

  17. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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

  18. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    but y it is not looping for me:(

  19. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  20. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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.:(

  21. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @KonradZuse

  22. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  23. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    That while loop makes no sense at the bottom.

  24. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @eSpex

  25. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  26. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    http://pastebin.com/wz7crjvD

  27. ajprincess
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  28. KonradZuse
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  29. eSpeX
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

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

  30. Not the answer you are looking for?
    Search for more explanations.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find 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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.