Sei sulla pagina 1di 39

DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS

III SEMESTER
(10MCA37) JAVA PROGRAMMING LABORATORY

LABORATORY MANUAL
ACADEMIC YEAR 20132014 (ODD SEMESTER)
Na m e US N Semester : ____________________________________________________ : ____________________________________________________ : ____________________________________________________

DEPARTMENT OF MCA

MVJCE

CONTENTS
Sl.No. 1 Name of the Experiment a. Write a JAVA Program to demonstrate Constructor Overloading and Method overloading. b. Write a JAVA Program to implement Inner class and demonstrate its Access Protections. a. Write a JAVA Program to demonstrate Inheritance. b. Write a JAVA Program to demonstrate Exception Handling (Using Nested try catch and finally). Write a JAVA program which has i. A Class called Account that creates account with 500Rs minimum balance, a deposit() method to deposit amount, a withdraw() method to withdraw amount and also throws LessBalanceException if an account holder tries to withdraw money which makes the balance become less than 500Rs. ii. A Class called LessBalanceException which returns the statement that says withdraw amount (___Rs) is not valid. iii. A Class which creates 2 accounts, both account deposit money and one account tries to withdraw more money which generates a LessBalanceException take appropriate action for the same. Write a JAVA program using Synchronized Threads, which demonstrates Producer Consumer concept. Write a JAVA program which has i. A Interface class for Stack Operations ii. A Class that implements the Stack Interface and creates a fixed length Stack. iii. A Class that implements the Stack Interface and creates a Dynamic length Stack. iv. A Class that uses both the above Stacks through Interface reference and does the Stack operations that demonstrates the runtime binding. Write a JAVA program which has i. 2 classes which initializes a String in its constructor ii. A Generic class with 2 type Parameters iii. Create a Generic Class reference for t he above 2 Class and try to print the message inside the constructor (Use to string method). Write JAVA programs which demonstrates utilities of LinkedList Class Write a JAVA Program which uses FileInputStream / FileOutPutStream Classes.

4 5

7 8

Java Programming Laboratory (10MCA37)

III Semester

DEPARTMENT OF MCA

MVJCE

9 10 11 12 13 14

Write a JAVA Program which writes a object to a file (use transient variable also). Write a JAVA program which uses Datagram Socket for Client Server Communication Write JAVA Applet programs which handles MouseEvent Write JAVA Applet programs which handles KeyBoardEvent Write a JAVA program which implements RMI Write a Swing Application which uses i. JTabbed Pane ii. Each Tab should use JPanel, which includes any one component given below in each Panel iii. ComboBox / List / Tree / Radiobutton

Java Programming Laboratory (10MCA37)

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 1(a) Write a Java Program to demonstrate Constructor Overloading and Method overloading Coding: import java.io.*; import java.lang.*; class A { int a; A() { a=0; } A(int x) { a=x; } void show(int x) { a=x; System.out.println("a="+a); } void show() { System.out.println("a="+a); } } class J1_1 { public static void main(String args[]) { A a1=new A(); A a2=new A(2); a1.show(5); a2.show(); } }

Output:a=5 a=2

Java Programming Laboratory (10MCA37)

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 1(b) Write a Java Program to implement inner class and demonstrate its Access Protections Coding import java.io.*; import java.lang.*; class outer { int a=10; class inner { int b=20; void display() { System.out.println("a = " +a + "b=" +b); } } void test() { inner in1=new inner(); in1.display(); System.out.println("a = " +a); } } class InnerDemo { public static void main(String args[]) { outer out1=new outer(); out1.test(); } }

Output : a = 10 b=20 a = 10

Java Programming Laboratory (10MCA37)

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 2(a) Write a Java Program to demonstrate Inheritance. Coding : class A { int a; void show_a() { System.out.println("a=" +a); } } class B extends A { int x; void show_x() { x=a; System.out.println("x=" +x); } } class InheritanceDemo { public static void main(String args[]) { B x1=new B(); x1.a=10; x1.show_a(); x1.show_x(); } }

