Question: Create a JUNIT5 test for this driver class. Code of Driver Class: package package5; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.EmptyStackException; import java.util.List; import
Create a JUNIT5 test for this driver class.
Code of Driver Class:
package package5;
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.EmptyStackException; import java.util.List; import java.util.Scanner; import java.util.Stack;
public class Driver {
public static void main(String[] args) { /** * Keeping the expression.txt file in the same package where the source code * exists */ String filePath = "src\\package5\\expression.txt"; File file = new File(filePath);
/** * Operands are maintained in a list object */ List
try { Scanner sc = new Scanner(file); String input = sc.nextLine();
/** * tokenizing the expression based on empty spaces */ String[] tokens = input.split("\\s");
try { Stack
// Looping through the tokens for (int i = 0; i < tokens.length; i++) {
String str = tokens[i]; /** * if the token is contained in operand list, pop two items from the stack and * result is calculated, in case sufficint tokens are not found on stack, an * empty stack exception would be thrown and proper error message displayed */ if (operators.contains(str)) { double operand2 = stack.pop(); double operand1 = stack.pop(); double result = evaluate(operand1, operand2, str); stack.push(result); } else { /** * If it is not a operand, extracting the integer out of the token. if anything * other than integer is found, an exception will be thrown and handle in catch * clause */ int x = Integer.parseInt(str); stack.push((double) x); }
}
/** * after end of processing, finally stack should contain a single element which * is the result, if stack is empty or contains more elements, the expression is * a invalid */ if (stack.size() == 1) {
StringBuilder sb = new StringBuilder(); for (String token : tokens) { sb.append(token).append(" "); } sb.append("= "); sb.append(stack.pop()); System.out.println(sb.toString()); } else { System.out.println("Invalid expression"); }
} catch (EmptyStackException e) { System.out.println("Invalid expression"); } catch (Exception e) { System.out.println("Invalid expression"); }
sc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
private static double evaluate(double operand1, double operand2, String str) { double result = 0;
if (str.equals("+")) { result = operand1 + operand2; } else if (str.equals("-")) { result = operand1 - operand2; } else if (str.equals("*")) { result = operand1 * operand2; } else if (str.equals("/")) { try { result = operand1 / operand2; } catch (ArithmeticException e) { /** * In case of divided by zero case an Arthematic exception will be thrown and * ignoring the zero by assigning first operand to result */ result = operand1;
e.printStackTrace(); } }
return result; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
