Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Remote Method InvocationRSS Feeds

Application for counting factorial of a given number

Posted By: Volker Fischer     Category: Java     Views: 7002

write an RMI application for counting factorial of a given number.

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
  
Share: 


Didn't find what you were looking for? Find more on Application for counting factorial of a given number Or get search suggestion and latest updates.

Volker Fischer
Volker Fischer author of Application for counting factorial of a given number is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!