Java Blog

 
 
 
// ResultRmi Interface

public interface ResultRMI extends java.rmi.Remote
{
    String calcresult(double marks[],int count) throws java.rmi.RemoteException;
}


// ResultRMIImpl.java, ResultRMI implementation

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

public class ResultRMIImpl extends UnicastRemoteObject implements ResultRMI
{

  public ResultRMIImpl(String name) throws RemoteException
  {
    super();
    try
    {
      Naming.rebind(name, this);
    } 
    catch (Exception e)
    { 
      System.out.println(\"Exception: \" + e.getMessage());
      e.printStackTrace();
    }
  }

  public String calcresult(double marks[],int count) throws RemoteException
  { 
    double total=0.0,average;
    for(int i=0;i<count;i++)
    {
        total=total+marks[i];
    }
    average = total/count;
    if(average >= 70)
    {
        return \"Distinction\";
    }
    else if(average >=60 && average < 70)
    {
        return \"Firstclass\";
    }
    else if(average >=50 && average < 60)
    {
        return \"secondclass\";
    }
    else if(average >=40 && average <50)
    {
        return \"passcalss\";
    }
    else 
    {
        return \"fail\";
    }

  }
}


// ResultRMIServer.java

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

public class ResultRMIServer
{

 public static void main(String args[])
 {

   // Create and install the security manager
   System.setSecurityManager(new RMISecurityManager());

   try
   {
     // Create ResultRMIImpl
     ResultRMIImpl myResult = new ResultRMIImpl(\"//Binita/myResultRMI\");
     System.out.println(\"ResultRMI Server ready.\");
   } 
   catch (Exception e)
   { 
     System.out.println(\"Exception: \" + e.getMessage());
     e.printStackTrace();
   }
 }
}


// ResultRMIClient.java  RMI Result client

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.io.*;
import java.lang.*;

public class ResultRMIClient
{ public static void main(String args[]) throws Exception
  { 
    // Create and install the security manager
    System.setSecurityManager(new RMISecurityManager());

    FileReader fr = new FileReader(\"studentdetails.txt\");
    StreamTokenizer tok = new StreamTokenizer(fr);
    String name=\"\",classval,s;
    double marks[] = new double[10];
    int i=0;
    tok.eolIsSignificant(true);
    try
    { 
        ResultRMI myResult = (ResultRMI)Naming.lookup(\"//\"
                          + args[0] + \"/\" + \"myResultRMI\");

        while( tok.nextToken() != tok.TT_EOF)
    {

        if(tok.ttype == tok.TT_WORD)
        {
            name = tok.sval;
            System.out.println(\"\\nname is:\"+name);
            System.out.println(\"Obtained marks are:\");
        }

        if(tok.ttype == tok.TT_NUMBER)
        {

            marks[i] = tok.nval;    
            System.out.println(\"Marks of subject \"+i +\" is \"+marks[i]);
            i++;

        }    
        if(tok.ttype == tok.TT_EOL && tok.ttype != tok.TT_EOF)
        {
            classval = myResult.calcresult(marks,i);
            System.out.println(name +\" has \" + classval);
            i=0;

        }     
    }

    fr.close();

    } 
    catch(Exception e)
    { 
        System.err.println(\"System Exception\" + e);
    }
   System.exit(0);
  }
}


// OUTPUT

-----  studentdetails.txt  ------

binita 45 56 74 68
rasesh 88 85 96 92
krupa 41 42 51 52
nauka 62 53 62 48
rinkal 35 38 41 40
--------------------




name is:binita
Obtained marks are:
Marks of subject 0 is 45.0
Marks of subject 1 is 56.0
Marks of subject 2 is 74.0
Marks of subject 3 is 68.0
binita has Firstclass

name is:rasesh
Obtained marks are:
Marks of subject 0 is 88.0
Marks of subject 1 is 85.0
Marks of subject 2 is 96.0
Marks of subject 3 is 92.0
rasesh has Distinction

name is:krupa
Obtained marks are:
Marks of subject 0 is 41.0
Marks of subject 1 is 42.0
Marks of subject 2 is 51.0
Marks of subject 3 is 52.0
krupa has passcalss

name is:nauka
Obtained marks are:
Marks of subject 0 is 62.0
Marks of subject 1 is 53.0
Marks of subject 2 is 62.0
Marks of subject 3 is 48.0
nauka has secondclass

name is:rinkal
Obtained marks are:
Marks of subject 0 is 35.0
Marks of subject 1 is 38.0
Marks of subject 2 is 41.0
Marks of subject 3 is 40.0
rinkal has fail