import java.io.*;
import java.util.*;
/**
* Class: TableExample4.
*
Description: This example illustrates how to use the
* buildFromInfix and evaluate methods in the ExprEvaluator class to
* process polar coordinate functions. The example is rather unusual
* in that it inputs expressions for both the radius and angle theta.
* In cases where there is only one function for the radius which is
* function of theta, then one could use use buildFromInfix and evaluate
* in the style of example 1 (that is, avoiding explicit references to a
* ExprTree).
*
* This example uses setValueOf to set the value of t and convertToRect * to convert r and theta into rectangular coordinates x and y. *
* The output is printed in a table which could be used to create * graph. Parameter t is incremented by PI/20 (about 0.15708) each time. *
* Sample input to produce values for a 3-leaf rose:
*
Enter the first value for t: 0
*
Enter the last value of t: 6.2832
*
Enter the expression for o (theta) as a function of t: t/2
*
Enter the expression for r as a function of t or o (theta): cos(3*o)
*
* If more than 2 functions are processed, it many be convenient to * create arrays for r, theta, and tree and use a loop to process them. * *
* Requires:
*
ExprEvaluator.java
*
EasyFormat.java
*
DoubleScanner.java
* @version Date: 6/18/2008
* @author James Brink, brinkje@plu.edu
*/
public class TableExample4 {
double t, x, y, r, theta;
String exprR, exprTheta;
ExprTree treeR, treeTheta;
ExprEvaluator evaluator = new ExprEvaluator();
Scanner scan = new Scanner(System.in);
/**
* Constructor for TableExample4. Calulates theta and r as functions
* of t and then converts theta and r into rectangular cordinates.
* (Functions for theta and r are input by the user.) Prints results in
* a table.
*/
public TableExample4() {
double firstT, lastT;
int exprRLength, exprThetaLength, exprXYLength;
// 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 rest of line
System.out.print("Enter the expression for o (theta) as a function of t: ");
exprTheta = scan.nextLine();
System.out.print("Enter the expression for r as a function of t or o (theta): ");
exprR = scan.nextLine();
try {
exprThetaLength = Math.max((" theta = " + exprTheta).length(), 12);
exprRLength = Math.max((" r = " + exprR).length(), 12);
exprXYLength = Math.max((" x").length(), 12);
System.out.println("\n"
+ EasyFormat.format("t", 10)
+ EasyFormat.format(" theta = " + exprTheta, exprThetaLength)
+ EasyFormat.format(" r = " + exprR, exprRLength)
+ EasyFormat.format(" x", exprXYLength)
+ EasyFormat.format(" y", exprXYLength));
// form expression trees for theta and r. Setting the value of theta
// (variable o) allows r to be a function of theta .
treeTheta = evaluator.buildFromInfix("o = " + exprTheta);
treeR = evaluator.buildFromInfix(exprR);
// evaluate expression at several points and print the results
t = firstT;
while (t <=lastT + .05) {
evaluator.setValueOf('t', t); // or use evaluator.setT(t);
theta = evaluator.evaluate(treeTheta);
r = evaluator.evaluate(treeR);
evaluator.convertToRect(r, theta);
x = evaluator.getValueOf('x');
y = evaluator.getValueOf('y');
System.out.println(
EasyFormat.format(t, 10, 3)
+ EasyFormat.format(theta, exprThetaLength, 3)
+ EasyFormat.format(r, exprRLength, 3)
+ EasyFormat.format(x, exprXYLength, 3)
+ EasyFormat.format(y, exprXYLength, 3));
t = t + Math.PI/20;
}
// // variable values may have changed so use tree.getValueOf
} catch (Exception e) {
System.out.println ("Error: namely: " + e);
}
} // constructor TableExample4
/**
* Trivial main program that starts the application.
*/
public static void main (String args[]) {
TableExample4 app = new TableExample4();
System.exit(0);
} // end main
} // class TableExample4