0% found this document useful (0 votes)
85 views23 pages

RMI Factorial Calculation Example

The document describes a Java program that uses RMI to calculate factorials remotely. The client program calls a remote method on the server to calculate the factorial of a number input by the user. The server interface defines the remote method. The server implementation class extends UnicastRemoteObject and implements the remote interface. The server program binds the implementation object to make it accessible. When run, the client inputs a number and the server calculates and returns the factorial, which is displayed to the user.

Uploaded by

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

RMI Factorial Calculation Example

The document describes a Java program that uses RMI to calculate factorials remotely. The client program calls a remote method on the server to calculate the factorial of a number input by the user. The server interface defines the remote method. The server implementation class extends UnicastRemoteObject and implements the remote interface. The server program binds the implementation object to make it accessible. When run, the client inputs a number and the server calculates and returns the factorial, which is displayed to the user.

Uploaded by

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

1.

RMI pgm to calculate factorial of a number


PROGRAM CLIENT PROGRAM:

import [Link].*; import [Link].*; public class client { public static void main(String args[])throws Exception { try { String s="rmi://"+args[0]+"/abc"; serverint f=(serverint)[Link](s); DataInputStream m=new DataInputStream([Link]); int n1=[Link]([Link]()); [Link]("the factorial is"+[Link](n1)); } catch(Exception e) { [Link](e); } } } INTERFACE PROGRAM: import [Link].*; public interface serverint extends Remote { int fact(int n)throws Exception; } IMPLEMENTATION PROGRAM: import [Link].*; import [Link].*; public class serverimpl extends UnicastRemoteObject implements serverint { public serverimpl()throws Exception { } public int fact(int n) { int i,c=1; for(i=1;i<=n;i++) { c=i*c;

} return c; } } SERVER PROGRAM: import [Link].*; import [Link].*; public class server { public static void main(String args[]) { try { serverimpl m=new serverimpl(); [Link]("abc",m); } catch(Exception e) { [Link]("Exception"+e); } } }

OUTPUT: SERVER WINDOW: C:\vino20>javac [Link] C:\vino20>javac [Link] C:\vino20>javac [Link] C:\vino20>rmic serverimpl C:\vino20>start rmiregistry C:\vino20>java server CLIENT WINDOW: C:\vino20>javac [Link] Note: [Link] uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\vino20>java client localhost 3 the factorial is 6

6(a)FACT and FIB import [Link].*; interface factandfib { void fact(int a); void fib(int a); }

class imp implements factandfib { public void fact(int a) { int fact=1; for(int i=1;i<=a;i++) fact=fact*i; [Link]("factorial of given number="+fact); } public void fib(int a) { int p,c,n; [Link]("Fibnocci Series"); p=0; c=1; [Link](" "+p+" "+c); for(int i=1;i<=a;i++) { n=c+p; [Link](" "+n); p=c; c=n; } } }

