Use an ascii text editor or word processor to answer questions 1-5. Submission instructions are at the end of this assignment.
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; }
3 -5 brad 8 nelsthe program would print the sum 11 for that line.
These exceptions should be thrown to the calling methods.
Your driver should take its input from stdin and test your queue by creating a queue of strings. The first line of your input should be the maximum number of items in the queue. Each succeeding line should start with one of the following two commands:
For example (I know I'm using integers but input them as string--our test cases will include normal strings):
3 enqueue 5 enqueue 8 dequeue enqueue 10 enqueue 11 enqueue 20 dequeue dequeue dequeue dequeue dequeue enqueue 15 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 an understandable error message, ignore the item and continue. If the input tries to dequeue an item and the queue throws an empty queue exception, print an understandable error message, ignore the command, and continue. As an example, your output based on the above input might be:
5 queue full: cannot enqueue 20 8 10 11 queue empty: cannot dequeue queue empty: cannot dequeue 15
Remember to close your scanner after you process each line. You need to do so regardless of whether or not the line contained a command that executed properly. Hence you will probably need to use a finally clause to close your scanner.
java -jar SumLines.jar inputand have SumLines run.
java -jar Queue.jar < queueInputand have your queue driver run.