Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ajprincess Group Title

Please help:) Can anyone please check the error in this code?

  • one year ago
  • one year ago

  • This Question is Closed
  1. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    import java.io.*; class Queue2 { private int maxSize; private int[] queueArr; private int front; private int rear; public Queue2(int s) { maxSize=s; queueArr=new int[maxSize]; front=0; rear=-1; } public void insert(int j) { if(rear==maxSize-1) rear=-1; queueArr[++rear]=j; } public int remove() { int temp=queueArr[front++]; if(front==maxSize) front=0; return temp; } public int peekFront() { return queueArr[front]; } public boolean isEmpty() { return (maxSize-1+front==rear); } public boolean isFull() { return (maxSize-2+front==rear); } } class QueueApp2 { public static void main(String [] args) throws IOException { BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in)); int a; int b; int c; String d; do { System.out.print("Enter the number of items to be inserted:"); a=Integer.parseInt(keyboard.readLine()); Queue theQueue=new Queue(a+1); for(int i=1;i<=a;i++) { System.out.print("Enter the item:"); b=Integer.parseInt(keyboard.readLine()); theQueue.insert(b); } System.out.print("Enter the number of items to be removed:"); c=Integer.parseInt(keyboard.readLine()); for(int k=1;k<=c;k++) { theQueue.remove(); } while(!theQueue.isEmpty()) { int n=theQueue.remove(); System.out.print(n); System.out.print(" "); } System.out.println(" "); System.out.print("Do you want to continue:"); d=keyboard.readLine(); } while(d!="Quit"); } }

    • one year ago
  2. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    it compiled for me. i got both the class files... could u tell me how to run, il test.. . .

    • one year ago
  3. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I saved this as Queue2.java. javac Queue2.java-for compiling java QueueApp2-to run

    • one year ago
  4. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    morpheus/users/durgat $ /tools/cfr/bin/java QueueApp2 Enter the number of items to be inserted:4 Enter the item:a Exception in thread "main" java.lang.NumberFormatException: For input string: "a" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.parseInt(Integer.java:497) at QueueApp2.main(QueueApp2.java:21)

    • one year ago
  5. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    it throws that... u seeing the same ?

    • one year ago
  6. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    its running fine now.... looks u declared the items as integers.. .

    • one year ago
  7. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    morpheus/users/durgat $ /tools/cfr/bin/java QueueApp2 Enter the number of items to be inserted:5 Enter the item:1 Enter the item:2 Enter the item:3 Enter the item:4 Enter the item:5 Enter the number of items to be removed:

    • one year ago
  8. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    ya. it asks whether we want to continue. if we input Quit it shud terminate bt it keeps on repeating. that's the problem I am havng in it.

    • one year ago
  9. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    yea i see its looping foreverin that last while loop seems !theQueue.isEmpty() is never becoming false - the fifo is never becoming empty. looks somthing worong with isEmpty method

    • one year ago
  10. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    return (maxSize-1+front==rear) here we testing if the front and rear are equal, if they're equal, the fifo is empty, right ?

    • one year ago
  11. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    ya

    • one year ago
  12. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    import java.io.*; class Queue2 { private int maxSize; private int[] queueArr; private int front; private int rear; public Queue2(int s) { maxSize=s; queueArr=new int[maxSize]; front=0; rear=-1; } public void insert(int j) { if(rear==maxSize-1) rear=-1; queueArr[++rear]=j; } public int remove() { int temp=queueArr[front++]; if(front==maxSize) front=0; return temp; } public int peekFront() { return queueArr[front]; } public boolean isEmpty() { return (maxSize-1+front==rear); } public boolean isFull() { return (maxSize-2+front==rear); } } class QueueApp2 { public static void main(String [] args) throws IOException { BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in)); int a; int b; int c; int d; do { System.out.print("Enter the number of items to be inserted:"); a=Integer.parseInt(keyboard.readLine()); Queue theQueue=new Queue(a+1); for(int i=1;i<=a;i++) { System.out.print("Enter the item:"); b=Integer.parseInt(keyboard.readLine()); theQueue.insert(b); } System.out.print("Enter the number of items to be removed:"); c=Integer.parseInt(keyboard.readLine()); for(int k=1;k<=c;k++) { theQueue.remove(); } while(!theQueue.isEmpty()) { int n=theQueue.remove(); System.out.print(n); System.out.print(" "); } System.out.println(" "); System.out.print("Enter 0 to exit:"); d=Integer.parseInt(keyboard.readLine()); } while(d!=0); } } if u run this code and enter 0 when it says to enter 0 to exit the program the program terminates. bt if we enter different number the program repeats. this is vat I want

    • one year ago
  13. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    its looping forever while removing items itself !

    • one year ago
  14. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    bt I dnt get that output when I declare d as string and do:(

    • one year ago
  15. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    hey could u tell me how this checks if the queue is empty : public boolean isEmpty() { return (maxSize-1+front==rear); }

    • one year ago
  16. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    i think the correct method should be as below : public boolean isEmpty() { return (front-1 == rear); }

    • one year ago
  17. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    that maxSize over there in ur method is throwing me off... im not able to get what its doing there.. .

    • one year ago
  18. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    public boolean isEmpty() { return ((maxSize-1)+front==rear);//or (front==rear+1); }

    • one year ago
  19. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    well maxSize is the size of the queue.

    • one year ago
  20. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    how below thing works : (maxSize-1)+front==rear

    • one year ago
  21. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    To be honest I am nt so clear with this code.:(

    • one year ago
  22. bookworm00981 Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I got lost while looking at all those stands of code for a sec

    • one year ago
  23. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @Chris2332 please help me:)

    • one year ago
  24. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @eSpeX please help me:)

    • one year ago
  25. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    This is the output I get when i run the second code. Enter the number of items to be inserted:5 Enter the item:12 Enter the item:13 Enter the item:14 Enter the item:15 Enter the item:16 Enter the number of items to be removed:2 14 15 16 Enter 0 to exit:0 The program does not repeat again This is the output I get when i run the second code. Enter the number of items to be inserted:5 Enter the item:12 Enter the item:13 Enter the item:14 Enter the item:15 Enter the item:16 Enter the number of items to be removed:2 14 15 16 Enter 0 to exit:5 Enter the number of items to be inserted:5 Enter the item:17 Enter the item:18 Enter the item:19 Enter the item:20 Enter the item:21 Enter the number of items to be removed:2 19 20 21 Enter 0 to exit:0

    • one year ago
  26. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    looks its working fine now. what u changed

    • one year ago
  27. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I declared d as int. that's the only difference I made

    • one year ago
  28. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I want the same output when I declare d as char or string.

    • one year ago
  29. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    ha.. did u try using .equals() for comparinng "Quit"

    • one year ago
  30. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    no. how do we use it?

    • one year ago
  31. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    could u paste the complete code u using in a txt file, so that i test it at my end. and if it works il get back ?

    • one year ago
  32. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    tha java source code file... pls attach the file..

    • one year ago
  33. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    sure:)

    • one year ago
  34. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    also if u have teamviewer we can do that..

    • one year ago
  35. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    But I do not have it:(

    • one year ago
    1 Attachment
  36. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    /tools/cfr/bin/javac Queue2.java Queue2.java:55: cannot find symbol symbol : class Queue location: class QueueApp2 Queue theQueue=new Queue(a+1); ^ Queue2.java:55: cannot find symbol symbol : class Queue location: class QueueApp2 Queue theQueue=new Queue(a+1); ^ 2 errors

    • one year ago
  37. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    im getting errors when compiling ur code as-it-is

    • one year ago
  38. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    pls check once.. and attach the code which worked/compiled for u

    • one year ago
  39. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    That's the code worked for me. I am using jdk1.7.0_07 and u?

    • one year ago
  40. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Queue2 theQueue=new Queue2(a+1); I thnk it will work fine when it is edited as above in urs.

    • one year ago
  41. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    thats right, i did the same yesterday.

    • one year ago
  42. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    so other than that, the code which u using is same as the file u just posted ?

    • one year ago
  43. hartnn Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    try this string d= "Quit" ........ while (! equalsIgnoreCase(String d))

    • one year ago
  44. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    when I edit it nd compile in mine it does nt show the correct output. ya the same code

    • one year ago
  45. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    huh it repeats during while loop itself. pls check the file u attached again

    • one year ago
  46. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    ya it is the same. vat is the output u r getting?

    • one year ago
  47. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    durgat/ajpr/v2 $ /tools/cfr/bin/java QueueApp2 Enter the number of items to be inserted:5 Enter the item:1 Enter the item:2 Enter the item:3 Enter the item:4 Enter the item:5 Enter the number of items to be removed:2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3

    • one year ago
  48. ganeshie8 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    it is repeating forever during removing stage itself.

    • one year ago
  49. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    oh I c:(

    • one year ago
  50. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @ganeshie8 I will include the pdf book. in that book in page number 111 u will find that code.

    • one year ago
  51. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    If you are trying to replicate the code in the book then you do have several omissions. One is when they declare the array 1 cell larger than maxSize to work around an anomaly where the queue can appear full and empty at the same time. There are also some discrepancies in your isEmpty() and isFull() methods, were these differences intentional?

    • one year ago
  52. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    For your while condition, try replacing this: System.out.print("Do you want to continue:"); d=keyboard.readLine(); } while(d!="Quit"); with this: // declare your string String exit = new String("Quit"); // declare your old string String d = new String(); ... System.out.print("Do you want to continue:"); d=keyboard.readLine(); } while(!d.equalsIgnoreCase(exit));

    • one year ago
  53. KonradZuse Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Wowza this is long.

    • one year ago
  54. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    They wanted us to declare the array size 1 greater than the number of items to be inserted. System.out.print("Enter the number of items to be inserted:"); a=Integer.parseInt(keyboard.readLine()); Queue theQueue=new Queue(a+1); Also I really dnt get the conditions of isFull() nd isEmpty() methods. i jst included the one my instructor gave us.:( If u dnt mind will u be able to explain me that @eSpeX? after changing it in the way u suggested it worked fine. thank u soooooooooooo much.

    • one year ago
  55. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    The logic in the isFull() basically says that if your rear pointer is pointing to the index of maxSize (plus two will put it in index[0]), or your front is pointing at index 0, the array is full. Similarly the isEmpty() looks to test if your rear pointer and your front pointer are both pointing to the location of maxSize - 1 as an indication the array is empty.

    • one year ago
  56. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Reallyyy sorrry for troubling you. I still dont get it. If u dnt mind can u please explain it a bit further. maybe with an example of maxSize.

    • one year ago
  57. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @ganeshie8 try running this one

    • one year ago
    1 Attachment
  58. eSpeX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    Let's say your max size is 10 because you had items 's=9' and you're creating your array to 's+1', then you have an index of 0 - 9. isEmpty() tests the conditions: (front==rear+1)||(front+maxSize-1==rear), so your initial conditions are front = 0 rear = -1 So, if front == rear + 1 you have 'if 0 == -1+1', this means that both of your pointers are now looking at the beginning of the array. Similarly, if after your operations your rear is pointing at the last location (9 in this example) and your front is pointing to maxSize-1 then you have 0+(10-1)==9, because your maxSize is s+1. Through similar logic you can examine your isFull() method. (front==rear+2)||(front+maxSize-2==rear) the logic is applied after you insert items into the array, so if your front = 0 and your rear has reached the end of the items, 8, then the logic tests to see if 0 == 8 + 2, the counter will loop on itself first pointing at index 9 and then at index 0. The other direction is also true, if your rear pointer is on index 8, then front+maxSize-2==rear translates to 0+(10-2)==8

    • one year ago
  59. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank u sooo much. it makes sense nw. u r the best:)

    • one year ago
    • Attachments:

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
  • 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.