class Demo1 { public static void main(String ar[]) throws IOException { int num,op; imp i=new imp(); do { DataInputStream ob=new DataInputStream([Link]);

[Link]("\nEnter a number:"); num=[Link]([Link]()); [Link]("\nMENU\[Link]\[Link] Series\[Link]"); [Link]("Enter your option:"); op=[Link]([Link]()); switch(op) { case 1: [Link](num); break; case 2: [Link](num); break; case 3: break; } }while(op!=3); } } 6(b)Applet to convert Fahrenheit to Celsius import [Link].*; import [Link].*; import [Link]; import [Link].*; public class TempConversion extends Applet { private Label lblFahrenheit, lblCelsius; private TextField txtFahrenheit, txtCelsius; private Button Calculate; private Panel panel;. public void init () { setBackground ([Link]); panel = new Panel (); [Link] (new GridLayout (5, 1, 12, 12)); lblFahrenheit = new Label ("Fahrenheit"); txtFahrenheit = new TextField (10); lblCelsius = new Label ("Celsius"); txtCelsius = new TextField (10); Calculate = new Button ("Calculate"); [Link] (new CalculateListener ()); [Link] (lblFahrenheit); [Link] (txtFahrenheit); [Link] (lblCelsius);

[Link] (txtCelsius); [Link] (Calculate); add (panel); } class CalculateListener implements ActionListener { public void actionPerformed (ActionEvent event) { double fahrenheit, celsius; fahrenheit = [Link] (txtFahrenheit); celsius = (fahrenheit - 32) * 5 / 9; [Link] ("" + [Link] (celsius)); } } } class AppletIO { . public static double getDouble (TextField box) { double number; try { number = [Link] ([Link] ()).doubleValue (); } catch (NumberFormatException e) { number = 0;} return number; } public static String decimals (double number) { DecimalFormat decFor = new DecimalFormat (); [Link] (2); [Link] (2); return [Link] (number); }. public static int getInteger (TextField box) { int number; try { number = [Link] ([Link]()); } catch (NumberFormatException e) { number = 0;} return number; } } /*<HTML> <HEAD>

<TITLE>Temperature Conversion</TITLE> </HEAD> <BODY> <H1>Temperature Conversion</H1> <APPLET CODE="[Link]" WIDTH=300 HEIGHT=300></APPLET> </BODY> </HTML> */ 7(a)Java pgm using interface import java .io.*; interface sbi_acc { public void balance(); } class Account implements sbi_acc { String acc_type,name; int acc_no,bal,init; Account(String n,String a,int num) { name=n; acc_type=a; acc_no=num; } public void balance() { [Link]("Balance:"+bal); } void display() { [Link]("Name:"+name); [Link]("Account Num:"+acc_no); [Link]("acc_type:"+acc_type); balance(); } void assign_init() { bal=0; } void deposit(int am) { [Link]("Deposited Amount:"+am); bal=bal+am; balance(); } void withdraw(int wd) { [Link]("Withdrawn Amount:"+wd); bal=bal-wd;

balance(); } } class MainAcc { public static void main(String args[])throws IOException { DataInputStream in=new DataInputStream([Link]); String n,ac; int an,op=0,dep,wid; [Link]("Enter Name:"); n=[Link](); [Link]("Enter Number:"); an=[Link]([Link]()); [Link]("Enter AccType:"); ac=[Link](); Account a1=new Account(n,ac,an); while(op!=4) { [Link]("Menu"); [Link]("[Link]"); [Link]("[Link]"); [Link]("[Link] Draw"); [Link]("Enter ur option"); op=[Link]([Link]()); switch(op) { case 1: [Link]("Enter Amount to Deposit"); dep=[Link]([Link]()); [Link](dep); break; case 2: [Link]("SBI-Details"); [Link](); break; case 3: [Link]("Enter Amount to Withdraw"); wid=[Link]([Link]()); [Link](wid); break; default: [Link]("Invalid Request"); } }

}}

7(b)Applet pgm to add two numbers import [Link].*; import [Link].*; import [Link].*; import [Link].*; /* <applet code="Addition" width=250 height=250> </applet> */ public class Addition extends Applet implements ActionListener { TextField t1,t2,t3; Button b1; public void init() { t1=new TextField(); t2=new TextField(); t3=new TextField(); b1=new Button("Add"); add(t1); add(t2); add(t3); add(b1); [Link](this); } public void actionPerformed(ActionEvent ae) { double d1=[Link]([Link]()); int s1=(int)d1; double d2=[Link]([Link]()); int s2=(int)d2; int s3=s1+s2; String s4=[Link](s3); [Link](s4); } }

8(a)Multilevel Inheritance(not yet completed)

8(b)This keyword import [Link].*; class Rec { int length; int breadth; Rec(int length,int breadth) { [Link]=length; [Link]=breadth; } int area() { return(length*breadth); } } class This { public static void main(String arg[]) throws IOException { int l,b,c,d; DataInputStream n=new DataInputStream([Link]); [Link]("\nEnter two values:"); l=[Link]([Link]()); b=[Link]([Link]()); Rec r1,r2; r1=new Rec(l,b); r2=new Rec(l,b); c=[Link](); d=[Link](); [Link]("Area of rectangle using Object r1="+c); [Link]("Area of rectangle using Object r2="+d); } } 9(a)Method overloading class overloading {

public static void main(String[] args) { functionOverload obj = new functionOverload(); [Link](1,2); [Link]("Life at","?"); [Link](11.5, 22.5); } } class functionOverload { void add(int a, int b, int c) {

int sum = a + b + c; [Link]("Sum of a+b+c is "+sum); } void add(double a, double b) {

double sum = a + b; [Link]("Sum of a+b is "+sum); } void add(String s1, String s2) { String s = s1+s2; [Link](s); } } Output E:\pgm>javac [Link] E:\pgm>java overloading Sum of a+b is 3.0 Life at? Sum of a+b is 34.0 9(b) Applet program to draw lines,rectangle,oval

//java program that allows user to draw lines,rectangles and ovals //<applet code="[Link]" height="250" width="400"> </applet> import [Link].*; import [Link].*; import [Link].*; public class LinesRectsOvals extends JApplet { public void paint(Graphics g) { [Link](g); [Link]([Link]); [Link](5,30,350,30); [Link]([Link]); [Link](5,40,90,55); [Link](100,40,90,55); [Link]([Link]); [Link](195,40,90,55,50,50); [Link](290,40,90,55,20,20); [Link]([Link]); g.draw3DRect(5,100,90,55,true); g.fill3DRect(100,100,90,55,false); [Link]([Link]); [Link](195,100,90,55); [Link](290,100,90,55); } }

