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

1.RMI PGM To Calculate Factorial of A Number: Program Client Program

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)
237 views23 pages

1.RMI PGM To Calculate Factorial of A Number: Program Client Program

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
You are on page 1/ 23

1.

RMI pgm to calculate factorial of a number


PROGRAM CLIENT PROGRAM:

import java.io.*; import java.rmi.*; public class client { public static void main(String args[])throws Exception { try { String s="rmi://"+args[0]+"/abc"; serverint f=(serverint)Naming.lookup(s); DataInputStream m=new DataInputStream(System.in); int n1=Integer.parseInt(m.readLine()); System.out.println("the factorial is"+f.fact(n1)); } catch(Exception e) { System.out.println(e); } } } INTERFACE PROGRAM: import java.rmi.*; public interface serverint extends Remote { int fact(int n)throws Exception; } IMPLEMENTATION PROGRAM: import java.rmi.*; import java.rmi.server.*; 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 java.net.*; import java.rmi.*; public class server { public static void main(String args[]) { try { serverimpl m=new serverimpl(); Naming.rebind("abc",m); } catch(Exception e) { System.out.println("Exception"+e); } } }

OUTPUT: SERVER WINDOW: C:\vino20>javac serverint.java C:\vino20>javac serverimpl.java C:\vino20>javac server.java C:\vino20>rmic serverimpl C:\vino20>start rmiregistry C:\vino20>java server CLIENT WINDOW: C:\vino20>javac client.java Note: client.java 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 java.io.*; 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; System.out.println("factorial of given number="+fact); } public void fib(int a) { int p,c,n; System.out.println("Fibnocci Series"); p=0; c=1; System.out.print(" "+p+" "+c); for(int i=1;i<=a;i++) { n=c+p; System.out.print(" "+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(System.in);

System.out.println("\nEnter a number:"); num=Integer.parseInt(ob.readLine()); System.out.println("\nMENU\n1.Factorial\n2.Fibnocci Series\n3.Exit"); System.out.println("Enter your option:"); op=Integer.parseInt(ob.readLine()); switch(op) { case 1: i.fact(num); break; case 2: i.fib(num); break; case 3: break; } }while(op!=3); } } 6(b)Applet to convert Fahrenheit to Celsius import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.text.*; public class TempConversion extends Applet { private Label lblFahrenheit, lblCelsius; private TextField txtFahrenheit, txtCelsius; private Button Calculate; private Panel panel;. public void init () { setBackground (Color.cyan); panel = new Panel (); panel.setLayout (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"); Calculate.addActionListener (new CalculateListener ()); panel.add (lblFahrenheit); panel.add (txtFahrenheit); panel.add (lblCelsius);

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

<TITLE>Temperature Conversion</TITLE> </HEAD> <BODY> <H1>Temperature Conversion</H1> <APPLET CODE="TempConversion.class" 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() { System.out.println("Balance:"+bal); } void display() { System.out.println("Name:"+name); System.out.println("Account Num:"+acc_no); System.out.println("acc_type:"+acc_type); balance(); } void assign_init() { bal=0; } void deposit(int am) { System.out.println("Deposited Amount:"+am); bal=bal+am; balance(); } void withdraw(int wd) { System.out.println("Withdrawn Amount:"+wd); bal=bal-wd;

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

}}

7(b)Applet pgm to add two numbers import java.awt.*; import java.awt.event.*; import java.applet.*; import java.lang.*; /* <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); b1.addActionListener(this); } public void actionPerformed(ActionEvent ae) { double d1=Double.parseDouble(t1.getText()); int s1=(int)d1; double d2=Double.parseDouble(t2.getText()); int s2=(int)d2; int s3=s1+s2; String s4=String.valueOf(s3); t3.setText(s4); } }

8(a)Multilevel Inheritance(not yet completed)

8(b)This keyword import java.io.*; class Rec { int length; int breadth; Rec(int length,int breadth) { this.length=length; this.breadth=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(System.in); System.out.println("\nEnter two values:"); l=Integer.parseInt(n.readLine()); b=Integer.parseInt(n.readLine()); Rec r1,r2; r1=new Rec(l,b); r2=new Rec(l,b); c=r1.area(); d=r2.area(); System.out.println("Area of rectangle using Object r1="+c); System.out.println("Area of rectangle using Object r2="+d); } } 9(a)Method overloading class overloading {

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

int sum = a + b + c; System.out.println("Sum of a+b+c is "+sum); } void add(double a, double b) {

double sum = a + b; System.out.println("Sum of a+b is "+sum); } void add(String s1, String s2) { String s = s1+s2; System.out.println(s); } } Output E:\pgm>javac overloading.java 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="LinesRectsOvals.class" height="250" width="400"> </applet> import java.applet.*; import java.awt.*; import javax.swing.*; public class LinesRectsOvals extends JApplet { public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.drawLine(5,30,350,30); g.setColor(Color.blue); g.drawRect(5,40,90,55); g.fillRect(100,40,90,55); g.setColor(Color.cyan); g.fillRoundRect(195,40,90,55,50,50); g.drawRoundRect(290,40,90,55,20,20); g.setColor(Color.yellow); g.draw3DRect(5,100,90,55,true); g.fill3DRect(100,100,90,55,false); g.setColor(Color.magenta); g.drawOval(195,100,90,55); g.fillOval(290,100,90,55); } }

10(a)Method Overridding import java.io.*; class student {

int id; String name; int marks1,marks2,marks3; void read() throws IOException { DataInputStream in=new DataInputStream(System.in); System.out.println("\nEnter the details"); System.out.println("\nId:"); id=Integer.parseInt(in.readLine()); System.out.println("\nName:"); name=in.readLine(); System.out.println("\nMarks1:"); marks1=Integer.parseInt(in.readLine()); System.out.println("\nMarks2:"); marks2=Integer.parseInt(in.readLine()); System.out.println("\nMarks3:"); marks3=Integer.parseInt(in.readLine()); } void show() { System.out.println("\ndetails"); System.out.println("\nId:"+id); System.out.println("\nName:"+name); System.out.println("\nMarks1:"+marks1); System.out.println("\nMarks2:"+marks2); System.out.println("\nMarks3:"+marks3); } } class sports_student extends student { int extra_marks; void input() throws IOException { DataInputStream in=new DataInputStream(System.in); System.out.println("\n Enter the Extra marks for sports student:"); extra_marks=Integer.parseInt(in.readLine()); } void show() { System.out.println("\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; s.read(); s1.input(); r=s; r.show(); r=s1; r.show(); } } 10(b) Reading a file,Checking for file existence and type import java.util.Scanner; import java.io.File; public class Filedemo { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("\nEnter the file name:"); String s=input.nextLine(); File f1=new File(s); System.out.println("File Name:"+f1.getName()); System.out.println("Path:"+f1.getPath()); System.out.println("Abs Path:"+f1.getAbsolutePath()); System.out.println("Parent:"+f1.getParent()); System.out.println("This file is:"+(f1.exists()?"Exists":"Does not exists")); System.out.println("Is file:"+f1.isFile()); System.out.println("Is Directory:"+f1.isDirectory()); System.out.println("Is Readable:"+f1.canRead()); System.out.println("IS Writable:"+f1.canWrite()); System.out.println("File Size:"+f1.length()+"bytes"); System.out.println("Is Hidden:"+f1.isHidden()); } } OUTPUT E:\pgm>java Filedemo Enter the file name: server2.java File Name:server2.java Path:server2.java Abs Path:E:\pgm\server2.java 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 java.io.*; class Exception { public static void main(String args[])throws IOException { DataInputStream n=new DataInputStream(System.in); try { int a,b=42,c,d; a=args.length; c=b/a; System.out.println("C="+c); System.out.println("Enter a Number:"); d=Integer.parseInt(n.readLine()); } catch(ArithmeticException e) { System.out.println("CaughtException:"+e); } catch(NumberFormatException e) { System.out.println("CaughtException:"+e); }}} 11(b)Applet to display college information import java.awt.*; import java.applet.*; import java.lang.*; /* <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-------\n1.ECE \n2.EEE\n3.MCA "; t1=new TextArea(str); setLayout(new BorderLayout()); add(l1,BorderLayout.NORTH); add(t1,BorderLayout.CENTER); add(l2,BorderLayout.SOUTH); }} 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; System.out.println("Sum:"+c); } public void sub() { c=a-b; System.out.println("Difference:"+c); } public void mul() { c=a*b; System.out.println("Product:"+c); } public void div() { c=a/b; System.out.println("Division:"+c); } } Package Implementation import arith_pack.*; import java.io.*; class Arith

{ public static void main(String arg[]) throws IOException { DataInputStream n=new DataInputStream(System.in); int i,j; System.out.println("Enter First Value:"); i=Integer.parseInt(n.readLine()); System.out.println("Enter Second Value:"); j=Integer.parseInt(n.readLine()); Arithmetic ar=new Arithmetic(i,j); ar.add(); ar.sub(); ar.mul(); ar.div(); }} 13(b) Applet programto display arc on mouse click and display your name on mouse drag import java.awt.*; import java.awt.event.*; import java.applet.*; /* <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 = me.getX();; mouseY = me.getY(); 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 = me.getX(); mouseY = me.getY(); 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) g.drawString(msg, mouseX, mouseY); if(flag==0) g.drawArc(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 java.awt.*; import java.applet.*; import java.awt.event.*; /* <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); Fill.addActionListener(this); NoFill.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource()==Fill) { isFill="Y"; isNoFill="N"; repaint(); } if (e.getSource()==NoFill) { isFill="N"; isNoFill="Y"; repaint(); } } public void paint(Graphics gscreen) { gscreen.setColor(Color.red); if(isFill=="Y") { gscreen.fillRect(200,10,60,50); } else if (isNoFill=="Y") { gscreen.drawRect(200,10,60,50); } } }

18(a)Cash Register import java.io.*; import java.util.*; import java.lang.*; 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(System.in); System.out.println("\nThe Cash Register:"); for(j=1;j<=i;j++) { System.out.println(name[j]+":"+price[j]); } if(tot>200) { disc=tot*0.05; System.out.println("Discount:"+disc); discprice=tot-disc; System.out.println("Discounted price:"+discprice); } System.out.println("\nEnter the tendered amount:"); tendamt=Double.parseDouble(in.readLine()); System.out.println("Tender:"+tendamt); change=tendamt-discprice; System.out.println("Change:"+change); } void input()throws IOException { DataInputStream in=new DataInputStream(System.in); System.out.println("\nCASH REGISTER"); System.out.println("\nEnter item name and price"); String str; i=1; do { System.out.println("Enter the article"+i+":"); name[i]=in.readLine(); str=name[i].toString(); if(str.equals("Exit")) { name[i]=" "; display(); System.exit(0); } System.out.println("\nEnter the price of "+name[i]+":"); price[i]=Double.parseDouble(in.readLine()); tot=tot+price[i]; i=i+1; System.out.println(i); } while(!str.equals("Exit")); }

} class cash { public static void main(String args[])throws IOException { exam8 c=new exam8(); c.input(); c.display(); } } 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="LinesRectsOvals.class" height="250" width="400"> </applet> import java.applet.*; import java.awt.*; import javax.swing.*; public class LinesRectsOvals extends JApplet { public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.drawLine(5,30,350,30); g.setColor(Color.blue); g.drawRect(5,40,90,55); g.fillRect(100,40,90,55); g.setColor(Color.cyan); g.fillRoundRect(195,40,90,55,50,50); g.drawRoundRect(290,40,90,55,20,20); g.setColor(Color.yellow); g.draw3DRect(5,100,90,55,true); g.fill3DRect(100,100,90,55,false); g.setColor(Color.magenta); g.drawOval(195,100,90,55); g.fillOval(290,100,90,55); } }

You might also like