Output : a=10 x=10

Java Programming Laboratory (10MCA37)

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 2(b) Write a Java Program to demonstrateException Handling (Using Nested try catch and finally) Coding:import java.io.*; class ExceptionHand { public static void main(String args[]) throws Exception { int arg=args.length; System.out.println("No of Argument : "+arg); try { int c=42/arg; System.out.println("c=" +c); try { int d[]={1,2,3}; d[arg]=5; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index error Exception"); } } catch(ArithmeticException e) { System.out.println("Division by zero Exception"); } finally { System.out.println("Default Exception"); } } } Output: Java ExceptionHand No of Argument : 0 Division by zero Exception Default Exception

Java Programming Laboratory (10MCA37)

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 3(a, b, c) Write a Java Program which has A class called Account that creates with 500 Rs minimum balance, a deposit() method to deposit amount, a withdraw() method to withdraw amount and also throws LessBalanceException if an account holder tries to withdraw money which makes the balance become less than 500Rs Write a Java Program which has a class called LessBalanceException which returns the statement that says withdraw amont(_Rs) is not valid Write a java program which has a class which creates 2 accounts, both account deposit money and one account tries to withdraw money which generates a LessBalanceException take appropriate action for the same

Coding:class LessBalenceException extends Exception { LessBalenceException(int x) { super(x+" is not valid"); } } class Account { int a=500; void withdrow(String s) { try { int i = Integer.parseInt(s); // NumberFormatException a=a-i; if(a<500) throw new LessBalenceException(i); // generate the new Exception. System.out.println("successfull transaction"); } catch(Exception ex) { System.out.println(" --> "+ex); System.exit(0); } } void deposit(String s) { int i = Integer.parseInt(s); // NumberFormatException a=a+i; }
Java Programming Laboratory (10MCA37) 8 III Semester

DEPARTMENT OF MCA

MVJCE

public static void main(String aa[]) { Account o = new Account(); try { o.deposit(aa[0]); // ArrayIndex o.withdrow(aa[1]); } catch(Exception ex) { System.out.println("Exception is --> "+ex); } } }

Output

java Account 1000 200 Successful Transaction

Java Programming Laboratory (10MCA37)

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 4 Write a Java Program using Synchronized Threads, which demonstrates Producer Consumer concept. class Q { int n; boolean valueSet=false; synchronized int get() { if(!valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("Interrupted Exception caught"); } System.out.println("Got:"+n); valueSet=false; notify(); return n; } synchronized void put(int n) { if(valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("Interrupted Exception caught"); } this.n=n; valueSet=true; System.out.println("Put:"+n); notify(); } } class Producer implements Runnable { Q q; Producer(Q q) {
Java Programming Laboratory (10MCA37) 10 III Semester

DEPARTMENT OF MCA

MVJCE

this.q=q; new Thread(this,"Producer").start(); } public void run() { int i=0; while(true) { q.put(i++); } } } class Consumer implements Runnable { Q q; Consumer(Q q) { this.q=q; new Thread(this,"Consumer").start(); } public void run() { while(true) { q.get(); } } } class ProdCons { public static void main(String[] args) { Q q=new Q(); new Producer(q); new Consumer(q); System.out.println("Press Control-c to stop"); } } Output: Put:1 Got:1 Put:2 Got:2 Put:3 Got:3 Put:4 Got:4
Java Programming Laboratory (10MCA37) 11 III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM : 5a Write a JAVA program which has : A Interface class for Stack Operations interface IntStack { void push(int item); int pop(); } class FixedStack implements IntStack { private int stck[]; private int tos; FixedStack(int size) { stck = new int[size]; tos= -1; } public void push(int item) { if(tos==stck.length-1) System.out.println("stack is full"); else stck[++tos] = item; } public int pop( ) { if(tos < 0) { System.out.println("Stack underflow"); return 0; } else return stck[tos --]; } } class IFTest { public static void main(String args[]) { FixedStack mystack1 = new FixedStack(5); FixedStack mystack2 = new FixedStack(8);
Java Programming Laboratory (10MCA37) 12 III Semester

DEPARTMENT OF MCA

MVJCE

for(int i=0; i<5; i++) mystack1.push(i); for(int i=0; i<8; i++) mystack2.push(i); System.out.println("Stack in mystack1"); for(int i=0;i<5;i++) System.out.println(mystack1.pop()); System.out.println("Stack in mystack2"); for(int i=0;i<8;i++) System.out.println(mystack2.pop()); } }

Output Stack in mystack1 43210 Stack in mystack2 76543210

Java Programming Laboratory (10MCA37)

13

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 5B : Write a JAVA program which has : A Class that implements the Stack Interface and creates a fixed length Stack. interface IntStack { void push(int item); int pop(); } class FixedStack implements IntStack { private int stck[]; private int tos; FixedStack(int size) { stck = new int[size]; tos= -1; } public void push(int item) { if(tos==stck.length-1) System.out.println("stack is full"); else stck[++tos] = item; } public int pop( ) { if(tos < 0) { System.out.println("Stack underflow"); return 0; } else return stck[tos --]; } } class IFTest { public static void main(String args[]) {

Java Programming Laboratory (10MCA37)

14

III Semester

DEPARTMENT OF MCA

MVJCE

FixedStack mystack1 = new FixedStack(5); FixedStack mystack2 = new FixedStack(8); for(int i=0; i<5; i++) mystack1.push(i); for(int i=0; i<8; i++) mystack2.push(i); System.out.println("Stack in mystack1"); for(int i=0;i<5;i++) System.out.println(mystack1.pop()); System.out.println("Stack in mystack2"); for(int i=0;i<8;i++) System.out.println(mystack2.pop()); } }

Output Stack in mystack1 43210 Stack in mystack2 76543210

Java Programming Laboratory (10MCA37)

15

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 5C : Write a JAVA program which has : A Class that implements the Stack Interface and creates a Dynamic length Stack. interface IntStack { void push(int item); int pop(); } class DynStack implements IntStack { private int stck[]; private int tos; DynStack(int size) { stck = new int[size]; tos= -1; } public void push(int item) { if(tos==stck.length-1) { int temp[]=new int[stck.length * 2]; for( int i=0; i<stck.length;i++) temp[i]= stck[i]; stck=temp; stck[++tos]= item; } else stck[++tos] = item; } public int pop( ) { if(tos < 0) { System.out.println("Stack underflow"); return 0; } else return stck[tos --]; } }

Java Programming Laboratory (10MCA37)

16

III Semester

DEPARTMENT OF MCA

MVJCE

class Dstack { public static void main(String args[]) { DynStack mystack1 = new DynStack(5); DynStack mystack2 = new DynStack(8); for(int i=0; i<12; i++) mystack1.push(i); for(int i=0; i<20; i++) mystack2.push(i); System.out.println("Stack in mystack1"); for(int i=0;i<12;i++) System.out.println(mystack1.pop()); System.out.println("Stack in mystack2"); for(int i=0;i<20;i++) System.out.println(mystack2.pop()); } }

Output Stack in mystack1 11 10 9 8 7 6 5 4 3 2 1 0 Stack in mystack2 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Java Programming Laboratory (10MCA37)

17

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 5D : Write a JAVA program which has : A Class that uses both the above Stacks through Interface reference and does the Stack operations that demonstrates the runtime binding. interface IntStack { void push(int item); int pop(); }

class Rstack { public static void main(String args[]) { IntStack mystack; DynStack ds = new DynStack(5); FixedStack fs = new FixedStack(8); mystack = ds; for(int i=0; i<12;i++) mystack=fs; for(int i=0; i<8;i++) mystack = ds; System.out.println(" Values in dynamic Stack "); for(int i=0; i<12; i++) System.out.println(mystack.pop()); mystack = fs; System.out.println(" Values in fixed Stack "); for(int i=0; i<8; i++) System.out.println(mystack.pop()); } }

Output Stack in mystack1 11 10 9 8 7 6 5 4 3 2 1 0 Stack in mystack2 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Java Programming Laboratory (10MCA37)

18

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM : 6A Write a JAVA program which has classes which initializes a String in its constructor Coding:import java.io.*; class MakeString { Public static void main(String args[]) { Char c[]= {J a, V, a} String s1 = new String(c); String s2 = new String(c); System.out.println(s1); System.out.println(s2); } }

Output Java Java

Java Programming Laboratory (10MCA37)

19

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 6B Write a JAVA program which has A Generic class with 2 type Parameters class TwoGen<T, V> { T ob1; V ob2; TwoGen(T o1, V o2) { ob1 = o1; ob2 = o2; } void showTypes() { System.out.println("Type of T is :" +ob1.getClass().getName()); System.out.println("Type of V is :" +ob2.getClass().getName()); } T getob1() { return ob1; } V getob2() { return ob2; } } class SimpGen { public static void main(String args[]) { TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics"); tgObj.showTypes(); int v = tgObj.getob1(); System.out.println("Values :" +v); String str = tgObj.getob2(); Sysem.out.println("Values :" +str); } } Output Type of T is java.lang.Integer Type of V is java.lang.String Value: 88 Value: Generics
Java Programming Laboratory (10MCA37) 20 III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 6C Write a JAVA program which has Create a Generic Class reference for t he above 2 Class and try to print the message inside the constructor (Use to string method). class TwoGen<T, V> { T ob1; V ob2; TwoGen(T o1, V o2) { ob1 = o1; ob2 = o2; } void showTypes() { System.out.println("Type of T is :" +ob1.to.String(getClass()).getName()); System.out.println("Type of V is :" +ob2.getClass().getName()); } T getob1() { return ob1; } V getob2() { return ob2; } } class SimpGen { public static void main(String args[]) { TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics"); tgObj.showTypes(); int v = tgObj.getob1(); System.out.println("Values :" +v); String str = tgObj.getob2(); Sysem.out.println("Values :" +str); } } Output Type of T is java.lang.Integer Type of V is java.lang.String Value: 88 Value: Generics
Java Programming Laboratory (10MCA37) 21 III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 7 Write JAVA programs which demonstrates utilities of LinkedList Class import java.util.*; class LinkedListDemo { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<String>(); ll.add("F"); ll.add("B"); ll.add("D"); ll.add("E"); ll.add("C"); ll.addLast("Z"); ll.addFirst("A"); ll.add( 1, "A2"); System.out.println("Original contents of ll" +ll); ll.remove("F"); ll.remove(2); System.out.println(" Contents of ll after deletion are " +ll); ll.removeFirst(); ll.removeLast(); System.out.println(" Contents of ll after deleting first and last are " +ll); String val = ll.get(2); ll.set(2, val + "Changed"); System.out.println(" Contents of ll after change are " +ll); } }

Output Original Contents of ll: [A, A2, f, B,D,E,C,Z] Contents of ll after deletion: [A, A2, D,E,C,Z] Contents of ll after deleting first and last : [A2, D,E,C] ll after change : [A2, D,E Changed, C]

Java Programming Laboratory (10MCA37)

22

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 8 Write a JAVA Program which uses FileInputStream / FileOutPutStream Classes. import java.io.*; class copyfile { public static void main(String args[])throws IOException { int i; FileInputStream fin; FileOutputStream fout; try { try { fin = new FileInputStream(args[0]); } catch(FileNotFoundException e) { System.out.println("Input File Not Found"); return; } try { fout = new FileOutputStream(args[1]); } catch(FileNotFoundException e) { System.out.println("Error opening Output file"); return; } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("CopyFile"); return; } try { do { i=fin.read(); if(i!= -1) fout.write(i); }while(i != -1);
Java Programming Laboratory (10MCA37) 23 III Semester

DEPARTMENT OF MCA

MVJCE

} catch(IOException e) { System.out.println("File error"); } fin.close(); fout.close(); } }

Output Java Copyfile samp.txt samp1.txt

Java Programming Laboratory (10MCA37)

24

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 9 Write a JAVA Program which writes a object to a file (use transient variable also). import java.io.*; public class SerializationDemo { public static void main(String args[]) { try { MyClass object1= new MyClass("hello", -7, 10, 2.7e10); System.out.println("Object1 :" +object1); FileOutputStream fos = new FileOutputStream("serial"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(object1); oos.flush(); oos.close(); } catch(IOException e) { System.out.println("Exception during Serialization" +e); System.exit(0); } try { MyClass object2; FileInputStream fis = new FileInputStream("serial"); ObjectInputStream ois = new ObjectInputStream(fis); object2 = (MyClass)ois.readObject(); ois.close(); System.out.println("Object2 = " + object2); } catch(Exception e) { System.out.println("Exception during deSerialization" +e); System.exit(0); } } } class MyClass implements Serializable { String s; int i; transient int p; double d; public MyClass(String s, int i, int p, double d)
Java Programming Laboratory (10MCA37) 25 III Semester

DEPARTMENT OF MCA

MVJCE

{ this.s=s; this.i=i; this.p=p; this.d=d; } public String toString() { return "s=" + s + "; i=" + i + "; d=" + d;

} }

Output Object 1: s=Hello i =-7 d=2.7E10 Object 2: s=Hello i =-7 d=2.7E10

Java Programming Laboratory (10MCA37)

26

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 10 Write a JAVA program which uses Datagram Socket for Client Server Communication Coding:Client.java import java.net.*; import java.io.*; public class FClient { public static void main(String args[]) throws IOException { Socket echoSocket = null; BufferedReader in = null; BufferedReader stdin=null; PrintWriter out= null; try { echoSocket = new Socket(InetAddress.getLocalHost(),95); in = new BufferedReader(new InputStreamReader (echoSocket.getInputStream())); stdin = new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(echoSocket.getOutputStream(),true); } catch(UnknownHostException e) { System.err.println("Don't know about host"); System.exit(1); } catch(IOException e) { System.err.println("Could not get IO for the connection"); System.exit(1); } System.out.println("Enter File Name : \n"); String Fname= stdin.readLine(); out.println(Fname); String userInput; while ((userInput = in.readLine()) != null) { System.out.println(userInput); }
Java Programming Laboratory (10MCA37) 27 III Semester

DEPARTMENT OF MCA

MVJCE

out.close(); stdin.close(); in.close(); } } Server.java import java.net.*; import java.io.*; public class FServer { public static void main(String args[]) throws IOException { ServerSocket serverSocket = null; Socket clientSocket = null; try { serverSocket = new ServerSocket(95); clientSocket = serverSocket.accept(); System.out.println("Connected to:" + clientSocket); } catch(IOException e) { System.err.println("Could not listen on port :95"); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true); BufferedReader stdin = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out.println("Hello Client.......\n\n"); String s=stdin.readLine(); File f= new File(s); BufferedReader d = new BufferedReader(new FileReader(s)); if (f.exists()) { String line; while((line=d.readLine())!=null) { out.write(line+"\n"); out.flush(); } d.close();
Java Programming Laboratory (10MCA37) 28 III Semester

DEPARTMENT OF MCA

MVJCE

} out.close(); clientSocket.close(); serverSocket.close(); } }

Output Procedure Open two dos prompt for the program execution. One prompt is for the client and another is for the Server. To compile the Client.java: Javac FClient.java javac FServer.java To Run Server: java FServer Leave the server prompt as it To Run Client: java FClient

Enter The File Name : file.txt Hello Client Hi.. Welcome to socket Programming. Thanks.

Java Programming Laboratory (10MCA37)

29

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 11 Write JAVA Applet programs which handles MouseEvent import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="Mouse" width=500 height=500> </applet> */ public class Mouse extends Applet implements MouseListener,MouseMotionListener { int X=0,Y=20; String msg="MouseEvents"; public void init() { addMouseListener(this); addMouseMotionListener(this); setBackground(Color.black); setForeground(Color.red); } public void mouseEntered(MouseEvent m) { setBackground(Color.magenta); showStatus("Mouse Entered"); repaint(); } public void mouseExited(MouseEvent m) { setBackground(Color.black); showStatus("Mouse Exited"); repaint(); } public void mousePressed(MouseEvent m) { X=10; Y=20; msg="NEC"; setBackground(Color.green); repaint(); } public void mouseReleased(MouseEvent m) { X=10; Y=20; msg="Engineering";
Java Programming Laboratory (10MCA37) 30 III Semester

DEPARTMENT OF MCA

MVJCE

setBackground(Color.blue); repaint(); } public void mouseMoved(MouseEvent m) { X=m.getX(); Y=m.getY(); msg="College"; setBackground(Color.white); showStatus("Mouse Moved"); repaint(); } public void mouseDragged(MouseEvent m) { msg="CSE"; setBackground(Color.yellow); showStatus("Mouse Moved"+m.getX()+" "+m.getY()); repaint(); } public void mouseClicked(MouseEvent m) { msg="Students"; setBackground(Color.pink); showStatus("Mouse Clicked"); repaint(); } public void paint(Graphics g) { g.drawString(msg,X,Y); } } OUTPUT

Java Programming Laboratory (10MCA37)

31

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 12 Write JAVA Applet programs which handles KeyBoardEvent import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="Key" width=300 height=400> </applet> */ public class Key extends Applet implements KeyListener { int X=20,Y=30; String msg="KeyEvents--->"; public void init() { addKeyListener(this); requestFocus(); setBackground(Color.green); setForeground(Color.blue); } public void keyPressed(KeyEvent k) { showStatus("KeyDown"); int key=k.getKeyCode(); switch(key) { case KeyEvent.VK_UP: showStatus("Move to Up"); break; case KeyEvent.VK_DOWN: showStatus("Move to Down"); break; case KeyEvent.VK_LEFT: showStatus("Move to Left"); break; case KeyEvent.VK_RIGHT: showStatus("Move to Right"); break; } repaint(); } public void keyReleased(KeyEvent k) { showStatus("Key Up"); }
Java Programming Laboratory (10MCA37) 32 III Semester

DEPARTMENT OF MCA

MVJCE

public void keyTyped(KeyEvent k) { msg+=k.getKeyChar(); repaint(); } public void paint(Graphics g) { g.drawString(msg,X,Y); } }

OUTPUT

Java Programming Laboratory (10MCA37)

33

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 13 Write a JAVA program which implements RMI Coding:AddServerIntf.java import java.rmi.*; public interface AddServerIntf extends Remote { double add(double d1, double d2) throws RemoteException; } AddServerImpl.java import java.rmi.*; import java.rmi.server.*; public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf { public AddServerImpl() throws RemoteException { } public double add(double d1, double d2) throws RemoteException { return d1 + d2; } } AddServer.java import java.net.*; import java.rmi.*; public class AddServer { public static void main(String args[]) { try { AddServerImpl addServerImpl = new AddServerImpl(); Naming.rebind("AddServer", addServerImpl); } catch(Exception e) { System.out.println("Exception: " + e); } } }

Java Programming Laboratory (10MCA37)

34

III Semester

DEPARTMENT OF MCA

MVJCE

AddClient.java import java.rmi.*; public class AddClient { public static void main(String args[]) { try { String addServerURL = "rmi://" + args[0] + "/AddServer"; AddServerIntf addServerIntf =(AddServerIntf)Naming.lookup(addServerURL); System.out.println("The first number is: " + args[1]); double d1 = Double.valueOf(args[1]).doubleValue(); System.out.println("The second number is: " + args[2]); double d2 = Double.valueOf(args[2]).doubleValue(); System.out.println("The sum is: " + addServerIntf.add(d1, d2)); } catch(Exception e) { System.out.println("Exception: " + e); } } }

Java Programming Laboratory (10MCA37)

35

III Semester

DEPARTMENT OF MCA

MVJCE

Output Procedure First compile all the 4 source files mentioned above. Then open two prompts one is for client and another is for server. Now on the server prompt execute following command rmic AddServerImpl (rmic ( rmi Compiler) Now execute following command on the server prompt which will start the RMI Registry on the Server Machine. start rmiregistry Now on the server prompt execute this command also java AddServer

AddClient 192.168.1.2 10 15 The first number is: 10 The second number is: 15 The sum is: 25.0

Java Programming Laboratory (10MCA37)

36

III Semester

DEPARTMENT OF MCA

MVJCE

PROGRAM 14 Write a Swing Application which uses i. JTabbed Pane ii. Each Tab should use JPanel, which includes any one component given below in each Panel iii. ComboBox / List / Tree / Radiobutton import javax.swing.*; import java.awt.event.*; import java.awt.*; import javax.swing.tree.*; import javax.swing.event.*; /* <applet code = "JTabbedPaneDemo" width=400 height=100> </applet> */ public class JTabbedPaneDemo extends JApplet { public void init() { try { SwingUtilities.invokeAndWait( new Runnable () { public void run () { makeGUI(); } } ); } catch(Exception exc) { System.out.println("Cant create because of " + exc); } } private void makeGUI() { JTabbedPane jtp = new JTabbedPane(); jtp.addTab("Colors", new ColorsPanel () ); jtp.addTab("Flavors", new FlavorsPanel () ); jtp.addTab("Cities", new CitiesPanel () ); jtp.addTab("Courses", new CoursesPanel () ); add(jtp); }
Java Programming Laboratory (10MCA37) 37 III Semester

DEPARTMENT OF MCA

MVJCE

} class FlavorsPanel extends JPanel { public FlavorsPanel() { JComboBox jcb = new JComboBox(); jcb.addItem("Vanila"); jcb.addItem("chocolate"); jcb.addItem("strawberry"); add(jcb); } } class ColorsPanel extends JPanel { public ColorsPanel() { JRadioButton jrb = new JRadioButton("RED"); add(jrb); JRadioButton jrb1 = new JRadioButton("Blue"); add(jrb1); JRadioButton jrb2 = new JRadioButton("Green"); add(jrb2); ButtonGroup bg = new ButtonGroup(); bg.add(jrb); bg.add(jrb1); bg.add(jrb2); } } class CitiesPanel extends JPanel { public CitiesPanel() { JTree tree; DefaultMutableTreeNode top = new DefaultMutableTreeNode("options"); DefaultMutableTreeNode a = new DefaultMutableTreeNode("India"); top.add(a); DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("Delhi"); a.add(a1); DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("Bangalore"); a.add(a2); DefaultMutableTreeNode b = new DefaultMutableTreeNode("America"); top.add(b); DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("Texas"); b.add(b1);
Java Programming Laboratory (10MCA37) 38 III Semester

DEPARTMENT OF MCA

MVJCE

DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("Chicago"); b.add(b2); tree = new JTree(top); JScrollPane jsp = new JScrollPane(tree); add(jsp); } } class CoursesPanel extends JPanel { public CoursesPanel() { JList jlist; String Courses[] = {"MCA", "MBA", "BE", "BSc", "MSc", "Bcom","Mcom" }; jlist= new JList(Courses); jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane jscrlp; jscrlp = new JScrollPane(jlist); jscrlp.setPreferredSize(new Dimension(120,90)); add(jscrlp); } }

OUTPUT

Java Programming Laboratory (10MCA37)

39

III Semester

Potrebbero piacerti anche