My favorites
▼
|
Sign in
fnpad
a text editor based, functional programming style, graphing calculator
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
org
/
dolben
/
fn
/
Function.java
‹r12
r17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* fnPad: a functional programming style calculator
* Copyright © 2001-2010 Hank Dolben, org.dolben.fn.Pad.LICENSE
*/
package org.dolben.fn;
import java.util.List;
/**
* a derivation interface for fnPad functions written in Java
* </p><p>
* A derived class
* <ul>
* <il>has a constructor that specifies the number of paramaters</il>
* <il>implements evaluate() with no parameters</il>
* <il>uses getDouble() or getNumber() to get its arguments</il>
* </ul>
*/
public abstract class Function extends Symbol {
private int _params; // the number of parameters
private List<Object> _args; // the arguments during a "call"
/**
* sets the Symbol name to the (derived) class name
* and saves the number of parameters
*
* @param params the number of parameters to the derived class "function"
*/
public Function( int params ) {
setName(this.getClass().getName());
_params = params;
}
/**
* implements Symbol's evaluate,
* checks that there are the correct number of arguments,
* saves the list, and calls the derived class's evaluate()
*
* @param arguments a list of arguments
*
* @return the result of the derived class's evaluation
*
* @throws Exception when there is an incorrect number of arguments
*/
public Object evaluate( List<Evaluable> arguments ) throws Exception {
if ( arguments.size() != _params ) {
throw new Exception(
"number of arguments of '"+this+"' is not equal to "+_params
);
}
_args = evaluateArguments(arguments);
return evaluate();
}
/**
* evaluates the derived class's function
*
* @return the result of the evaluation
*
* @throws Exception
*/
protected abstract Object evaluate( ) throws Exception;
/**
* gets the double value of an argument
*
* @param index which argument (starts at zero)
*
* @throws Exception when the argument is not a Number
*/
protected double getDouble( int index ) throws Exception {
return getNumber(index).doubleValue();
}
/**
* gets an argument which is a Number
*
* @param index which argument (starts at zero)
*
* @return the argument Number
*
* @throws Exception when the argument is not a Number
*/
protected Number getNumber( int index ) throws Exception {
Object object = _args.get(_params-index-1);
if ( !(object instanceof Number) ) {
throw new Exception(
"'"+object+"' is not a number, argument of '"+this+"'"
);
}
return (Number)object;
}
/**
* gets an argument which is a Long
*
* @param index which argument (starts at zero)
*
* @return the argument Long
*
* @throws Exception when the argument is not a Long
*/
protected long getLong( int index ) throws Exception {
Object object = _args.get(_params-index-1);
if ( !(object instanceof Long) ) {
throw new Exception(
"'"+object+"' is not an integer, argument of '"+this+"'"
);
}
return ((Long)object).longValue();
}
}
Show details
Hide details
Change log
r17
by hdolben on Mar 14, 2010
Diff
use copyright symbol
Go to:
/trunk/fnx/abs.java
/trunk/fnx/acos.java
/trunk/fnx/asin.java
/trunk/fnx/atan.java
/trunk/fnx/atan2.java
/trunk/fnx/cos.java
/trunk/fnx/eng.java
/trunk/fnx/fix.java
/trunk/fnx/hex.java
/trunk/fnx/ln.java
/trunk/fnx/oct.java
/trunk/fnx/rnd.java
/trunk/fnx/sin.java
/trunk/fnx/sqrt.java
/trunk/fnx/tan.java
/trunk/org/dolben/dogs/Scale.java
/trunk/org/dolben/fn/Addition.java
/trunk/org/dolben/fn/And.java
...k/org/dolben/fn/Application.java
/trunk/org/dolben/fn/Array.java
...g/dolben/fn/BinaryOperation.java
/trunk/org/dolben/fn/BitAnd.java
/trunk/org/dolben/fn/BitOr.java
/trunk/org/dolben/fn/BitXor.java
...lben/fn/CalculationsExample.java
...nk/org/dolben/fn/Complement.java
...k/org/dolben/fn/Conditional.java
...lben/fn/ConditionalsExample.java
/trunk/org/dolben/fn/Constant.java
...olben/fn/DefinitionsExample.java
/trunk/org/dolben/fn/Division.java
/trunk/org/dolben/fn/EqualTo.java
/trunk/org/dolben/fn/Evaluable.java
...rg/dolben/fn/Exponentiation.java
.../org/dolben/fn/FormatDouble.java
/trunk/org/dolben/fn/Function.java
/trunk/org/dolben/fn/Graph.java
...nk/org/dolben/fn/GraphPanel.java
...g/dolben/fn/GraphingExample.java
...k/org/dolben/fn/GreaterThan.java
...ben/fn/GreaterThanOrEqualTo.java
/trunk/org/dolben/fn/HexLong.java
/trunk/org/dolben/fn/Index.java
/trunk/org/dolben/fn/LeftShift.java
/trunk/org/dolben/fn/LessThan.java
...dolben/fn/LessThanOrEqualTo.java
/trunk/org/dolben/fn/Modulus.java
...rg/dolben/fn/Multiplication.java
/trunk/org/dolben/fn/Negation.java
/trunk/org/dolben/fn/Not.java
Project members,
sign in
to write a code review
Older revisions
r12
by hdolben on Jan 9, 2010
Diff
merged branches/hank-01
r8
by hdolben on Jan 9, 2010
Diff
merged branches/hank-01
r2
by hdolben on Jan 9, 2010
Diff
initial import
All revisions of this file
File info
Size: 3362 bytes, 114 lines
View raw file
Powered by
Google Project Hosting