0% found this document useful (0 votes)
7 views2 pages

Arithmetic Operations

The document outlines the implementation of a Remote Method Invocation (RMI) service in Java for basic arithmetic operations. It includes the definition of a remote interface, the implementation of that interface, and the creation of both a server and a client to perform operations like addition, subtraction, multiplication, and division. Error handling is also included for division by zero in the service implementation.

Uploaded by

ugpgcs22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Arithmetic Operations

The document outlines the implementation of a Remote Method Invocation (RMI) service in Java for basic arithmetic operations. It includes the definition of a remote interface, the implementation of that interface, and the creation of both a server and a client to perform operations like addition, subtraction, multiplication, and division. Error handling is also included for division by zero in the service implementation.

Uploaded by

ugpgcs22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Step1 : Define the remote interface

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ArithmeticService extends Remote {
int add(int a, int b) throws RemoteException;
int subtract(int a, int b) throws RemoteException;
int multiply(int a, int b) throws RemoteException;
double divide(int a, int b) throws RemoteException;
}

Step 2: Implement the Remote Interface

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ArithmeticServiceImpl extends UnicastRemoteObject implements
ArithmeticService {
protected ArithmeticServiceImpl() throws RemoteException {
super();
}
public int add(int a, int b) throws RemoteException {
return a + b;
}
public int subtract(int a, int b) throws RemoteException {
return a - b;
}
public int multiply(int a, int b) throws RemoteException {
return a * b;
}
public double divide(int a, int b) throws RemoteException {
if (b == 0) {
throw new RemoteException("Cannot divide by zero");
}
return (double) a / b;
}
}

Step 3: Create the Server

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class ArithmeticServer {
public static void main(String[] args) {
try {
ArithmeticService service = new ArithmeticServiceImpl();
Registry registry = LocateRegistry.createRegistry(1099); // Default RMI registry
port
registry.rebind("ArithmeticService", service);
System.out.println("ArithmeticService is running...");
} catch (Exception e) {
System.err.println("ArithmeticService exception:");
e.printStackTrace();
}
}
}

Step 4: Create the Client


import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class ArithmeticClient {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost");
ArithmeticService service = (ArithmeticService)
registry.lookup("ArithmeticService");
// Perform operations
int resultAdd = service.add(10, 5);
int resultSubtract = service.subtract(10, 5);
int resultMultiply = service.multiply(10, 5);
double resultDivide = service.divide(10, 5);
System.out.println("Addition result: " + resultAdd);
System.out.println("Subtraction result: " + resultSubtract);
System.out.println("Multiplication result: " + resultMultiply);
System.out.println("Division result: " + resultDivide);
} catch (Exception e) {
System.err.println("ArithmeticClient exception:");
e.printStackTrace();
}
}
}

You might also like