[Link].
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) { [Link](a+b= : +(a+b)); } void show(int a,double b) { [Link](a+b : +(a+b)); } void show(double a,double b) { [Link](a+b : +(a+b)); } } class lab1a { public static void main(String args[]) { int area; ovr ob1=new ovr(); area=[Link]();
Mail: [Link]@[Link]
[Link]
[Link](area : +area); ovr ob2=new ovr(5,6); area=[Link](); [Link](area : +area); ovr ob3=new ovr(10); area=[Link](); [Link](area : +area); [Link](12,23); [Link](4.5,7.8); [Link](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(); [Link](); } class inner { void cal() { for(i=0;i<10;i++)
Mail: [Link]@[Link]
[Link]
sum+=a[i]; [Link]("Array Elements"); for(i=0;i<10;i++) [Link](" " +a[i]); [Link]("Sum :" +sum); } } } class lab1b { public static void main(String args[]) { outer ob1=new outer(); [Link](); [Link](); } } 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() { [Link](In Super Class); [Link](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: [Link]@[Link]
[Link]
void show() { [Link](In Sub class); [Link](c : +ans); [Link](); add=a+b+ans; [Link](addition of a,b and ans is :+add); } } class lab2a { public static void main(String args[]) { sub ob=new sub(4,5,6); [Link](); } } 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=[Link]; int b=32/a; [Link](a : +a); try { if(a==1) a=a/(a-a); if(a==2) { int c[]={1}; c[10]=90; } } catch(ArrayIndexOutOfBoundsException e)
Mail: [Link]@[Link]
[Link]
{ [Link](Array Index out of bounds +e); } } catch(ArithmeticException e) { [Link](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: [Link]@[Link]
[Link]
{ int tot; tot=s1+s2+s3; [Link] (Name: +name); [Link](Rno : +rno); [Link] (Sub1: +s1 +Sub2 +s2 + Sub3 : +s3); [Link] (total: +tot); if (tot40 && tot<60) [Link]("grade First CLass"); else [Link]("grade Distinction"); } } class lab3a { public static void main(String args[]) { test ob=new test(12,"Guru","mca",50,60,70); [Link](); } } 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=[Link](); [Link](The thread one name and priority:+t); for(int i=1;i<5;i++) { System .[Link]("thread done:"+i); [Link](500); } }catch(InterruptedException e)
Mail: [Link]@[Link]
[Link]
{ [Link]("Interrupted"); } [Link]("Exiting thread is done"); } } class lab3b { public static void main(String args[]) { Thread t=[Link](); [Link]("current thread details are:"+t); [Link]("mythread"); [Link](4); [Link]("The change in thread name and priority:"+t); [Link]("The priority is="+[Link]()); [Link]("The name is:"+[Link]()); try { [Link](3000); }catch(InterruptedException e) { [Link]("Main"); } A ThreadA=new A(); [Link]("Thread one"); [Link](6); [Link]("Start the thread one"); [Link](); [Link]("Enter main thread"); } } OUTPUT: E:\4th sem\JAVA lab>javac [Link] 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: [Link]@[Link]
[Link]
thread done:4 Exiting thread is done 4a:Write a Java program to create a scrolling text using java applets import [Link].*; import [Link].*; /* */ public class banner extends Applet implements Runnable { String msg=PES-Institute of technology; Thread t=null; int state; boolean flag; public void init() { setBackground([Link]); } public void start() { t=new Thread(this); flag=false; [Link](); } public void run() { char ch; for(; ; ) { try { repaint(); [Link](250); ch=[Link](0); msg=[Link](1,[Link]()); msg+=ch; if(flag) break; } catch(InterruptedException e){} } } public void stop() { flag=true; t=null;
Mail: [Link]@[Link]
[Link]
} public void paint(Graphics g) { [Link](msg,50,60); } } //Output is displayed in separate window 4b: Write a Java program to pass parameters to applets and display the same. import [Link].*; import [Link].*; /* */ 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=[Link](s3); t2=[Link](s4); t3=[Link](s5); setBackground([Link]); } public void paint(Graphics g) { if(t1<40||t2<40||t3<40) res="fail"; else res="pass"; [Link](s1,20,30); [Link](s2,50,30); [Link](s3,40,45); [Link](s4,50,56); [Link](s5,60,65); [Link](res,80,90); } }
Mail: [Link]@[Link]
[Link]
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 [Link].*; import [Link].*; public class client { public static void main(String args[])throws IOException { BufferedReader in=null; OutputStream os; Socket cs=null; try { cs=new Socket([Link](),555); in=new BufferedReader(new InputStreamReader([Link]())); } catch(Exception e) { [Link](do not know about the host); [Link](1); } BufferedReader stdin=new BufferedReader(new InputStreamReader([Link])); [Link](enter the file to be opened); String s=[Link](); os=[Link](); byte bt[]=[Link](); [Link](bt); String UserInput; try { [Link]([Link]()); while((UserInput=[Link]())!=null) { [Link](); [Link](UserInput); } } catch(Exception e) { [Link](Socket Exception); [Link](1);
Mail: [Link]@[Link]
[Link]
} [Link](); [Link](); } } //server import [Link].*; import [Link].*; 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) { [Link](could not listen); [Link](1); } Socket cs=null; try { cs=[Link](); is=[Link](); [Link](bt); inputline=new String(bt); [Link](connected to ..+[Link]()); [Link](inputline); } catch(IOException io) { [Link](Accept failed); [Link](1); } PrintWriter out=new PrintWriter([Link](),true); BufferedReader Stdin=new BufferedReader(new InputStreamReader([Link])); File f=new File(inputline); if([Link]()) { BufferedReader d=new BufferedReader(new FileReader(inputline));
Mail: [Link]@[Link]
[Link]
String line; while((line=[Link]())!=null) { if([Link]()) { while((line=[Link]())!=null) { [Link](line); [Link](); } [Link](); } [Link](); [Link](); } } } } OUTPUT:C:\Java\jdk1.5.0_04\bin>javac [Link] C:\Java\jdk1.5.0_04\bin>javac [Link] C:\Java\jdk1.5.0_04\bin>java server connected to ..Socket[addr=/[Link],port=1132,localport=555] [Link] Exception in thread main [Link]: Stream closed at [Link]([Link]) at [Link]([Link]) at [Link]([Link]) at [Link]([Link]) C:\Java\jdk1.5.0_04\bin>java client enter the file to be opened [Link] {double width,height,depth;}class boxdemo{public static void main(String args[]) {double vol;box mybox= new box();[Link]=10;[Link]=20;[Link]=30;S [Link](hiiiiiiiiii\n);vol=[Link]*[Link]*[Link];Syst [Link](the volume is :+vol);}}
Mail: [Link]@[Link]