rmi integral
hi, i have trpouble with difficult program about integral.Thx
package rmiexample;
publicinterface Evaluatable{
publicdouble evaluate (double valuate);
}
package rmiexample;
publicclass Integral{
publicstaticdouble 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);
}
publicstaticdouble integrate(double start,double stop,int numSteps,Evaluatable evalObj){
double step = (stop - start) / (double)numSteps;
start = start + stopSize / 2.0;
return(stepSize * sum(start, stop, stepSize, evalObj));
}
}
package rmiexample;
import java.rmi.*;
publicinterface RemoteIntegralextends Remote{
publicdouble sum(double start,double stop,double stepSize, Evaluatable evalObj)
throws RemoteException;
publicdouble integrate(double start,double stop,int numSteps, Evaluatable evalObj)
throws RemoteException;
}
//sdileni clientem a serverem
package rmiexample;
//
publicclass RemoteIntegralclient{
publicstaticvoid 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()));
}
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)));
}catch(RemoteException re){
System.out.println("RemoteException: " + re);
}catch(NotBoundException nbe){
System.out.println("NotBoundException: " + nbe);
}catch("MalformedURLException: " + mfe){
System.out.println("MalformedURLException: "+ mfe);
}
}
}
package rmiexample;
publicclass RemoteIntegralClient2{
publicstaticvoid 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=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()));
}
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)));
}catch(RemoteException re){
System.out.println("RemoteException> " + re);
}catch(NotBoundException nbe){
System.out.println("NotBoundException: " + nbe);
}catch(MalformedURLException:" + mfe){
System("MalformedURLException: + mfe);
}
}
}
package rmiexample;
//remote interal implementation
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
publicclass RemoteIntegralImpextends UnicastRemoteObjectimplements RemoteIntegral{
public RemoteIntegralImp()throws RemoteException{}
publicdouble sum(double start,double stop,double stepSize,Evaluatable evaObj){
return(Integral.sum(start, stop, stepSize, evalObj));
}
publicdouble integrate(double start,double stop,int numSteps,Evaluatable){
return(Integral.integrate(start, stop, numSteps, evalObj));
}
}
package rmiexample;
//remote integral server
import java.rmi.*;
import java.net.*;
publicclass RemoteIntegralServer{
publicstaticvoid 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);
}
}
}
package rmiexample;
//remote integral client
import java.io.Serializable;
class Sinimplements Evaluatable, Seriazable{
publicdouble evaluate val){
retrn(Math.sin(val));
}
public String toString(){
return("Sin")
}
}

