import java.text.DecimalFormat; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; /* Java application program for the purpose of testing the Polynomial class. ** If there is no command line argument provided, standard input is taken ** to be the input source. Otherwise, the first command line argument is ** taken to be the name of the file from which input is to be read. ** ** Author: R. McCloskey (last modified September 2025) */ public class PolynomialTester { private static Scanner input; // source of input private static boolean echo; // true if a file (and not keyboard) // for formatting real numbers when they are displayed private static DecimalFormat df = new DecimalFormat("#####0.0########"); public static void main(String[] args) { establishInputSource(args); System.out.println("Welcome to the Polynomial Tester Program!"); boolean keepGoing = true; while (keepGoing) { System.out.println(); System.out.println("1: Test Evaluate"); System.out.println("2: Test Root Find"); System.out.println("3: Test Arithmetic"); System.out.println("4: Quit"); System.out.println(); System.out.print("Enter choice: "); String responseStr = input.nextLine().trim(); int response; try { response = Integer.parseInt(responseStr); } catch (Exception e) { response = -1; } if (response == 1) { try { testValueAt(); } catch (Exception e) { e.printStackTrace(System.out); } } else if (response == 2) { try { testRootFind(); } catch (Exception e) { e.printStackTrace(System.out); } } else if (response == 3) { try { testArithmetic(); } catch (Exception e) { e.printStackTrace(System.out); } } else if (response == 4) { keepGoing = false; } else { System.out.println("Invalid response, moron!"); } } System.out.println("Goodbye"); } /* Tests the methods that do polynomial arithmetic: sum(), difference(), ** product(). */ private static void testArithmetic() { // Create polynomial p. Polynomial p = makePoly("p"); // Display p (which tests the toString() method). System.out.println("p, of degree " + p.degreeOf() + ", is " + p); System.out.println(); // Now create polynomial q. Polynomial q = makePoly("q"); // Display q (which tests the toString() method). System.out.println("q, of degree " + q.degreeOf() + ", is " + q); System.out.println(); // Compute and display the result of modifying p by adding to, // subtracting from, and multiplying by q. Use cloning so as to // be able to retrieve the initial values of p and q. (Of course, // the state of q should not change.) testOperation(p, q, '+'); testOperation(p, q, '-'); testOperation(p, q, '*'); System.out.println(); } /* Applies the operation specified by 'opCode' to p and q and ** reports the result. */ private static void testOperation(Polynomial p, Polynomial q, char opCode) { // Make clones of p and q (to serve as surrogates). Polynomial pClone = p.clone(); Polynomial qClone = q.clone(); // Apply the method indicated by opCode to pClone and qClone if (opCode == '+') { pClone.addTo(qClone); } else if (opCode == '-') { pClone.subtractFrom(qClone); } else if (opCode == '*') { pClone.multiplyBy(qClone); } else { System.out.println("ERROR: " + opCode + " is not a recognized operation"); } // Report the updated value of pClone. System.out.println("p" + opCode + "q is " + pClone); // In case qClone was modified, give a warning. if (!q.equals(qClone)) { System.out.println("ERROR: polynomial q was modified"); } } /* Tests the valueAt() method. */ private static void testValueAt() { Polynomial p = makePoly("p"); boolean keepGoing = true; while (keepGoing) { System.out.println(); System.out.println("Polynomial p = " + p); System.out.print("Enter value at which to evaluate p " + "(empty string to exit): "); String line = input.nextLine().trim(); if (line.length() == 0) { keepGoing = false; } else { double x = Double.parseDouble(line); double pAtX = p.valueAt(x); System.out.println("p(" + df.format(x) + ") = " + df.format(pAtX)); } } } /* Tests the rootOf() method. */ private static void testRootFind() { Polynomial p = makePoly("p"); boolean keepGoing = true; while (keepGoing) { System.out.println(); System.out.println("Polynomial p = " + p); System.out.print("Enter endpoints of interval " + "in which to search for a root " + "(empty string to exit): "); String response = input.nextLine().trim(); if (response.length() == 0) { keepGoing = false; } else { Scanner s = new Scanner(response); double xLow = s.nextDouble(); double xHigh = s.nextDouble(); double eps = 0.000005; // for epsilon try { // Search for root and display result. double z = p.rootOf(xLow, xHigh, eps); System.out.print("Root found at z = " + df.format(z)); double pAtZ = p.valueAt(z); System.out.println("; p(z) = " + df.format(pAtZ)); if (Math.abs(pAtZ) > 0.0005) { System.out.println("WARNING: p(z) seems too far " + "from zero for z to be a root"); } } catch (Exception e) { e.printStackTrace(System.out); } } } } /* If the two given polynomials (s and t) are not equal, displays an * error message indicating that the polynomials with the given names * (sName and tName) are not equal. Otherwise does nothing. */ /* private static void checkEquals(Polynomial s, Polynomial t, String sName, String tName) { if (!s.equals(t)) { System.out.println("ERROR: polynomial " + sName + " is not equal to " + tName); } } */ /* Makes a polynomial corresponding to the coefficients read from the ** input source. */ private static Polynomial makePoly(String name) { System.out.print("Enter the coefficients for polynomial " + name + ": "); return new Polynomial(getCoeffs()); } /* Reads a line of text that is expected to be a sequence of integers; ** returns an ArrayList containing those integers. */ private static ArrayList getCoeffs() { String line = input.nextLine(); if (echo) { System.out.println(line); } String[] tokens = line.split(" +"); ArrayList result = new ArrayList(tokens.length); for (int i=0; i != tokens.length; i++) { result.add(Integer.parseInt(tokens[i])); } return result; } /* Sets 'input' to a newly created Scanner object that reads input ** from either standard input (in case args.length is zero) or from the ** file whose name is args[0] (in case args.length is not zero). In the ** former case, sets 'echo' to false; in latter case, sets it to true. */ private static void establishInputSource(String[] args) { if (args.length == 0) { input = new Scanner(System.in); echo = false; } else { try { File inputFile = new File(args[0]); input = new Scanner(inputFile); echo = true; } catch (FileNotFoundException e) { e.printStackTrace(); } } } }