Calculator


Calculator.java
 public interface Calculator extends java.rmi.Remote
{

public long add(long a,long b) throws java.rmi.RemoteException;
public long sub(long a,long b) throws java.rmi.RemoteException;
public long mul(long a,long b) throws java.rmi.RemoteException;
public long div(long a,long b) throws java.rmi.RemoteException;

}
----------------------------------------------------------------------------------------------------
CalulatorClient.java


import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient
{
 public static void main(String args[])
 {
  long num1=Integer.parseInt(args[0]);
  long num2=Integer.parseInt(args[1]);
  try
    {
     Calculator c=(Calculator)
     Naming.lookup("rmi://localhost/CalculatorService");
     System.out.println("The Addition of "+num1+"and"+num2+"is:"+c.add(num1,num2));
     System.out.println("The Subtraction of "+num1+"and"+num2+"is:"+c.sub(num1,num2));
     System.out.println("The Multiplication of "+num1+"and"+num2+"is:"+c.mul(num1,num2));
     System.out.println("The Division of "+num1+"and"+num2+"is:"+c.div(num1,num2));
    }
   catch(MalformedURLException m)
  {
   System.out.println("MaluformedURLException");
  }
  catch(RemoteException re)
  {
   System.out.println("RemoteException");
  }
  catch(NotBoundException nbe)
  {
   System.out.println("NotBoundExceptionException");
  }
  catch(java.lang.ArithmeticException ae)
  {
   System.out.println("java.lang.ArithmeticException");
   }

  }
}
---------------------------------------------------------------------------------------------------------
CalculatorImpl.java


public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator
{

 public CalculatorImpl() throws java.rmi.RemoteException
 {
  super();
 }
 public long add(long a,long b) throws java.rmi.RemoteException
 {
  return a+b;
 }
 public long sub(long a,long b) throws java.rmi.RemoteException
 {
  return a-b;
 }
 public long mul(long a,long b) throws java.rmi.RemoteException
 {
  return a*b;
 }
 public long div(long a,long b) throws java.rmi.RemoteException
 {
  return a/b;
 }


}



---------------------------------------------------------------------------------------------------
CalculatorServer.java

import java.rmi.Naming;
public class CalculatorServer
{
 public CalculatorServer()
 {
  try
   {
    Calculator c= new CalculatorImpl();
    Naming.rebind("rmi://localhost:1099/CalculatorService",c);  

   }
   catch(Exception e)
   {
   System.out.println("Trouble:"+e);
   }
 }
 public static void main(String args[])
 {
  new CalculatorServer();
 }
}





Leave a Reply