throw FooException();rather than:
throw new FooException();
class RecognitionError extends Exception { ... } class MismatchedTokenError extends RecognitionError { ... } class InvalidTokenError extends RecognitionError { ... } class NoAvailableAlternativeError extends RecognitionError { ... } class MismatchedNumberError extends MismatchedTokenError { ... } class MismatchedOperatorError extends MismatchedTokenError { ... }For each of the following Java code snippets, explain what is wrong with the snippet and how you would fix it.
try { ... } catch (RecognitionError re) { ... } catch (InvalidTokenError te) { ... } catch (MismatchedOperatorError oe) { ... }
public void evaluate () { ... if (error condition) { throw new InvalidTokenError(); } ... }
class NegativeNumberError extends Exception { int index; public NegativeNumberError(int i) { index = i; } int getIndex() { return index; } public String getMessage() { return "Negative number at index " + index; } } int sumArray(int x[]) throws NegativeNumberError { int i; int sum = 0; try { for (i = 0; i < x.length; i++) { if (x[i] < 0) throw new NegativeNumberError(i); sum += x[i]; } } catch (NegativeNumberError ne) { System.out.println(ne.getMessage()); throw ne; } finally { System.out.printf("sum = %d%n", sum); } return sum; } public int printSum() { int numbers[] = { 10, 20, 40, -20, 30, -40 }; int sum = 0; try { sum = sumArray(numbers); } catch (NegativeNumberError ne) { System.out.printf("Negative number is %d%n", numbers[ne.getIndex()]); } System.out.printf("sum = %d%n", sum); return sum; }
The API that you will implement is:
void *queue_new(int numItems); // create a queue that can hold the indicated number of items void queue_enqueue(void *q, void *item); // add the element to the back of the queue void *queue_dequeue(void *q); // remove and return the element at the front of the queue int queue_isEmpty(void *q); // 1 if empty and 0 otherwiseI have included several files for this problem:
You need to observe the following requirements for these two classes:
"queue full: cannot enqueue" + string form of the itemThe EmptyQueue version should return a string of the form:
queue empty: cannot dequeueIn Java the toString method ensures that the object can print itself. You may assume that in the C++ version that objects have overloaded the << and string operators to produce strings on request.
If the user tries to input any other command, your driver should print the error message:
invalid command: user-entered commandwhere user-entered command is the command the user tried to enter.
Here is a sample input file (I know I'm using integers but input them as string--our test cases will include normal strings):
3 enqueue 5 enqueue 8 print dequeue enqueue 10 enqueue 11 enqueue 20 print dequeue dequeue dequeue dequeue dequeue enqueue 15 print dequeue
You may assume that the input is syntactically correct, but that it may cause the queue to either overflow or underflow (i.e., dequeues from an empty queue). If the input tries to enqueue an item and the queue throws an overflow exception, print the string returned by getMessage(), ignore the item and continue. If the input tries to dequeue an item and the queue throws an empty queue exception, print the string returned by getMessage(), ignore the command, and continue. As an example, your output based on the above input must be (your driver program will be printing the "dequeue value" string):
queue contents 5 8 dequeue: 5 queue full: cannot enqueue 20 queue contents 8 10 11 dequeue: 8 dequeue: 10 dequeue: 11 queue empty: cannot dequeue queue empty: cannot dequeue queue contents 15 dequeue: 15
It is highly likely that the TAs will be using script grading so please ensure that your output matches the output produced by my executable.
Other information that may be useful:
java -jar Queue.jar < q5Inputand have your queue driver run.