Question: 1 Implement the following classes: Calculator, InvalidExpressionException, and Assign06_1. InvalidExpressionException extends Exception Has constructor that takes String message and Exception e; call super(message, e) in
1 Implement the following classes: Calculator, InvalidExpressionException, and Assign06_1. InvalidExpressionException extends Exception Has constructor that takes String message and Exception e; call super(message, e) in this constructor. The Calculator class contains one static method: public static double eval(String expr) throws InvalidExpressionException This method only has to deal with POSITIVE real numbers. This method only has to handle 2-number expressions (e.g., 5 + 78). This method only has to handle the following operators: +, - Do NOT assume there are spaces in the expression! E.g., 5+6 or 5 + 6 or 5 + 6 are all valid expressions. All of the code that follows should be in a try block. Find the first index of any of the above operators. Take two operands and convert them to doubles using Double.parseDouble(). Perform the operation requested (addition or subtraction). Catch Exception, but rethrow as an InvalidExpressionException. DO NOT FORGET TO PASS IN THE OLD EXCEPTION TO InvalidExpressionExceptions CONSTRUCTOR. You do NOT need to distinguish between different kinds of errors! If the code in this method throws ANY exception, it will rethrow it as an InvalidExpressionException. Do not worry about specifying different messages. Assign06_1 only has a main() method. All the code that follows should be in a try block. Ask user to enter an expression. Read in line as expression. Use Calculator.eval() to get the answer. Print the answer. Catch InvalidExpressionException and print exception message AND stack trace. INPUT: 3 + 5 Answer is 8.0 INPUT: 5.0 - 8.2 Answer is -3.1999999999999993 INPUT: 3x + 5 Invalid expression InvalidExpressionException: Invalid expression (Rest of stack trace) INPUT: 6 / 7 Invalid expression InvalidExpressionException: Invalid expression (Rest of stack trace)
2.Implement the following classes: WordCountData, WordCounter and Assign06_2. WordCountData Contains three PRIVATE ints: charCnt, wordCnt, lineCnt Constructor that takes number of characters, number of words, and number of lines; sets data fields accordingly. Include getter methods for all three fields. WordCounter has one method: public static WordCountData count(String path) throws Exception If the String path contains http, then create a Scanner from a URL. Otherwise, create a Scanner from a File. Read through the data and count up the number of characters, the number of words, and the number of lines. A word is considered a string of text separated from other strings of text by (any amount of) whitespace. Example 1: A dogs life has 3 words: A, dogs, and life. Example 2: Also,not sure actually has TWO words, because theres no whitespace between Also, and not: Also,not and sure are the two words. Example 3: Happy joy has TWO words. You should NOT assume only a single space between words. Close Scanner object. Put this data into a WordCountData object and return it. Assign06_2 only has a main() method. All the code that follows should be in a try block. Ask the user to enter a file path or a URL. Read in next line as path. Call WordCounter.count() and get the WordCountData object. Print out the number of characters, the number of words, and the number of lines. Create a PrintWriter that writes to the file output.txt. Write the path string on the first line. Write the number of characters, the number of words, and the number of lines to the file. Close the file. Catch Exception and print OH NO! AND the stack trace. INPUT: http://cs.armstrong.edu/liang/data/Lincoln.txt OUTPUT: Number of characters: 1469 Number of words: 277 Number of lines: 16 INPUT: gameData.txt OUTPUT: Number of characters: 385 Number of words: 42 Number of lines: 29
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
