0% found this document useful (0 votes)
119 views6 pages

RMI Calculator Program in Java

The document describes a distributed calculator program using RMI in Java. It includes interfaces and classes for the calculator client and server. The Calculator interface defines remote methods for addition, subtraction, multiplication and division. CalculatorImpl implements this interface. RMICalculatorServer exports the implementation and binds it to the registry. RMICalculatorClient looks up the remote object and allows users to select and perform operations, inputting values and outputting results.

Uploaded by

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

RMI Calculator Program in Java

The document describes a distributed calculator program using RMI in Java. It includes interfaces and classes for the calculator client and server. The Calculator interface defines remote methods for addition, subtraction, multiplication and division. CalculatorImpl implements this interface. RMICalculatorServer exports the implementation and binds it to the registry. RMICalculatorClient looks up the remote object and allows users to select and perform operations, inputting values and outputting results.

Uploaded by

Sajid Qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/7/2021

Name: Muhammad Sajid


RollNo: 2K18/Swe/71
Department: BS Software Engineering

Subject: Distributed Computing (Lab)


ASSIGNMENT BY: SIR KAMRAN DAHRI
Faculty of Engineering and Technology
University of Sindh Jamshoro
LAB

Calculator Interface Program:


import java.rmi.*;
import java.rmi.server.*;

interface Calculator extends Remote{

double add(double no1, double no2) throws RemoteException;

double sub(double no1, double no2) throws RemoteException;

double mul(double no1, double no2) throws RemoteException;

double div(double no1, double no2) throws RemoteException;

Calculator Implementation:

import java.rmi.*;
import java.rmi.server.*;

public class CalculatorImpl implements Calculator{

public double add(double no1,double no2) throws RemoteException{

return no1+no2;

public double sub(double no1,double no2) throws RemoteException{

return no1-no2;

}
Faculty of Engineering and Technology
University of Sindh Jamshoro

public double mul(double no1,double no2) throws RemoteException{

return no1*no2;

public double div(double no1,double no2) throws RemoteException{

return no1/no2;

}
}

RMIServer Program:
import java.rmi.*;
import java.rmi.server.*;

class RMICalculatorServer{

public static void main(String[] args) throws


RemoteException,java.net.MalformedURLException{

CalculatorImpl calculator = new CalculatorImpl();

UnicastRemoteObject.exportObject(calculator);
Naming.rebind("Calculator",calculator);
System.out.println("RMI Server Started.");

RMIClient Program:
import java.rmi.*;

public class RMICalculatorClient{


Faculty of Engineering and Technology
University of Sindh Jamshoro

static java.util.Scanner input=null;


static int value1,value2;
public static int menu(){

System.out.println("press 1 for Addition");


System.out.println("press 2 for Subtraction");
System.out.println("press 3 for Multiplication");
System.out.println("press 4 for Divistion");
System.out.println("press 5 for Exit");
System.out.println("Enter Choice?");
return input.nextInt();
}
public static void userInput(){
System.out.print("Enter value 1:");
value1 = input.nextInt();
System.out.print("Enter value 2:");
value2 = input.nextInt();

}
public static void main(String[] args)throws
RemoteException,NotBoundException,java.net.MalformedURLException{
input = new java.util.Scanner(System.in);
int choice=0;
Calculator calculator=null;

while(true){

System.out.println("\nWelcome to Calculator\n");

try{
choice =RMICalculatorClient.menu();
calculator = (Calculator)Naming.lookup("Calculator");

}catch(NumberFormatException e){
e.printStackTrace();
}
if(choice==1){
userInput();
double add = calculator.add(value1,value2);
System.out.println("Addition: "+add);
}
else
if(choice==2){
userInput();
Faculty of Engineering and Technology
University of Sindh Jamshoro

double sub = calculator.sub(value1,value2);


System.out.println("Subtraction: "+sub );
}
else
if(choice==3){
userInput();
double mul = calculator.mul(value1,value2);
System.out.println("Multiplication: "+mul );
}

else
if(choice==4){
userInput();
double div = calculator.sub(value1,value2);
System.out.println("Division: "+div);
}
else
if(choice==5)
System.exit(0);
else
System.out.println("Invalid Choice");

}
}
Faculty of Engineering and Technology
University of Sindh Jamshoro

Program Snapshots:

You might also like