import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* FractionCalculator.java ** ** Author: R. McCloskey ** Date: November 2018 ** ** Java application that provides a simple fractions calculator. ** That is, in response to the user entering an expression such as ** ** 3/8 + 15/35 ** ** it will evaluate the expression and display the result. This is ** repeated until the user enters "quit". ** ** Only expressions of this form (i.e., two fractions separated by an ** operator) are handled. Supported operators are +, -, *, /, and =, ** the last being the relational "equals" operator. In the case of ** the = operator, the displayed result is 0/1 if the expression ** evaluates to true and 1/1 if the expression evaluates to false. ** Spaces may not appear within a fraction, but the operator must be ** preceded and followed by at least one space. ** ** To make the program read from a file (rather than the keyboard), ** provide a command-line argument (or what jGrasp calls a "run argument") ** that is the name of that file. */ public class FractionCalculator { private static final char ADD_OP = '+'; private static final char SUBTRACT_OP = '-'; private static final char MULTIPLY_OP = '*'; private static final char DIVIDE_OP = '/'; private static final char EQUALS_OP = '='; private static final Fraction ZERO = new Fraction(0,1); private static final Fraction ONE = new Fraction(1,1); private static Scanner input; private static boolean echo; public static void main(String[] args) { establishScanner(args); System.out.println("Welcome to the Fractions Calculator!"); boolean keepGoing = true; while (keepGoing) { System.out.print("\n> "); String line = input.nextLine().trim(); if (echo) { System.out.println(line); } if (line.length() == 0) { // ignore a blank line } else if (line.toLowerCase().equals("quit")) { keepGoing = false; } else { try { System.out.println(evaluate(line)); } catch (Exception e) { e.printStackTrace(System.out); } } } System.out.println("Goodbye."); } /* Evaluates the given expression and returns the result, in the ** form of a String. ** An IllegalArgumentException is thrown if it is determined that ** the given expression is malformed. */ public static String evaluate(String expr) { Scanner exprScanner = new Scanner(expr); String frac1Str = exprScanner.next(); String operatorStr = exprScanner.next(); if (operatorStr.length() != 1) { throw new IllegalArgumentException("Operator is invalid"); } char operator = operatorStr.charAt(0); String frac2Str = exprScanner.next(); Fraction frac1 = new Fraction(frac1Str); Fraction frac2 = new Fraction(frac2Str); String result; if (operator == EQUALS_OP) { if (frac1.equals(frac2)) { result = "true"; } else { result = "false"; } } else { // operator is something other than = if (operator == ADD_OP) { frac1.addTo(frac2); result = frac1.toString(); } else if (operator == SUBTRACT_OP) { frac1.subtractFrom(frac2); result = frac1.toString(); } else if (operator == MULTIPLY_OP) { frac1.multiplyBy(frac2); result = frac1.toString(); } else if (operator == DIVIDE_OP) { frac1.divideBy(frac2); result = frac1.toString(); } else { throw new IllegalArgumentException("Invalid operator."); } } return result; } /* Establishes the source of input, which is the file whose ** name is args[0] unless args has length zero, in which case ** it is the keyboard. */ private static void establishScanner(String[] args) { if (args.length == 0) { input = new Scanner(System.in); echo = false; } else { String fileName = args[0]; try { input = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.printf("No file named \"%s\"; program aborting.\n", fileName); System.exit(0); } echo = true; } } }