Sie sind auf Seite 1von 12

emonpatel.wordpress.

com

JAVA LAB PROGRAMS 1.a. Write a Java program to demonstrate constructor overloading and method overloading. class ovr { int l,b; ovr() { l=-1; b=-1; } ovr(int x,int y) { l=x; b=y; } ovr(int x) { l=x; b=x; } int ar() { return(l*b); } void show(int a,int b) { System.out.println(a+b= : +(a+b)); } void show(int a,double b) { System.out.println(a+b : +(a+b)); } void show(double a,double b) { System.out.println(a+b : +(a+b)); } } class lab1a { public static void main(String args[]) { int area; ovr ob1=new ovr(); area=ob1.ar();

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

System.out.println(area : +area); ovr ob2=new ovr(5,6); area=ob2.ar(); System.out.println(area : +area); ovr ob3=new ovr(10); area=ob3.ar(); System.out.println(area : +area); ob1.show(12,23); ob1.show(4.5,7.8); ob1.show(6.7,9.0); } } OUTPUT: Area 1 Area 30 Area 100 a+b=35 a+b=12.3 a+b=15.7

1.b. Write a Java program to implement Inner class and demonstrate its access protections. class outer { int a[]=new int[10]; int sum=0,i,k=0; void init() { for(i=0;i<10;i++) { a[i]=k; k++; } } void show() { inner ob=new inner(); ob.cal(); } class inner { void cal() { for(i=0;i<10;i++)

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

sum+=a[i]; System.out.println("Array Elements"); for(i=0;i<10;i++) System.out.print(" " +a[i]); System.out.println("Sum :" +sum); } } } class lab1b { public static void main(String args[]) { outer ob1=new outer(); ob1.init(); ob1.show(); } } OUTPUT: 0123456789 Sum = 45 2a: Write a Java program to implement Inheritance. class outer { int a,b; outer(int x,int y) { a=x; b=y; } void show() { System.out.println(In Super Class); System.out.println(a and b are : +a+ +b); } } class sub extends outer { int ans; int add; sub(int a,int b,int c) { super(a,b); ans=c; }

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

void show() { System.out.println(In Sub class); System.out.println(c : +ans); super.show(); add=a+b+ans; System.out.println(addition of a,b and ans is :+add); } } class lab2a { public static void main(String args[]) { sub ob=new sub(4,5,6); ob.show(); } } OUTPUT: In Sub class c=6 In super class a=4 and b=5 Addition of a, b and c= 15 2b:Write a Java program to implement Exceptional handling (using nested try and catch) class error { public static void main(String args[]) { try { int a=args.length; int b=32/a; System.out.println(a : +a); try { if(a==1) a=a/(a-a); if(a==2) { int c[]={1}; c[10]=90; } } catch(ArrayIndexOutOfBoundsException e)

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

{ System.out.println(Array Index out of bounds +e); } } catch(ArithmeticException e) { System.out.println(Divide by zero : +e); } } } OUTPUT: C :\> java error Divide by zero C:\>java error 1 2 Array index out of bounds 3a:Write a Java program to create a interface and implement it in a class. interface grade { final int fail=40; final int fist=60; final int dist=75; void show(); } class cinfo { int rno; String name,course; cinfo(int x,String y,String z) { rno=x; name=y; course=z; } } class test extends cinfo implements grade { int s1,s2,s3; test(int a,String b,String c,int d,int e,int f) { super(a,b,c); s1=d; s2=e; s3=f; } public void show()

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

{ int tot; tot=s1+s2+s3; System.out.println (Name: +name); System.out.println(Rno : +rno); System.out.println (Sub1: +s1 +Sub2 +s2 + Sub3 : +s3); System.out.println (total: +tot); if (tot40 && tot<60) System.out.println("grade First CLass"); else System.out.println("grade Distinction"); } } class lab3a { public static void main(String args[]) { test ob=new test(12,"Guru","mca",50,60,70); ob.show(); } } OUTPUT: Name:Guru Rollno: 12 Subject1:50 Subject2:60 Subject3:70 Total: 180 Grade: distinction 3.b. Write a JAVA program to create a class(extending Thread) and use methods Thread class to change name,priority,of the current Thread and display the name. class A extends Thread { public void run() { try { Thread t=Thread.currentThread(); System.out.println(The thread one name and priority:+t); for(int i=1;i<5;i++) { System .out.println("thread done:"+i); Thread.sleep(500); } }catch(InterruptedException e)

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

{ System.out.println("Interrupted"); } System.out.println("Exiting thread is done"); } } class lab3b { public static void main(String args[]) { Thread t=Thread.currentThread(); System.out.println("current thread details are:"+t); t.setName("mythread"); t.setPriority(4); System.out.println("The change in thread name and priority:"+t); System.out.println("The priority is="+t.getPriority()); System.out.println("The name is:"+t.getName()); try { Thread.sleep(3000); }catch(InterruptedException e) { System.out.println("Main"); } A ThreadA=new A(); ThreadA.setName("Thread one"); ThreadA.setPriority(6); System.out.println("Start the thread one"); ThreadA.start(); System.out.println("Enter main thread"); } } OUTPUT: E:\4th sem\JAVA lab>javac lab3b.java E:\4th sem\JAVA lab>java lab3b current thread details are:Thread[main,5,main] The change in thread name and priority is:Thread[mythread,4,main] The priority is=4 The name is:mythread Start the thread one Enter main thread The thread one name and priority is:Thread[Thread one,6,main] thread done:1 thread done:2 thread done:3

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

