Code for Application for counting factorial of a given number in Java
----------------------------------------------------------------------------------
Interface
----------------------------------------------------------------------------------publicinterface factInterface extends java.rmi.Remote
{
int find_fact(int n) throws java.rmi.RemoteException;
}
-------------------------------------------------------------------------------------------------
Implementation
-------------------------------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
publicclass factImpl extends UnicastRemoteObject implements factInterface
{
public factImpl(String name) throws RemoteException
{
super();
try
{
Naming.rebind(name,this);
}
catch(Exception e) { }
}
publicint find_fact(int n1) throws RemoteException
{
int ans=n1;
n1--;
while(n1 > 0)
{
ans = ans * n1;
n1--;
}
return ans;
}
}
-------------------------------------------------------------------------------------------------
Client
-------------------------------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.io.*;
publicclass factClient
{
publicstaticvoid main(String args[])
{
System.setSecurityManager(new RMISecurityManager());
try
{
int factorial;
int intNumber=0;
factInterface pi = (factInterface) Naming.lookup("//localhost/pno");
BufferedReader ber=new BufferedReader(new InputStreamReader(System.in));
String temp=new String();
System.out.print("\n Enter Number for Factorial : ");
temp=ber.readLine();
intNumber = Integer.parseInt(temp);
factorial = pi.find_fact(intNumber);
System.out.println("Factorial is : " + factorial);
}
catch(Exception e)
{
System.out.println("System Exception" + e);
}
}
}
-------------------------------------------------------------------------------------------------
Server
-------------------------------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
publicclass factServer
{
publicstaticvoid main(String args[])
{
System.setSecurityManager(new RMISecurityManager());
try
{
factImpl pim = new factImpl("//localhost/pno");
System.out.println("\nServer is ready...");
}
catch(Exception e) { }
}
}
-------------------------------------------------------------------------------------------------
Policy
-------------------------------------------------------------------------------------------------
grant
{
// Allow everything for now
permission java.security.AllPermission;
};
-------------------------------------------------------------------------------------------------
Output
-------------------------------------------------------------------------------------------------
Enter Number for Factorial : 5
Factorial is : 120