import java.io.*; import java.util.*; /** * Class: TableExample3. *
Description: This example illustrates how to use * the buildFromInfix and evaluate methods in the ExprEvaluator class to * process parametric functions. This example uses setT to set the * value of t. *

* The output is printed in a table which could be used to create * graph. *

* Sample input: *
Enter the first value for t: 1 *
Enter the last value for t: 4 *
Enter the expression for x: t^2 *
Enter the expression for y: t^3 *

* If more than 2 functions are processed, it many be convenient to * create arrays for y, expr, and tree and use a loop to process them. *

Readers may notice that this class is a very simple variation of * TableExample2. * *

* Requires: *
ExprEvaluator.java *
EasyFormat.java *
DoubleScanner.java * @version Date: 6/18/2008 * @author James Brink, brinkje@plu.edu */ public class TableExample3 { double t, x, y; String exprX, exprY; ExprTree treeX, treeY; ExprEvaluator evaluator = new ExprEvaluator(); Scanner scan = new Scanner(System.in); /** * Constructor for TableExample3. Calulates x = f(t) and y = g(t) for * functions f(x) and g(x) input by the user. Prints results in a table. */ public TableExample3() { double firstT, lastT; int exprXLength, exprYLength; // get input System.out.print("Enter the first value for t: "); firstT = scan.nextDouble(); System.out.print("Enter the last value for t: "); lastT = scan.nextDouble(); scan.nextLine(); // read and ignore rest of line System.out.print("Enter the expression for x: "); exprX = scan.nextLine(); System.out.print("Enter the expression for y: "); exprY = scan.nextLine(); try { // determine formatting and print column headings exprXLength = Math.max((" x = " + exprX).length() + 1, 12); exprYLength = Math.max((" y = " + exprY).length() + 1, 12); System.out.println( "\n" + EasyFormat.format("t", 10) + EasyFormat.format("x = " + exprX, exprXLength) + EasyFormat.format("y = " + exprY, exprYLength)); // form the expression trees for each expression treeX = evaluator.buildFromInfix(exprX); treeY = evaluator.buildFromInfix(exprY); // evaluate expression at several points and print the results t = firstT; while (t <=lastT + .05) { evaluator.setT(t); x = evaluator.evaluate(treeX); y = evaluator.evaluate(treeY); System.out.println( EasyFormat.format(t, 10, 1) + EasyFormat.format(x, exprXLength, 3) + EasyFormat.format(y, exprYLength, 3)); t = t + .2; } } catch (Exception e) { System.out.println ("Error: namely: " + e); } } // constructor TableExample3 /** * Trivial main program that starts the application. */ public static void main (String args[]) { TableExample3 app = new TableExample3(); System.exit(0); } // end main } // class TableExample3