A community for students.
Here's the question you clicked on:
 0 viewing

This Question is Closed

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0import 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==maxSize1) 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 (maxSize1+front==rear); } public boolean isFull() { return (maxSize2+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
 2 years ago
Best ResponseYou've already chosen the best response.0it compiled for me. i got both the class files... could u tell me how to run, il test.. . .

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0I saved this as Queue2.java. javac Queue2.javafor compiling java QueueApp2to run

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0morpheus/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
 2 years ago
Best ResponseYou've already chosen the best response.0it throws that... u seeing the same ?

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0its running fine now.... looks u declared the items as integers.. .

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0morpheus/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
 2 years ago
Best ResponseYou've already chosen the best response.0ya. 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
 2 years ago
Best ResponseYou've already chosen the best response.0yea 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
 2 years ago
Best ResponseYou've already chosen the best response.0return (maxSize1+front==rear) here we testing if the front and rear are equal, if they're equal, the fifo is empty, right ?

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0import 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==maxSize1) 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 (maxSize1+front==rear); } public boolean isFull() { return (maxSize2+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
 2 years ago
Best ResponseYou've already chosen the best response.0its looping forever while removing items itself !

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0bt I dnt get that output when I declare d as string and do:(

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0hey could u tell me how this checks if the queue is empty : public boolean isEmpty() { return (maxSize1+front==rear); }

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0i think the correct method should be as below : public boolean isEmpty() { return (front1 == rear); }

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0that maxSize over there in ur method is throwing me off... im not able to get what its doing there.. .

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0public boolean isEmpty() { return ((maxSize1)+front==rear);//or (front==rear+1); }

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0well maxSize is the size of the queue.

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0how below thing works : (maxSize1)+front==rear

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0To be honest I am nt so clear with this code.:(

bookworm00981
 2 years ago
Best ResponseYou've already chosen the best response.1I got lost while looking at all those stands of code for a sec

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0@Chris2332 please help me:)

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0@eSpeX please help me:)

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0This 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
 2 years ago
Best ResponseYou've already chosen the best response.0looks its working fine now. what u changed

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0I declared d as int. that's the only difference I made

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0I want the same output when I declare d as char or string.

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0ha.. did u try using .equals() for comparinng "Quit"

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0no. how do we use it?

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0could 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
 2 years ago
Best ResponseYou've already chosen the best response.0tha java source code file... pls attach the file..

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0also if u have teamviewer we can do that..

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0But I do not have it:(

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.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

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0im getting errors when compiling ur code asitis

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0pls check once.. and attach the code which worked/compiled for u

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0That's the code worked for me. I am using jdk1.7.0_07 and u?

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0Queue2 theQueue=new Queue2(a+1); I thnk it will work fine when it is edited as above in urs.

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0thats right, i did the same yesterday.

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0so other than that, the code which u using is same as the file u just posted ?

hartnn
 2 years ago
Best ResponseYou've already chosen the best response.0try this string d= "Quit" ........ while (! equalsIgnoreCase(String d))

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0when I edit it nd compile in mine it does nt show the correct output. ya the same code

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0huh it repeats during while loop itself. pls check the file u attached again

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0ya it is the same. vat is the output u r getting?

ganeshie8
 2 years ago
Best ResponseYou've already chosen the best response.0durgat/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
 2 years ago
Best ResponseYou've already chosen the best response.0it is repeating forever during removing stage itself.

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0@ganeshie8 I will include the pdf book. in that book in page number 111 u will find that code.

eSpeX
 2 years ago
Best ResponseYou've already chosen the best response.2If 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
 2 years ago
Best ResponseYou've already chosen the best response.2For 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));

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0They 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
 2 years ago
Best ResponseYou've already chosen the best response.2The 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
 2 years ago
Best ResponseYou've already chosen the best response.0Reallyyy 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
 2 years ago
Best ResponseYou've already chosen the best response.0@ganeshie8 try running this one

eSpeX
 2 years ago
Best ResponseYou've already chosen the best response.2Let'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+maxSize1==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 maxSize1 then you have 0+(101)==9, because your maxSize is s+1. Through similar logic you can examine your isFull() method. (front==rear+2)(front+maxSize2==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+maxSize2==rear translates to 0+(102)==8

ajprincess
 2 years ago
Best ResponseYou've already chosen the best response.0Thank u sooo much. it makes sense nw. u r the best:)
Ask your own question
Sign UpFind 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
 Engagement 19 Mad Hatter
 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.