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]();