Integral.java Performs actual numerical integration of the function (evaluatable object).
/** A class to calculate summations and numeric integrals. The
* integral is calculated according to the midpoint rule.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class Integral {
/** Returns the sum of f(x) from x=start to x=stop, where the
* function f is defined by the evaluate method of the
* Evaluatable object.
*/
public static double sum(double start, double stop,
double stepSize,
Evaluatable evalObj) {
double sum = 0.0, current = start;
while (current <= stop) {
sum += evalObj.evaluate(current);
current += stepSize;
}
return(sum);
}
/** Returns an approximation of the integral of f(x) from
* start to stop, using the midpoint rule. The function f is
* defined by the evaluate method of the Evaluatable object.
*/
public static double integrate(double start, double stop,
int numSteps,
Evaluatable evalObj) {
double stepSize = (stop - start) / (double)numSteps;
start = start + stepSize / 2.0;
return(stepSize * sum(start, stop, stepSize, evalObj));
}
}
Evaluatable.java An interface for evaluating functions. Requires the Evaluatable object to implement the evaluate method.
/** An interface for evaluating functions y = f(x) at a specific
* value. Both x and y are double-precision floating-point
* numbers.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public interface Evaluatable {
public double evaluate(double value);
}
RemoteIntegral.java Establishes the methods in the remote object available to the client.
import java.rmi.*;
/** Interface for remote numeric integration object.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public interface RemoteIntegral extends Remote {
public double sum(double start, double stop, double stepSize,
Evaluatable evalObj)
throws RemoteException;
public double integrate(double start, double stop,
int numSteps, Evaluatable evalObj)
throws RemoteException;
}
RemoteIntegralClient.java The client that sends an Evaluatable object to the remote server for integration.
import java.rmi.*;
import java.net.*;
import java.io.*;
/** This class calculates a variety of numerical integration
* values, printing the results of successively more accurate
* approximations. The actual computation is performed on a
* remote machine whose hostname is specified as a command-
* line argument.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class RemoteIntegralClient {
public static void main(String[] args) {
try {
String host = (args.length > 0) ? args[0] : "localhost";
RemoteIntegral remoteIntegral =
(RemoteIntegral)Naming.lookup("rmi://" + host +
"/RemoteIntegral");
for(int steps=10; steps<=10000; steps*=10) {
System.out.println
("Approximated with " + steps + " steps:" +
"\n Integral from 0 to pi of sin(x)=" +
remoteIntegral.integrate(0.0, Math.PI,
steps, new Sin()) +
"\n Integral from pi/2 to pi of cos(x)=" +
remoteIntegral.integrate(Math.PI/2.0, Math.PI,
steps, new Cos()) +
"\n Integral from 0 to 5 of x^2=" +
remoteIntegral.integrate(0.0, 5.0, steps,
new Quadratic()));
}
System.out.println
("`Correct' answer using Math library:" +
"\n Integral from 0 to pi of sin(x)=" +
(-Math.cos(Math.PI) - -Math.cos(0.0)) +
"\n Integral from pi/2 to pi of cos(x)=" +
(Math.sin(Math.PI) - Math.sin(Math.PI/2.0)) +
"\n Integral from 0 to 5 of x^2=" +
(Math.pow(5.0, 3.0) / 3.0));
} catch(RemoteException re) {
System.out.println("RemoteException: " + re);
} catch(NotBoundException nbe) {
System.out.println("NotBoundException: " + nbe);
} catch(MalformedURLException mfe) {
System.out.println("MalformedURLException: " + mfe);
}
}
}
Sin.java Evaluatable version of sin(x).
import java.io.Serializable;
/** An evaluatable version of sin(x).
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
class Sin implements Evaluatable, Serializable {
public double evaluate(double val) {
return(Math.sin(val));
}
public String toString() {
return("Sin");
}
}
Cos.java Evaluatable version of cos(x).
import java.io.Serializable;
/** An evaluatable version of cos(x).
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
class Cos implements Evaluatable, Serializable {
public double evaluate(double val) {
return(Math.cos(val));
}
public String toString() {
return("Cosine");
}
}
Quadratic.java Evaluatable version of x2.
import java.io.Serializable;
/** An evaluatable version of x^2.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
class Quadratic implements Evaluatable, Serializable {
public double evaluate(double val) {
return(val * val);
}
public String toString() {
return("Quadratic");
}
}
RemoteIntegralImpl.java The remote object which provides a concrete implementation of the RemoteIntegral interface.
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
/** The actual implementation of the RemoteIntegral interface.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class RemoteIntegralImpl extends UnicastRemoteObject
implements RemoteIntegral {
/** Constructor must throw RemoteException. */
public RemoteIntegralImpl() throws RemoteException {}
/** Returns the sum of f(x) from x=start to x=stop, where the
* function f is defined by the evaluate method of the
* Evaluatable object.
*/
public double sum(double start, double stop, double stepSize,
Evaluatable evalObj) {
return(Integral.sum(start, stop, stepSize, evalObj));
}
/** Returns an approximation of the integral of f(x) from
* start to stop, using the midpoint rule. The function f is
* defined by the evaluate method of the Evaluatable object.
* @see #sum
*/
public double integrate(double start, double stop, int numSteps,
Evaluatable evalObj) {
return(Integral.integrate(start, stop, numSteps, evalObj));
}
}
RemoteIntegralServer.java Creates an instance of RemoteIntegralImpl on the remote server and binds the object in the registry for lookup by the client.
import java.rmi.*;
import java.net.*;
/** Creates a RemoteIntegralImpl object and registers it under
* the name 'RemoteIntegral' so that remote clients can connect
* to it for numeric integration results. The idea is to place
* this server on a workstation with very fast floating-point
* capabilities, while slower interfaces can run on smaller
* computers but still use the integration routines.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class RemoteIntegralServer {
public static void main(String[] args) {
try {
RemoteIntegralImpl integral = new RemoteIntegralImpl();
Naming.rebind("rmi:///RemoteIntegral", integral);
} catch(RemoteException re) {
System.out.println("RemoteException: " + re);
} catch(MalformedURLException mfe) {
System.out.println("MalformedURLException: " + mfe);
}
}
}
RemoteIntegralClient2.java An enterprise version of the client that imposes a security manager to support the Java 2 RMI 1.2 stub protocol.
import java.rmi.*;
import java.net.*;
import java.io.*;
/** This class is a Java 2 version of RemoteIntegralClient
* that imposes a SecurityManager to allow the client to
* connect to a remote machine for loading stub files and
* performing numerical integration through a remote
* object.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class RemoteIntegralClient2 {
public static void main(String[] args) {
try {
System.setSecurityManager(new RMISecurityManager());
String host =
(args.length > 0) ? args[0] : "localhost";
RemoteIntegral remoteIntegral =
(RemoteIntegral)Naming.lookup("rmi://" + host +
"/RemoteIntegral");
for(int steps=10; steps<=10000; steps*=10) {
System.out.println
("Approximated with " + steps + " steps:" +
"\n Integral from 0 to pi of sin(x)=" +
remoteIntegral.integrate(0.0, Math.PI,
steps, new Sin()) +
"\n Integral from pi/2 to pi of cos(x)=" +
remoteIntegral.integrate(Math.PI/2.0, Math.PI,
steps, new Cos()) +
"\n Integral from 0 to 5 of x^2=" +
remoteIntegral.integrate(0.0, 5.0, steps,
new Quadratic()));
}
System.out.println
("`Correct' answer using Math library:" +
"\n Integral from 0 to pi of sin(x)=" +
(-Math.cos(Math.PI) - -Math.cos(0.0)) +
"\n Integral from pi/2 to pi of cos(x)=" +
(Math.sin(Math.PI) - Math.sin(Math.PI/2.0)) +
"\n Integral from 0 to 5 of x^2=" +
(Math.pow(5.0, 3.0) / 3.0));
} catch(RemoteException re) {
System.out.println("RemoteException: " + re);
} catch(NotBoundException nbe) {
System.out.println("NotBoundException: " + nbe);
} catch(MalformedURLException mfe) {
System.out.println("MalformedURLException: " + mfe);
}
}
}
rmiclient.policy Policy file for the client. Grants permissions for the client to connect to the RMI server and Web server.
// Taken from Core Web Programming from
// Prentice Hall and Sun Microsystems Press,
// .
// © 2001 Marty Hall and Larry Brown;
// may be freely used or adapted.
grant {
// rmihost - RMI registry and the server
// webhost - HTTP server for stub classes
permission java.net.SocketPermission
"rmihost:1024-65535", "connect";
permission java.net.SocketPermission
"webhost:80", "connect";
};
Similar Posts