import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* UNatCalculator.java ** Author: R. McCloskey ** Date: February 2019 (updated Feb. 2026) ** ** Java application having as its purpose to test the UnboundedNatural ** class. It serves as, in effect, a calculator that interprets each ** line of input as an expression to be evaluated, where the expression ** is expected to have the form ** ** ** ** An is a sequence of (decimal) digits and an ** is any of "=", "+", "-", or "*". ** Examples: "4509345 - 23456", "98345643345543333 * 4678023" ** ** In response to each expression provided as input, the program displays ** the result of applying the given operator to the given numbers, making ** use of the methods of the UnboundedNatural class. ** ** If a command line argument is provided, it is taken to be the name of ** the file containing input data. Otherwise, input is read from standard ** input. A blank line entered as input is taken to mean that the program ** is to terminate. */ public class Calculator { public static void main(String[] args) throws FileNotFoundException { Scanner scanner; boolean echoInput; if (args.length != 0) { scanner = new Scanner(new File(args[0])); echoInput = true; } else { scanner = new Scanner(System.in); echoInput = false; } System.out.println("Welcome to the UnboundedNatural Calculator!\n"); boolean keepGoing = true; System.out.print("> "); while (keepGoing) { String expression = scanner.nextLine().trim(); if (expression.length() == 0) { keepGoing = false; } else { if (echoInput) { System.out.println(expression); } try { evaluateExpression(expression); } catch (Exception e) { e.printStackTrace(System.out); } System.out.print("\n> "); } } System.out.println("Goodbye."); } /* Evaluates the given expression and displays the result. */ public static void evaluateExpression(String expression) { Scanner exprScanner = new Scanner(expression); String numeral1 = exprScanner.next(); String operator = exprScanner.next(); String numeral2 = exprScanner.next(); UnboundedNatural left = new UnboundedNatural(numeral1); UnboundedNatural right = new UnboundedNatural(numeral2); System.out.printf("Computing %s %s %s\n", left, operator, right); UnboundedNatural numberResult = null; Boolean boolResult = null; String arithOperators = "+-*/%"; String relationalOperators = "=<>"; if (arithOperators.contains(operator)) { if (operator.equals("+")) { numberResult = left.plus(right); } else if (operator.equals("-")) { numberResult = left.minus(right); } else if (operator.equals("*")) { numberResult = left.times(right); } else if (operator.equals("/")) { System.out.println("The division operator is not supported."); //numberResult = left.dividedBy(right); } else if (operator.equals("%")) { System.out.println("The remainder operator is not supported."); //numberResult = left.mod(right); } } else if (relationalOperators.contains(operator)) { if (operator.equals("=")) { boolResult = left.equals(right); } else if (operator.equals("<")) { System.out.println("The less-than operator is not supported."); // boolResult = left.isLessThan(right); } else if (operator.equals(">")) { System.out.println("The greater-than operator is not supported."); // boolResult = left.isGreaterThan(right); } } else { System.out.println("Unrecognized operator"); } if (numberResult != null) { System.out.println(numberResult); } else if (boolResult != null) { System.out.println(boolResult); } else { System.out.println("No result was computed."); } } }