Source Code Cross Referenced for Constant.java in  » Science » jcm1-source » edu » hws » jcm » data » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » jcm1 source » edu.hws.jcm.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*************************************************************************
002:         *                                                                        *
003:         *   1) This source code file, in unmodified form, and compiled classes   *
004:         *      derived from it can be used and distributed without restriction,  *
005:         *      including for commercial use.  (Attribution is not required       *
006:         *      but is appreciated.)                                              *
007:         *                                                                        *
008:         *    2) Modified versions of this file can be made and distributed       *
009:         *       provided:  the modified versions are put into a Java package     *
010:         *       different from the original package, edu.hws;  modified          *
011:         *       versions are distributed under the same terms as the original;   *
012:         *       and the modifications are documented in comments.  (Modification *
013:         *       here does not include simply making subclasses that belong to    *
014:         *       a package other than edu.hws, which can be done without any      *
015:         *       restriction.)                                                    *
016:         *                                                                        *
017:         *   David J. Eck                                                         *
018:         *   Department of Mathematics and Computer Science                       *
019:         *   Hobart and William Smith Colleges                                    *
020:         *   Geneva, New York 14456,   USA                                        *
021:         *   Email: eck@hws.edu          WWW: http://math.hws.edu/eck/            *
022:         *                                                                        *
023:         *************************************************************************/package edu.hws.jcm.data;
024:
025:        /**
026:         * A Constant is a Value that represents a constant real number.  (The value doesn't have to
027:         * be constant in sub-classes, since the member that stores the value is protected, not private.)
028:         * A Constant doesn't necessarily need a name.  If the name is null, then the print string for the
029:         * Constant is the value of the constant.  If it has a non-null name, then the print string 
030:         * is the name.  (Note that, as for any MathObject, if the name is null, than the Constant can't
031:         * be added to a Parser.)  Constant objects are used to represent the mathematical constants
032:         * pi and e.
033:         *    A Constant is both an Expression and an ExpressionCommand.  Since it is an ExpressionCommand,
034:         * it can occur as a command in an ExpressionProgram.  In that case, it simply represens a named constant
035:         * occurs in an expression.
036:         */
037:        public class Constant implements  Expression, ExpressionCommand,
038:                MathObject {
039:            // Also implements Value, which is a subinterface of Expression.
040:
041:            private String name; // This Constant's name, possibly null.
042:
043:            /**
044:             * The value of this Constant.   
045:             */
046:            protected double value;
047:
048:            /**
049:             * Create an unnamed Constant with the given value and null name.
050:             */
051:            public Constant(double value) {
052:                this .value = value;
053:            }
054:
055:            /**
056:             * Create a Constant with the given name and value.
057:             * The name can be null.
058:             */
059:            public Constant(String name, double value) {
060:                setName(name);
061:                this .value = value;
062:            }
063:
064:            // -------------------- Methods from the MathObject interface -------------------------
065:
066:            /**
067:             * Return the name of this Constant.  It can be null.
068:             */
069:            public String getName() {
070:                return name;
071:            }
072:
073:            /**   
074:             * Set the name of this Constant.  (Note that this should not be done
075:             * if the Constant has been registered with a Parser.)
076:             */
077:            public void setName(String name) {
078:                this .name = name;
079:            }
080:
081:            // -------------- Method from the Value interface (inherited through Expression) ------
082:
083:            /**
084:             * Return the value of this Constant.
085:             */
086:            public double getVal() {
087:                return value;
088:            }
089:
090:            // ----------------------- Methods from the Expression interface ---------------------
091:
092:            /**
093:             * Return the value of the Constant.  Since a constant is continuous function,
094:             * there is only one "case", so no case information needs to be recorded in cases.
095:             */
096:            public double getValueWithCases(Cases cases) {
097:                return value;
098:            }
099:
100:            /**   
101:             * Return the derivative of this Constant with respect to the variable wrt.  
102:             * The derivative is another Constant with value zero.
103:             */
104:            public Expression derivative(Variable wrt) {
105:                return new Constant(0);
106:            }
107:
108:            /**   
109:             * Return the print string representing this Constant.  The string is the
110:             * name of the constant, if that is non-null.  Otherwise, it is the value
111:             * of the constant.
112:             */
113:            public String toString() {
114:                if (name == null)
115:                    return NumUtils.realToString(value);
116:                else
117:                    return name;
118:            }
119:
120:            // -------------------- Methods from the ExpressionCommand interface -----------------
121:
122:            /**
123:             * Apply the Constant to the stack.  This is done by pushing the value of
124:             * the constant onto the stack.  The evaluation of a constant doesn't have any
125:             * "cases", so there is no need to record any information in cases.
126:             */
127:            public void apply(StackOfDouble stack, Cases cases) {
128:                stack.push(getVal()); // Feb 3, 2001 -- changed this from "stack.push(value)", which caused problems with sub-classes!
129:            }
130:
131:            /**   
132:             * Add a commands to deriv to evaluate the derivative of this Constant with respect to the
133:             * variable.  The derivative is 0, so the only command is the constant 0 (which really
134:             * represents the stack operation "push 0").  The program and the position of the Constant
135:             * in that program are irrelevant.
136:             */
137:            public void compileDerivative(ExpressionProgram prog, int myIndex,
138:                    ExpressionProgram deriv, Variable wrt) {
139:                deriv.addConstant(0);
140:            }
141:
142:            /**   
143:             * Return the number of locations that this Constant uses in the program.
144:             * The value is always 1, since the constant is a complete sub-expression
145:             * in itself.
146:             */
147:            public int extent(ExpressionProgram prog, int myIndex) {
148:                return 1;
149:            }
150:
151:            /**
152:             * Retrun false, since the value of this Constant is independent of the value of x.
153:             */
154:            public boolean dependsOn(Variable x) {
155:                return false;
156:            }
157:
158:            /**
159:             * Append the print string for this Constant to the buffer.  (The values of prog and
160:             * myIndex are irrelevant.)
161:             */
162:            public void appendOutputString(ExpressionProgram prog, int myIndex,
163:                    StringBuffer buffer) {
164:                buffer.append(toString());
165:            }
166:
167:        } // end class Constant
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.