Java Lab Programs
1 a Recursive Fibonacci import [Link].*; public class Rfibonacci { public static void main(String args[])throws Exception { int f=0; [Link]("Enter n value:"); BufferedReader b=new BufferedReader(new InputStreamReader([Link])); int m; m=[Link] ([Link]()); [Link]("fib(0)="+f); for(int i=1;i<=m;i++) { f=fibcal(i); [Link]("Fib("+i+")="+f); } } public static int fibcal(int n) { if(n<=2) return 1; else return fibcal(n-1)+fibcal(n-2); } }
04 2012
Java Lab Programs
1 b NonRecursive Fiboncci import [Link].*; public class NRfibonacci { public static void main(String args[])throws Exception { int a,b,c; [Link]("enter n value:"); BufferedReader d=new BufferedReader(new InputStreamReader([Link])); int n1=[Link] ([Link]()); a=0 ;b=1; c=1; [Link]("Fib(0):"+a); [Link]("Fib(0):"+b); for(int i=1;i<n1;i++) { c=a+b; [Link]("Fib("+i+"):"+c); a=b; a2=a3; } } }
04 2012
Java Lab Programs
2 a Prime numbers import [Link]; class prime { public static void main(String arg[]) { int n,p,j; Scanner s=new Scanner([Link]); [Link]("Enter upto which no. prime nos. are needed:"); n=[Link](); for(int i=2;i<n;i++) { p=0; for(j=2;j<i;j++) { if(i%j==0) p=1; } if(p==0) [Link](i); } } }
04 2012
Java Lab Programs
2 b Matrix Multiplication import [Link].*; class matrix { public static void main (String arg[]) throws IOException { DataInputStream dis= new DataInputStream([Link]); int r1,r2,c1,c2,i,j,k; [Link]("Enter the Row Size of First Matrix:"); r1 = [Link]([Link]()); [Link]("Enter the Coloumn Size of First Matrix:"); c1 = [Link]([Link]()); [Link]("Enter the Row Size of Second Matrix:"); r2 = [Link]([Link]()); [Link]("Enter the Coloumn Size of Second Matrix:"); c2 = [Link]([Link]()); int a[][]=new int[r1][r2]; int b[][]=new int[c1][c2]; int c[][]=new int[r1][c2]; [Link]("Enter the elements of the First matrix:"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) a[i][j]=[Link]([Link]()); [Link]("Enter the elements of the Second matrix:"); for(i=0;i<r2;i++) for(j=0;j<c2;j++) b[i][j]=[Link]([Link]()); if(c1!=r2) [Link]("Multiplication not possible"); else { for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; . 04 2012
Java Lab Programs
for(k=0;k<r1;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { [Link](c[i][j]+""); } [Link](" "); } } }
04 2012
Java Lab Programs
2 c String Tokenizer Class import [Link].*; import [Link]; class Token { public static void main(String args[]) throws Exception { BufferedReader b = new BufferedReader(new InputStreamReader([Link])); String in= [Link](); StringTokenizer st= new StringTokenizer(in); while([Link]()) { String t=[Link](); [Link](t); } } }
04 2012
Java Lab Programs
3 a Inheritance class Dimensions { int length; int breadth; } class Rectangle extends Dimensions { int a; void area() { a=length*breadth; } } class Area { public static void main(String args[]) { Rectangle Rect= new Rectangle(); [Link]=7; [Link]=16; [Link](); [Link]("Area of Rectangle of length:"+[Link]+"and breadth:"+[Link]+"is:"+Rect.a); } }
04 2012
Java Lab Programs
3 b String Class & String Buffer Class import [Link].*; class stringBuffer { public static void main (String args[]) throws Exception { BufferedReader b= new BufferedReader(new InputStreamReader([Link])); String str; try { [Link]("Enter Your Name:"); str=[Link](); str+="This is the Example of String Buffer Class and its Function"; //Create a object of String Buffer Class StringBuffer strbuf= new StringBuffer(); [Link](str); [Link](strbuf); [Link](0,[Link]()); //append() [Link]("Hello"); [Link]("World"); //Print Hello World [Link](strbuf); //insert() [Link](5,"_Java"); //Print Hello_Java World [Link](strbuf);
//reverse() [Link](); [Link]("Reverse String:"); [Link](strbuf); //Print dlroW avaJ_olleH [Link](); [Link](strbuf); //Print Hello_Java World //set charAt() [Link](5,' '); [Link](strbuf); //Print Hello Java World
04 2012
Java Lab Programs
//charAt() [Link]("Character At 6th Position is:"); [Link]([Link](6)); // Print J //substring() [Link]("substring from 3 to 6:"); [Link]([Link](3,7)); //Print 1oJ //delete chatAt() [Link](3); [Link](strbuf); //Print Helo Java World //capacity() [Link]("capacity of the String buffer object is:"); [Link]([Link]()); //print 21 //delete() and length() [Link](6,[Link]()); [Link](strbuf); //Print nothing } catch(StringIndexOutOfBoundsException e) { [Link]([Link]()); } } }
04 2012
Java Lab Programs
4 a Vector Class import [Link].*; public class VectorDemo { public static void main(String args[]) { Vector v= new Vector(); int i=10; Integer j=new Integer(20); String s="Welcome"; [Link](i); [Link](j); [Link](s); [Link]("The Element of the Vector:"+v); [Link]("The Size of the Vector are:"+[Link]()); [Link]("The Elements at the position 2 is:"+[Link](2)); [Link]("The First Element of Vector is"+[Link]()); [Link]("The Last Element of Vector is"+[Link]()); [Link](2); Enumeration e=[Link](); [Link]("The Element of Vector:"+v); while([Link]()) { [Link]("The Elements are:"+[Link]()); } } }
04 2012
10
Java Lab Programs
4 b Stack Class import [Link].*; public class StackDemo { public static void main(String args[]) { Stack s=new Stack(); [Link](new Integer(10)); [Link]("a"); [Link]("The contents of the Stack is:"+s); [Link]("The Size of the Stack is:"+[Link]()); [Link]("The No. Popped out is:"+[Link]()); [Link]("The Content of the Stack is:"+s); [Link]("The Size of the Stack is:"+[Link]()); } }
04 2012
11
Java Lab Programs
5 a Applet Program import [Link].*; import [Link].*; public class App extends Applet { public void paint (Graphics g) { [Link]("Welcome to applet",100,100); } } <HTML> <APPLET CODE=[Link] WIDTH=500 HEIGHT=500> </APPLET> </HTML>
04 2012
12
Java Lab Programs
5 b TextField and Label Applet import [Link].*; import [Link].*; import [Link].*; public class De extends Applet implements ActionListener { TextField d; TextField k; Button b1; Button b2; public void init() { d=new TextField(10); add(d); k=new TextField(10); add(k); b1=new Button("WELCOME"); add(b1); b2=new Button("JAVA"); add(b2); [Link](this); [Link](this); } public void actionPerformed(ActionEvent evt) { if ([Link]() == b1) [Link]("WELCOME"); else if ([Link]() == b2) [Link]("JAVA"); } } <html> <applet code=[Link] width=500 height=500> </applet> </html> . 04 2012
13
Java Lab Programs
6 Exceptions class exception { public static void main(String args[]) { int a[]={1,2,3}; int i=0,j=0,k=0; try { for(i=0;i<4;i++) { [Link](a[i]); [Link](); } } catch(Exception e) { [Link]("Exception:"+e); } try { for(i=0;i<3;i++) { k=a[i]/j; } } catch(Exception e1) { [Link]("Exception e1:"+e1); } } }
04 2012
14
Java Lab Programs
7 a Multiple Threading class A extends Thread { public void run() { try { for(int i=1;i<=5;i++) { [Link]("From Thread:"+i); [Link](1000); } } catch(Exception e) { } } } class ThreadTest { public static void main(String args[]) { A a=new A(); [Link](); } }
04 2012
15
Java Lab Programs
7 b Inter-Thread Communication class Q { int n; boolean valueSet=false; synchronized int get() { if(!valueSet) try { wait(); } catch(InterruptedException e) { [Link]("Interrupted Exception caught"); } [Link]("GOT:"+n); valueSet=false; notify(); return n; } synchronized void put(int n) { if(valueSet) try { wait(); } catch(InterruptedException e) { [Link]("Interrupted Exception caught"); } this.n=n; valueSet=true; [Link]("put:"+n); notify(); . 04 2012
16
Java Lab Programs
} } class producer implements Runnable { Q q; producer(Q q) { this.q=q; new Thread(this,"producer").start(); } public void run() { int i=0; while(true) { [Link](i++); } } } class consumer implements Runnable { Q q; consumer(Q q) { this.q=q; new Thread(this,"consumer").start(); } public void run() { while(true) { [Link](); } } } class PcFixed . 04 2012
17
Java Lab Programs
{ public static void main(String args[]) { Q q=new Q(); new producer(q); new consumer(q); [Link]("press control-c to stop"); } }
04 2012
18
Java Lab Programs
8 a Packages package MyPack; class Balance { string name; double balance; Balance(strint n,double b) { name=n; bal=b; } void show() { if(bal<0) [Link]("--->"); [Link](name+":$"+bal); } } class AccountBalance { public static void main(String args[]) { Balance current[]=new Balance[3]; current[0]=new Balanace("KJ",123.23) current[0]=new Balanace("KJ",123.23) current[0]=new Balanace("KJ",123.23) for(int i=0;i<3;i++) { current[i].show(); } } }
04 2012
19
Java Lab Programs
8 b Interfaces interface area { float compute(float x,float y); } class rectangle implements area { public float compute(float x,float y) { return(x*y); } } class triangle implements area { public float compute(float x,float y) { return(x*y*(1/2)); } } class Interface { public static void main(String args[]) { rectangle rect = new rectangle(); triangle tri = new triangle(); area a; a= rect; [Link]("Area of the Rectangle is :" +[Link](10,20)); [Link]("Area of the Triangle is :" +[Link](10,6)); } }
04 2012
20
Java Lab Programs
9 Abstract Classes abstract class Figure { double d; double w; Figure(double a,double b) { d=a; w=b; } abstract double area(); } class rectangle extends Figure { rectangle(double a,double b) { super(a,b); } double area() { [Link]("Inside Area of Rectangle:"); return w*d; } } class triangle extends Figure { triangle(double a,double b) { super(a,b); } double area() { [Link]("Inside Area of Triangle:"); return w*d*1/2; } }
04 2012
21
Java Lab Programs
class AbstractAread { public static void main(String args[]) { //Figure f= new Figure(10,10); //illegal now rectangle r= new rectangle(9,5); triangle t=new triangle(10,8); Figure figref; figref = r; [Link]("Area is:"+[Link]()); figref = t; [Link]("Area is:"+[Link]()); } }
04 2012
22
Java Lab Programs
10 Client-Server Application CLIENT import [Link].*; import [Link].*;import [Link].*; import [Link].*; class Client { public static DatagramSocket ds; public static int clientport=6910,serverport=6909; public static void main(String args[])throws IOException { byte buffer[]=new byte[1024]; ds=new DatagramSocket(clientport); BufferedReader dis=new BufferedReader(new InputStreamReader([Link])); [Link]("\t\tCLIENT PROGRAM"); [Link]("\t\tCLIENT WAITING FOR INPUT"); InetAddress ia=[Link]("localhost"); while(true) { String str=[Link](); if(str==null||[Link]("end")) break; buffer=[Link](); [Link](new DatagramPacket(buffer,[Link](),ia,serverport)); } } } SERVER import [Link].*; class Server { public static DatagramSocket ds; public static byte buffer[]=new byte[1024]; public static int clientport=6910,serverport=6909; public static void main(String args[])throws IOException { ds=new DatagramSocket(serverport); [Link]("\t\tSEVER PROGRAM"); [Link]("\t\tPRESS ctrl+C TO COME PROMPT"); while(true) . 04 2012
23
Java Lab Programs
{ DatagramPacket p=new DatagramPacket(buffer,[Link]); [Link](p); String psx=new String([Link](),0,[Link]()); [Link](psx); } } }
04 2012
24
Java Lab Programs
11 Read & Write a File import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; public class ReadWriteFile { private static void doReadWriteTextFile() { try { String inputFileName = "[Link]"; String outputFileName = "[Link]"; FileReader inputFileReader = new FileReader(inputFileName); FileWriter outputFileReader = new FileWriter(outputFileName); BufferedReader inputStream = new BufferedReader(inputFileReader); PrintWriter outputStream = new PrintWriter(outputFileReader); String s = null; while ((s = [Link]()) != null) { [Link](s); [Link](s); } [Link](); [Link](); } catch (IOException e) { [Link]("IOException:"); [Link](); } } public static void main(String[] args) { doReadWriteTextFile(); } } . 04 2012
25
Java Lab Programs
12 Drawing Shapes import [Link].*; import [Link].*; public class Drawing extends Applet { public void paint(Graphics g) { [Link](10,10,50,50); [Link](10,10,60,40); [Link](60,60,50,50); } } //<applet code="Drawing" width="300" height="300"> //</applet>
04 2012
26