Ace school

with brainly

  • Get help from millions of students
  • Learn from experts with step-by-step explanations
  • Level-up by helping others

A community for students.

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

Computer Science
See more answers at brainly.com
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.

Join Brainly to access

this expert answer

SIGN UP FOR FREE
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"); } }
it compiled for me. i got both the class files... could u tell me how to run, il test.. . .
I saved this as Queue2.java. javac Queue2.java-for compiling java QueueApp2-to run

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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

Not the answer you are looking for?

Search for more explanations.

Ask your own question