ajprincess
  • ajprincess
Please help:) Can anyone please check the error in this code?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
ajprincess
  • ajprincess
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"); } }
ganeshie8
  • ganeshie8
it compiled for me. i got both the class files... could u tell me how to run, il test.. . .
ajprincess
  • ajprincess
I saved this as Queue2.java. javac Queue2.java-for compiling java QueueApp2-to run

Looking for something else?

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

More answers

ganeshie8
  • ganeshie8
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)
ganeshie8
  • ganeshie8
it throws that... u seeing the same ?
ganeshie8
  • ganeshie8
its running fine now.... looks u declared the items as integers.. .
ganeshie8
  • ganeshie8
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:
ajprincess
  • ajprincess
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.
ganeshie8
  • ganeshie8
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
ganeshie8
  • ganeshie8
return (maxSize-1+front==rear) here we testing if the front and rear are equal, if they're equal, the fifo is empty, right ?
ajprincess
  • ajprincess
ya
ajprincess
  • ajprincess
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
ganeshie8
  • ganeshie8
its looping forever while removing items itself !
ajprincess
  • ajprincess
bt I dnt get that output when I declare d as string and do:(
ganeshie8
  • ganeshie8
hey could u tell me how this checks if the queue is empty : public boolean isEmpty() { return (maxSize-1+front==rear); }
ganeshie8
  • ganeshie8
i think the correct method should be as below : public boolean isEmpty() { return (front-1 == rear); }
ganeshie8
  • ganeshie8
that maxSize over there in ur method is throwing me off... im not able to get what its doing there.. .
ajprincess
  • ajprincess
public boolean isEmpty() { return ((maxSize-1)+front==rear);//or (front==rear+1); }
ajprincess
  • ajprincess
well maxSize is the size of the queue.
ganeshie8
  • ganeshie8
how below thing works : (maxSize-1)+front==rear
ajprincess
  • ajprincess
To be honest I am nt so clear with this code.:(
anonymous
  • anonymous
I got lost while looking at all those stands of code for a sec
ajprincess
  • ajprincess
@Chris2332 please help me:)
ajprincess
  • ajprincess
@eSpeX please help me:)
ajprincess
  • ajprincess
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
ganeshie8
  • ganeshie8
looks its working fine now. what u changed
ajprincess
  • ajprincess
I declared d as int. that's the only difference I made
ajprincess
  • ajprincess
I want the same output when I declare d as char or string.
ganeshie8
  • ganeshie8
ha.. did u try using .equals() for comparinng "Quit"
ajprincess
  • ajprincess
no. how do we use it?
ganeshie8
  • ganeshie8
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 ?
ganeshie8
  • ganeshie8
tha java source code file... pls attach the file..
ajprincess
  • ajprincess
sure:)
ganeshie8
  • ganeshie8
also if u have teamviewer we can do that..
ajprincess
  • ajprincess
But I do not have it:(
1 Attachment
ganeshie8
  • ganeshie8
/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
ganeshie8
  • ganeshie8
im getting errors when compiling ur code as-it-is
ganeshie8
  • ganeshie8
pls check once.. and attach the code which worked/compiled for u
ajprincess
  • ajprincess
That's the code worked for me. I am using jdk1.7.0_07 and u?
ajprincess
  • ajprincess
Queue2 theQueue=new Queue2(a+1); I thnk it will work fine when it is edited as above in urs.
ganeshie8
  • ganeshie8
thats right, i did the same yesterday.
ganeshie8
  • ganeshie8
so other than that, the code which u using is same as the file u just posted ?
hartnn
  • hartnn
try this string d= "Quit" ........ while (! equalsIgnoreCase(String d))
ajprincess
  • ajprincess
when I edit it nd compile in mine it does nt show the correct output. ya the same code
ganeshie8
  • ganeshie8
huh it repeats during while loop itself. pls check the file u attached again
ajprincess
  • ajprincess
ya it is the same. vat is the output u r getting?
ganeshie8
  • ganeshie8
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
ganeshie8
  • ganeshie8
it is repeating forever during removing stage itself.
ajprincess
  • ajprincess
oh I c:(
ajprincess
  • ajprincess
@ganeshie8 I will include the pdf book. in that book in page number 111 u will find that code.
eSpeX
  • eSpeX
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?
eSpeX
  • eSpeX
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));
KonradZuse
  • KonradZuse
Wowza this is long.
ajprincess
  • ajprincess
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.
eSpeX
  • eSpeX
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.
ajprincess
  • ajprincess
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.
ajprincess
  • ajprincess
@ganeshie8 try running this one
1 Attachment
eSpeX
  • eSpeX
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
ajprincess
  • ajprincess
Thank u sooo much. it makes sense nw. u r the best:)

Looking for something else?

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