10(a)Method Overridding import [Link].*; class student {

int id; String name; int marks1,marks2,marks3; void read() throws IOException { DataInputStream in=new DataInputStream([Link]); [Link]("\nEnter the details"); [Link]("\nId:"); id=[Link]([Link]()); [Link]("\nName:"); name=[Link](); [Link]("\nMarks1:"); marks1=[Link]([Link]()); [Link]("\nMarks2:"); marks2=[Link]([Link]()); [Link]("\nMarks3:"); marks3=[Link]([Link]()); } void show() { [Link]("\ndetails"); [Link]("\nId:"+id); [Link]("\nName:"+name); [Link]("\nMarks1:"+marks1); [Link]("\nMarks2:"+marks2); [Link]("\nMarks3:"+marks3); } } class sports_student extends student { int extra_marks; void input() throws IOException { DataInputStream in=new DataInputStream([Link]); [Link]("\n Enter the Extra marks for sports student:"); extra_marks=[Link]([Link]()); } void show() { [Link]("\nSports weightage:"+extra_marks); } } class Overridding {

public static void main(String[] args) throws IOException { student s=new student(); sports_student s1=new sports_student(); student r; [Link](); [Link](); r=s; [Link](); r=s1; [Link](); } } 10(b) Reading a file,Checking for file existence and type import [Link]; import [Link]; public class Filedemo { public static void main(String[] args) { Scanner input=new Scanner([Link]); [Link]("\nEnter the file name:"); String s=[Link](); File f1=new File(s); [Link]("File Name:"+[Link]()); [Link]("Path:"+[Link]()); [Link]("Abs Path:"+[Link]()); [Link]("Parent:"+[Link]()); [Link]("This file is:"+([Link]()?"Exists":"Does not exists")); [Link]("Is file:"+[Link]()); [Link]("Is Directory:"+[Link]()); [Link]("Is Readable:"+[Link]()); [Link]("IS Writable:"+[Link]()); [Link]("File Size:"+[Link]()+"bytes"); [Link]("Is Hidden:"+[Link]()); } } OUTPUT E:\pgm>java Filedemo Enter the file name: [Link] File Name:[Link] Path:[Link] Abs Path:E:\pgm\[Link] Parent:null This file is:Exists Is file:true

Is Directory:false Is Readable:true IS Writable:true File Size:249bytes Is Hidden:false 11(a)Java pgm for in-built exception import [Link].*; class Exception { public static void main(String args[])throws IOException { DataInputStream n=new DataInputStream([Link]); try { int a,b=42,c,d; a=[Link]; c=b/a; [Link]("C="+c); [Link]("Enter a Number:"); d=[Link]([Link]()); } catch(ArithmeticException e) { [Link]("CaughtException:"+e); } catch(NumberFormatException e) { [Link]("CaughtException:"+e); }}} 11(b)Applet to display college information import [Link].*; import [Link].*; import [Link].*; /* <applet code="LayCollege" width=250 height=250> </applet> */ public class LayCollege extends Applet { TextArea t1; Label l1,l2; public void init() { l1=new Label("Narayanaguru College"); l2=new Label("Manjalumoodu,Marthandom,Tamil Nadu.");

String str="Narayanaguru College Is the Best College In Tamilnadu. It provides Lot of degrees in Engineering. College is managed by Sidhardhan. \n Best College in India.\n Courses \n-------\[Link] \[Link]\[Link] "; t1=new TextArea(str); setLayout(new BorderLayout()); add(l1,[Link]); add(t1,[Link]); add(l2,[Link]); }} 13(a)Java pgm to perform arithmetic op using packages Package package arith_pack; public class Arithmetic { public int a,b,c; public Arithmetic(int i,int j) { a=i; b=j; } public void add() { c=a+b; [Link]("Sum:"+c); } public void sub() { c=a-b; [Link]("Difference:"+c); } public void mul() { c=a*b; [Link]("Product:"+c); } public void div() { c=a/b; [Link]("Division:"+c); } } Package Implementation import arith_pack.*; import [Link].*; class Arith

