Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Implement the following classes: Calculator, InvalidExpressionException, and A

ID: 3705600 • Letter: 1

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 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 InvalidExpressionException’s 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 dog’s life” has 3 words: ”A”, ”dog’s”, and ”life”. Example 2: ”Also,not sure” actually has TWO words, because there’s 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

Explanation / Answer

Let me know if you have any doubts.

Answer1)  

//InvalidExpressionException.java

public class InvalidExpressionException extends Exception{

//constructor calling the Exception constructor

public InvalidExpressionException(String msg,Exception e) {

super(msg,e);

}

}

//Calculator.java

public class Calculator {

public static double eval(String expr) throws InvalidExpressionException{

//two initial vars

Double operand1=0.0d,operand2=0.0d;

Double result=0.0d;

try {

//if add operation

if(expr.contains("+")) {

String ar[]=expr.split("\+");

operand1=Double.valueOf(ar[0].trim());

operand2=Double.valueOf(ar[1].trim());

result=operand1+operand2;

}

//if - operation

else if(expr.contains("-")) {

String ar[]=expr.split("\-");

operand1=Double.parseDouble(ar[0].trim());

operand2=Double.parseDouble(ar[1].trim());

result=operand1-operand2;

}

//for any other

else {

Double.parseDouble(expr);

}

}

catch(Exception e) {

//throwing the exception

throw new InvalidExpressionException("Invalid Expression", e);

}

return result;

}

}

//Assign06_1.java

import java.util.Scanner;

public class Assign06_1 {

public static void main(String[] args) {

//in try catch asking input and catching exception

try {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the expression : ");

String input=sc.nextLine();

Double result=Calculator.eval(input);

System.out.println("INPUT: "+input);

System.out.println("Answer is : "+result);

}

catch(InvalidExpressionException ie) {

System.out.println(ie.getMessage());

ie.printStackTrace();

}

}

}