0% found this document useful (0 votes)
4 views3 pages

RMI Calculator Implementation in Java

The document contains a Java implementation of a remote calculator service using RMI (Remote Method Invocation). It defines a Calculator interface with methods for basic arithmetic operations, implements the interface in the CalculatorImpl class, and sets up a server and client for interaction. The client allows users to perform calculations by selecting operations and inputting numbers, while the server processes these requests remotely.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

RMI Calculator Implementation in Java

The document contains a Java implementation of a remote calculator service using RMI (Remote Method Invocation). It defines a Calculator interface with methods for basic arithmetic operations, implements the interface in the CalculatorImpl class, and sets up a server and client for interaction. The client allows users to perform calculations by selecting operations and inputting numbers, while the server processes these requests remotely.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

Remote;
import [Link];

public interface Calculator extends Remote {


double add(double a, double b) throws RemoteException;
double subtract(double a, double b) throws RemoteException;
double multiply(double a, double b) throws RemoteException;
double divide(double a, double b) throws RemoteException;
}

import [Link];
import [Link];

public class CalculatorImpl extends UnicastRemoteObject implements


Calculator {

protected CalculatorImpl() throws RemoteException {


super();

@Override
public double add(double a, double b) throws RemoteException {
return a + b;
}
@Override
public double subtract(double a, double b) throws RemoteException {
return a - b;
}
@Override
public double multiply(double a, double b) throws RemoteException {
return a * b;
}

@Override
public double divide(double a, double b) throws RemoteException {
if (b == 0) throw new ArithmeticException("Division by zero is not
allowed.");
return a / b;
}
}
import [Link];
import [Link];

public class CalculatorServer {


public static void main(String[] args) {
try {
Calculator calculator = new CalculatorImpl();
Registry registry = [Link](1099);
[Link]("Calculatorservice", calculator);
[Link]("Calculator Server is running ... ");
} catch (Exception e) {
[Link]();

}
}
}

import [Link];

import [Link];

import [Link];

public class CalculatorClient {

public static void main(String[] args) {

try {

Registry registry = [Link]("localhost", 1099);

Calculator calculator = (Calculator) [Link]("CalculatorService");

Scanner scanner = new Scanner([Link]);

while (true) {

[Link]("\nSelect operation: 1. Add 2. Subtract 3. Multiply [Link]


[Link]");

int choice = [Link]();

if (choice == 5) break;

[Link]("Enter first number: ");

double a = [Link]();

[Link]("Enter second number: ");

double b = [Link]();

double result = 0;
switch (choice) {

case 1: result = [Link](a, b); break;

case 2: result = [Link](a, b); break;

case 3: result = [Link](a, b); break;

case 4:

try {

result = [Link](a, b);

} catch (ArithmeticException e) {

[Link]("Error: " + [Link]());

continue;

break;

default: [Link]("Invalid choice."); continue;

[Link]("Result: "+ result);

[Link]();

} catch (Exception e) {

[Link]();

You might also like