thread done:4 Exiting thread is done 4a:Write a Java program to create a scrolling text using java applets import java.awt.*; import java.applet.*; /* */ public class banner extends Applet implements Runnable { String msg=PES-Institute of technology; Thread t=null; int state; boolean flag; public void init() { setBackground(Color.green); } public void start() { t=new Thread(this); flag=false; t.start(); } public void run() { char ch; for(; ; ) { try { repaint(); Thread.sleep(250); ch=msg.charAt(0); msg=msg.substring(1,msg.length()); msg+=ch; if(flag) break; } catch(InterruptedException e){} } } public void stop() { flag=true; t=null;

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

} public void paint(Graphics g) { g.drawString(msg,50,60); } } //Output is displayed in separate window 4b: Write a Java program to pass parameters to applets and display the same. import java.awt.*; import java.applet.*; /* */ public class stdpar extends Applet { String s1,s2,s3,s4,s5,res; int t1,t2,t3; public void init() { s1=getParameter(name); s2=getParameter(rollno); s3=getParameter(sub1); s4=getParameter(sub2); s5=getParameter(sub3); t1=Integer.parseInt(s3); t2=Integer.parseInt(s4); t3=Integer.parseInt(s5); setBackground(Color.green); } public void paint(Graphics g) { if(t1<40||t2<40||t3<40) res="fail"; else res="pass"; g.drawString(s1,20,30); g.drawString(s2,50,30); g.drawString(s3,40,45); g.drawString(s4,50,56); g.drawString(s5,60,65); g.drawString(res,80,90); } }

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

6: Write a JAVA Program to implement Client Server (Client requests a file, Server responds to client with contents of that file which is then display on the screen by Client Socket Programming). //client import java.net.*; import java.io.*; public class client { public static void main(String args[])throws IOException { BufferedReader in=null; OutputStream os; Socket cs=null; try { cs=new Socket(InetAddress.getLocalHost(),555); in=new BufferedReader(new InputStreamReader(cs.getInputStream())); } catch(Exception e) { System.out.println(do not know about the host); System.exit(1); } BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); System.out.println(enter the file to be opened); String s=stdin.readLine(); os=cs.getOutputStream(); byte bt[]=s.getBytes(); os.write(bt); String UserInput; try { System.out.println(in.readLine()); while((UserInput=in.readLine())!=null) { System.out.println(); System.out.println(UserInput); } } catch(Exception e) { System.err.println(Socket Exception); System.exit(1);

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

} in.close(); cs.close(); } } //server import java.net.*; import java.io.*; public class server { public static void main(String args[])throws IOException { ServerSocket ss=null; String inputline,outputline; inputline=new String(); InputStream is; byte bt[]=new byte[20]; try{ ss=new ServerSocket(555); } catch(IOException io) { System.out.println(could not listen); System.exit(1); } Socket cs=null; try { cs=ss.accept(); is=cs.getInputStream(); is.read(bt); inputline=new String(bt); System.out.println(connected to ..+cs.toString()); System.out.println(inputline); } catch(IOException io) { System.out.println(Accept failed); System.exit(1); } PrintWriter out=new PrintWriter(cs.getOutputStream(),true); BufferedReader Stdin=new BufferedReader(new InputStreamReader(System.in)); File f=new File(inputline); if(f.exists()) { BufferedReader d=new BufferedReader(new FileReader(inputline));

Mail: imran.patel@e-morphus.com

emonpatel.wordpress.com

String line; while((line=d.readLine())!=null) { if(f.exists()) { while((line=d.readLine())!=null) { out.write(line); out.flush(); } d.close(); } cs.close(); ss.close(); } } } } OUTPUT:C:\Java\jdk1.5.0_04\bin>javac server.java C:\Java\jdk1.5.0_04\bin>javac client.java C:\Java\jdk1.5.0_04\bin>java server connected to ..Socket[addr=/192.168.10.164,port=1132,localport=555] boxdemo.java Exception in thread main java.io.IOException: Stream closed at java.io.BufferedReader.ensureOpen(BufferedReader.java:97) at java.io.BufferedReader.readLine(BufferedReader.java:293) at java.io.BufferedReader.readLine(BufferedReader.java:362) at server.main(server.java:43) C:\Java\jdk1.5.0_04\bin>java client enter the file to be opened boxdemo.java {double width,height,depth;}class boxdemo{public static void main(String args[]) {double vol;box mybox= new box();mybox.width=10;mybox.height=20;mybox.depth=30;S ystem.out.println(hiiiiiiiiii\n);vol=mybox.width*mybox.height*mybox.depth;Syst em.out.println(the volume is :+vol);}}

Mail: imran.patel@e-morphus.com

Das könnte Ihnen auch gefallen