Hi, ive been working on this problem for about 5 hours now and haven't found a way to resolve it. It's an RMI program that has to be able to return the mean, mode and median of a set of numbers.
It's composed of 4 classes; a client, an implementation class (with the math), an Interface, and a server class.
Here's all of the code
Client
import java.rmi.NotBoundException; import java.rmi.RMISecurityManager; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.io.*; public class rmiClient { public static void main (String[ ] args) throws RemoteException, NotBoundException { if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } String hostName = "localhost"; // default host name // assign host machine name to connect to if (args.length != 0) { if (args[0] != null) hostName = args[0]; // user specified machine } try { // connect to the remote object via the registry Registry registry = LocateRegistry.getRegistry(hostName); rmiInterface stub = (rmiInterface) registry.lookup("myFunctions"); Scanner rmiScanner = new Scanner(System.in); System.out.println( '\n' + "Enter the numbers to be calculated" + '\n'); int [] values = new int [8]; int math; int number=0; int breaker; int i = 0; while (true && i<8) { breaker = rmiScanner.nextInt(); if (breaker <=0) break; values[i]= breaker; i++; } String tempArray = Arrays.toString(values); System.out.println(); System.out.println("Enter your Selection: "); System.out.println("1 - Return the mean"); System.out.println("2 - Return the mode"); System.out.println("3 - Return the median"); System.out.println("4 - Exit"); math = rmiScanner.nextInt(); //read client request int results; results=0; if (math==1) { System.out.println("The mean is "+stub.mean(avg)); } } catch (Exception e) {System.out.println("Error : " + e); } System.exit(0); } }
Implementation
import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; public class rmiImpl implements rmiInterface { int avg; // default constructor public rmiImpl() { super(); } // end constructor public int mean (int number){ // calculates the mean of the numbers int[] values = new int [8]; int i = 0; for (i=0; i< values.length; i++) { avg = avg + values[i]; } return avg / values.length; } @Override public int mode(int mode) throws RemoteException { // TODO Auto-generated method stub return 0; } @Override public int median(int median) throws RemoteException { // TODO Auto-generated method stub return 0; } /*public int mode (int mode){ int maxval = 0, maxCount = 0; for (int i = 0; i < values.length; i++) { int count = 0; for (int j = 0; j < values.length; ++j) { if (values[j] == values[i]) ++count; } if (count > maxCount) { maxCount = count; maxval = values[i]; } } return maxval; } public int median (int median) { int mid = values.length/2; if (values.length%2 == 1) { return values[mid]; } else { return (int) ((values[mid-1] + values[mid]) / 2.0); } } */ }
Interface
import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; public interface rmiInterface extends Remote { public int mean(int number) throws RemoteException; public int mode (int mode) throws RemoteException; public int median (int median) throws RemoteException; }
Server class
import java.rmi.RMISecurityManager; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; public class Serverclass { public static void main (String args[]) { if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } try { rmiImpl impl = new rmiImpl(); rmiInterface rmiStub = (rmiInterface) UnicastRemoteObject.exportObject(impl, 0); // Bind the remote object's stub in the registry Registry registry = LocateRegistry.getRegistry(); registry.bind("myFunctions", rmiStub); } catch (Exception e) {System.out.println("Error: " + e); } System.out.println("Server started waiting ..."); } // end main } // end CensorServer
If anyone could have a read over this i'd be hugely appreciative since i'm at a total loss at the moment.
I figure that I need to use this excerpt from rmiClient
if (math==1) { System.out.println("The mean is "+stub.mean(avg)); }
to display the results of the calculation performed in the implementation class..
Anyway hopefully someone can help,
thanks