import java.text.DecimalFormat; import java.util.Scanner; /* For the purpose of testing the Polynomial class. ** ** Author: R. McCloskey (last modified October 2022) */ public class PolynomialTester { private static Scanner keyboard = new Scanner(System.in); // for formatting real numbers when they are displayed private static DecimalFormat df = new DecimalFormat("#####0.0########"); public static void main(String[] 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 = keyboard.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); // Make clones of p and q. Polynomial pClone = p.clone(); Polynomial qClone = q.clone(); // Compute and display the sum, difference, and product of p and q, and // each time check to see that neither p's nor q's state has changed. System.out.println(); Polynomial sumPQ = p.sum(q); System.out.println("p+q is " + sumPQ); checkEquals(p, pClone, "p", "pClone"); checkEquals(q, qClone, "q", "qClone"); Polynomial diffPQ = p.difference(q); System.out.println("p-q is " + diffPQ); checkEquals(p, pClone, "p", "pClone"); checkEquals(q, qClone, "q", "qClone"); Polynomial prodPQ = p.product(q); System.out.println("p*q is " + prodPQ); checkEquals(p, pClone, "p", "pClone"); checkEquals(q, qClone, "q", "qClone"); System.out.println(); } /* 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 = keyboard.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 = keyboard.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 entered at the * keyboard in response to a prompt. */ private static Polynomial makePoly(String name) { System.out.print("Enter the coefficients for polynomial " + name + ": "); return new Polynomial(getCoeffs()); } /* Reads a line of text entered at the keyboard that is expected to be ** a sequence of integers; returns an array containing those integers. */ private static int[] getCoeffs() { String line = keyboard.nextLine(); String[] tokens = line.split(" +"); int[] result = new int[tokens.length]; for (int i=0; i != result.length; i++) { result[i] = Integer.parseInt(tokens[i]); } return result; } }