Question: Write a code in JAVA Please use this Remote Method Invocation RMI example to perform the addition method call. Add try-catches, the imports, class definitions,

Write a code in JAVA

Please use this Remote Method Invocation RMI example to perform the addition method call. Add try-catches, the imports, class definitions, and the other mathematical calculator operations needed for the subtract, multiply, and divide operations.

Your calculator should implement the following remote methods:

double add(double operand1, double operand2)

double subtract(double operand1, double operand2)

double multiply(double operand1, double operand2)

double divide(double operand1, double operand2)

You should have four files in your project: Calculator Interface, Calculator Implementation, Calculator Server, and Calculator Client.

//Make sure to add the following imports

import java.rmi.Remote;

import java.rmi.RemoteException;

//Calculator Interfaceneeded for RMI

public interface Calculator extends Remote{

double add(double operand1,double operand2) throws RemoteException; //Addition interface

//add the other operations for the subtract, multiply, and divide

}

//Calculator Implementation class

public class CalculatorImpl extends UnicastRemoteObject implements Calculator{

public double add(double operand1,double operand2) throws RemoteException{

return operand1 + operand2; //Addition implementation

//add the other operations for the subtract, multiply, and divide

}

//Calculator Client class- makes the remote method call for all four operations (add, subtract, multiply, divide)

//Shown below is just the add operation add the other operations for the subtract, multiply, and divide

public static void main(String[]args) {

Calculator calc = (Calculator)Naming.lookup(\"rmi://localhost:1999/Calculator\"); //Port number = 1999

try{

System.out.println(calc.add(10,5)); //Add two Numbers RMI call

} catch (RemoteException e){

System.out.println(\"Remote exception: \" e.getMessage());

}

//Calculator Server class - listens for the RMI requests

public static void main(String [] args) throws RemoteException {

Registry registry = LocateRegistry.createRegistry(1999); //Port number = 1999

registry.rebind(\"Calculator\", new CalculatorImpl()); //Start the RMI Service first

System.out.println(\"Server is Running\");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!