// **** Imports **** import java.io.*; import java.util.*; /** * Class: TrivialExample1. *
Description: This trivial example illustrates how to use * the buildFromInfix and evaluate methods in the ExprEvaluator * class. *

* To evaluate prefix or postfix expressions, simply replace "buildFromInfix" by * "buildFromPrefix" or "buildFromPostfix". *

* Requires: *
ExprEvaluator.java *
EasyFormat.java *
DoubleScanner.java * @version Date: 6/10/2008 * @author James Brink, brinkje@plu.edu */ public class TrivialExample1 { double xVal; double yVal; double infixVal; String infix; ExprEvaluator evaluator = new ExprEvaluator(); Scanner scan = new Scanner(System.in); /** * Constructor for TrivialExample1. * * Note: in this case, the expression only uses x and y. Hence the t * parameter is just set to 0 when evaluating the expression. */ public TrivialExample1() { // get input System.out.print("Enter the first value for x: "); xVal = scan.nextDouble(); System.out.print("Enter the first value for y: "); yVal = scan.nextDouble(); scan.nextLine(); // read rest of line System.out.print("Enter the expression to be evaluated: "); infix = scan.nextLine(); // evaluate expression at several points and print the results try { evaluator.buildFromInfix(infix); System.out.println("The result are"); for (int i = 0; i < 10; i++) { infixVal = evaluator.evaluate(0, xVal, yVal); System.out.println("x =" + xVal + ", y = " + yVal + ", expression = " + infixVal); xVal++; yVal++; } }catch (Exception e) { System.out.println ("Error: namely: " + e); } } // constructor TrivialExample1 /** * Trivial main program that starts the application. */ public static void main (String args[]) { TrivialExample1 app = new TrivialExample1(); System.exit(0); } // end main } // class TrivialExample1