{ public static void main(String arg[]) throws IOException { DataInputStream n=new DataInputStream([Link]); int i,j; [Link]("Enter First Value:"); i=[Link]([Link]()); [Link]("Enter Second Value:"); j=[Link]([Link]()); Arithmetic ar=new Arithmetic(i,j); [Link](); [Link](); [Link](); [Link](); }} 13(b) Applet programto display arc on mouse click and display your name on mouse drag import [Link].*; import [Link].*; import [Link].*; /* <applet code="MouseEvents" width=300 height=100> </applet> */ public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg = ""; int flag=3; int mouseX = 0, mouseY = 0; public void init() { addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent me) { flag=0; mouseX = [Link]();; mouseY = [Link](); msg = "Mouse clicked."; repaint(); } public void mouseEntered(MouseEvent me) { public void mouseExited(MouseEvent me) { } }

public void mousePressed(MouseEvent me) { public void mouseReleased(MouseEvent me) { public void mouseDragged(MouseEvent me) {

} }

flag=1; mouseX = [Link](); mouseY = [Link](); msg = "Geethu"; showStatus("Dragging mouse at " + mouseX + ", " + mouseY); repaint(); } public void mouseMoved(MouseEvent me) { } // Display msg in applet window at current X,Y location. public void paint(Graphics g) { if(flag==1) [Link](msg, mouseX, mouseY); if(flag==0) [Link](mouseX-20, mouseY-20,70,70,0,75); } } 14(a)Thread Record pgm 14(b)Reading and Writing files Record pgm 17(a) Thread Application Record pgm

17(b) import [Link].*; import [Link].*; import [Link].*; /* <applet code="Exam5" width=300 height=200> </applet> */ public class Exam5 extends Applet implements ActionListener { String isFill="N", isNoFill="N"; Button Fill = new Button("Fill");

Button NoFill = new Button("No Fill"); public void init() { add(Fill); add(NoFill); [Link](this); [Link](this); } public void actionPerformed(ActionEvent e) { if ([Link]()==Fill) { isFill="Y"; isNoFill="N"; repaint(); } if ([Link]()==NoFill) { isFill="N"; isNoFill="Y"; repaint(); } } public void paint(Graphics gscreen) { [Link]([Link]); if(isFill=="Y") { [Link](200,10,60,50); } else if (isNoFill=="Y") { [Link](200,10,60,50); } } }

18(a)Cash Register import [Link].*; import [Link].*; import [Link].*; class exam8 { String[] name=new String[20]; double[] price=new double[20]; double discprice=0; double disc=0.0; double tendamt,change,tot=0; int i,j=1;; void display()throws IOException

{ DataInputStream in=new DataInputStream([Link]); [Link]("\nThe Cash Register:"); for(j=1;j<=i;j++) { [Link](name[j]+":"+price[j]); } if(tot>200) { disc=tot*0.05; [Link]("Discount:"+disc); discprice=tot-disc; [Link]("Discounted price:"+discprice); } [Link]("\nEnter the tendered amount:"); tendamt=[Link]([Link]()); [Link]("Tender:"+tendamt); change=tendamt-discprice; [Link]("Change:"+change); } void input()throws IOException { DataInputStream in=new DataInputStream([Link]); [Link]("\nCASH REGISTER"); [Link]("\nEnter item name and price"); String str; i=1; do { [Link]("Enter the article"+i+":"); name[i]=[Link](); str=name[i].toString(); if([Link]("Exit")) { name[i]=" "; display(); [Link](0); } [Link]("\nEnter the price of "+name[i]+":"); price[i]=[Link]([Link]()); tot=tot+price[i]; i=i+1; [Link](i); } while(![Link]("Exit")); }

} class cash { public static void main(String args[])throws IOException { exam8 c=new exam8(); [Link](); [Link](); } } OUTPUT CASH REGISTER Enter item name and price Enter the article1: Pen Enter the price of Pen: 20.0 2 Enter the article2: Books Enter the price of Books: 300.0 3 Enter the article3: Exit The Cash Register: Pen:20.0 Books:300.0 Discount:16.0 Discounted price:304.0 Enter the tendered amount:400.0 Tender:400.0 Change:96.0

19(a)Constructor Overloading 19(b)Applet pgm to draw various shapes //java program that allows user to draw lines,rectangles and ovals //<applet code="[Link]" height="250" width="400"> </applet> import [Link].*; import [Link].*; import [Link].*; public class LinesRectsOvals extends JApplet { public void paint(Graphics g) { [Link](g); [Link]([Link]); [Link](5,30,350,30); [Link]([Link]); [Link](5,40,90,55); [Link](100,40,90,55); [Link]([Link]); [Link](195,40,90,55,50,50); [Link](290,40,90,55,20,20); [Link]([Link]); g.draw3DRect(5,100,90,55,true); g.fill3DRect(100,100,90,55,false); [Link]([Link]); [Link](195,100,90,55); [Link](290,100,90,55); } }

You might also like