01: /*************************************************************************
02: * *
03: * 1) This source code file, in unmodified form, and compiled classes *
04: * derived from it can be used and distributed without restriction, *
05: * including for commercial use. (Attribution is not required *
06: * but is appreciated.) *
07: * *
08: * 2) Modified versions of this file can be made and distributed *
09: * provided: the modified versions are put into a Java package *
10: * different from the original package, edu.hws; modified *
11: * versions are distributed under the same terms as the original; *
12: * and the modifications are documented in comments. (Modification *
13: * here does not include simply making subclasses that belong to *
14: * a package other than edu.hws, which can be done without any *
15: * restriction.) *
16: * *
17: * David J. Eck *
18: * Department of Mathematics and Computer Science *
19: * Hobart and William Smith Colleges *
20: * Geneva, New York 14456, USA *
21: * Email: eck@hws.edu WWW: http://math.hws.edu/eck/ *
22: * *
23: *************************************************************************/package edu.hws.jcm.data;
24:
25: /**
26: * A ValueMath object is an easy way to create Value objects that are computed
27: * from other Value objects. For example, "new ValueMath(a,b,'+')" is an
28: * object whose value is obtained by adding the values of a and b.
29: */
30: public class ValueMath implements Value {
31:
32: private Function f; // If non-null, this is a value of the form f(params);
33: // If null, it's of the form x + y, x - y, ...
34: private double[] param;
35: private Value x, y;
36: private char op;
37:
38: /**
39: * Create a ValueMath object whose value is computed by applying an arithmetic
40: * operator the values of x and y.
41: * @param op The arithmetic operator that is to be applied to x and y. This should
42: * be one of the characters '+', '-', '*', '/', or '^'. (No error is
43: * thrown if another character is provided. It will be treated as a '/').
44: */
45: public ValueMath(Value x, Value y, char op) {
46: this .x = x;
47: this .y = y;
48: this .op = op;
49: }
50:
51: /**
52: * Create a ValueMath object whose value is computed as f(x).
53: */
54: public ValueMath(Function f, Value x) {
55: if (f.getArity() != 1)
56: throw new IllegalArgumentException(
57: "Internal Error: The function in a ValueMath object must have arity 1.");
58: this .f = f;
59: this .x = x;
60: param = new double[1];
61: }
62:
63: /**
64: * Get the value of this object.
65: */
66: public double getVal() {
67: if (f != null) {
68: param[0] = x.getVal();
69: return f.getVal(param);
70: } else {
71: double a = x.getVal();
72: double b = y.getVal();
73: switch (op) {
74: case '+':
75: return a + b;
76: case '-':
77: return a - b;
78: case '*':
79: return a * b;
80: case '/':
81: return a / b;
82: case '^':
83: return Math.pow(a, b);
84: default:
85: throw new IllegalArgumentException(
86: "Internal Error: Unknown math operator.");
87: }
88: }
89: }
90:
91: } // end class ValueMath
|