0% found this document useful (0 votes)
46 views

Java 2 Practical 8

The document describes 4 Java RMI programs that calculate: 1) The factorial of a given number 2) The Fibonacci series for a given number 3) The addition of two numbers 4) The subtraction of two numbers Each program defines an interface, implementation class, client, and server. The client looks up and invokes methods on remote objects managed by the server.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Java 2 Practical 8

The document describes 4 Java RMI programs that calculate: 1) The factorial of a given number 2) The Fibonacci series for a given number 3) The addition of two numbers 4) The subtraction of two numbers Each program defines an interface, implementation class, client, and server. The client looks up and invokes methods on remote objects managed by the server.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Practical No.

8
1) Write a Java RMI (Remote Method Invocation) program to
calculate the factorial of a given number.

Program:
1)Interface:
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface FactorialInterface extends Remote {


long factorial(int n) throws RemoteException;
}

2)Class:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class FactorialImpl extends UnicastRemoteObject


implements FactorialInterface {
public FactorialImpl() throws RemoteException {
super();
}

@Override
public long factorial(int n) throws RemoteException {
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
}
3)Client Side :
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class FactorialClient {


public static void main(String[] args) {
try {
Registry registry =
LocateRegistry.getRegistry("localhost",2099);
FactorialInterface stub = (FactorialInterface)
registry.lookup("Factorial");
int num = 5;
long result = stub.factorial(num);
System.out.println("Factorial of " + num + " is: " + result);
} catch (Exception e) {
System.err.println("FactorialClient exception:");
e.printStackTrace();
}
}
}

4)Server Side:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class FactorialServer {


public static void main(String[] args) {
try {
FactorialImpl obj = new FactorialImpl();
Registry registry = LocateRegistry.createRegistry(2099);
registry.rebind("Factorial", obj);
System.out.println("FactorialServer bound");
} catch (Exception e) {
System.err.println("FactorialServer exception:");
e.printStackTrace();
}
}
}

Output:
i)

ii)

2) Write a Java RMI (Remote Method Invocation) program to


calculate the Fibonacci series.

Program:
1)Interface:
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface FibonacciInterface extends Remote {


long getFibonacci(int n) throws RemoteException;
}
2)Class:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class FibonacciImpl extends UnicastRemoteObject


implements FibonacciInterface {
public FibonacciImpl() throws RemoteException {
super();
}

@Override
public long getFibonacci(int n) throws RemoteException {
if (n <= 1)
return n;
return getFibonacci(n - 1) + getFibonacci(n - 2);
}
}
3)Client Side:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class FibonacciClient {


public static void main(String[] args) {
try {
Registry registry =
LocateRegistry.getRegistry("localhost");
FibonacciInterface fibonacci = (FibonacciInterface)
registry.lookup("Fibonacci");

int n = 10; //
long result = fibonacci.getFibonacci(n);
System.out.println("Fibonacci(" + n + ") = " + result);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
4)Server Side:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class FibonacciServer {


public static void main(String[] args) {
try {
FibonacciInterface fibonacci = new FibonacciImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("Fibonacci", fibonacci);
System.out.println("Server started...");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Output:
i)

ii)

3) Write a Java RMI (Remote Method Invocation) program to


calculate the addition of two numbers.
Program:
1)Interface:
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface AdditionInterface extends Remote {


int add(int a, int b) throws RemoteException;
}
2)Class:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class AdditionImpl extends UnicastRemoteObject


implements AdditionInterface {
public AdditionImpl() throws RemoteException {
super();
}

@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
}
3)Client Side:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class AdditionClient {


public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost");
AdditionInterface addition = (AdditionInterface)
registry.lookup("Addition");

int a = 5;
int b = 7;
int result = addition.add(a, b);
System.out.println("Result of adding " + a + " and " + b + " is: " +
result);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
4)Server Side:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class AdditionServer {


public static void main(String[] args) {
try {
AdditionInterface addition = new AdditionImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("Addition", addition);
System.out.println("Server started...");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Output:
i)

ii)

4) Write a Java RMI (Remote Method Invocation) program to


calculate the subtraction of two numbers.

Program:
1)Interface:
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface SubtractionInterface extends Remote {


int subtract(int a, int b) throws RemoteException;
}
2)Class:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class SubtractionImpl extends UnicastRemoteObject


implements SubtractionInterface {
public SubtractionImpl() throws RemoteException {
super();
}

@Override
public int subtract(int a, int b) throws RemoteException {
return a - b;
}
}

3)Client Side:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class SubtractionClient {


public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost");
SubtractionInterface subtraction = (SubtractionInterface)
registry.lookup("Subtraction");

int a = 10;
int b = 5;
int result = subtraction.subtract(a, b);
System.out.println("Result of subtracting " + b + " from " + a +
" is: " + result);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}

4)Server Side:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class SubtractionServer {


public static void main(String[] args) {
try {
SubtractionInterface subtraction = new SubtractionImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("Subtraction", subtraction);
System.out.println("Server started...");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}

You might also like