// program to show exception handling using try, catch and finally public class Excp { public static void

main(String... args) { int a,b,c; try { a = 50; b = 0; c = a/b; } catch(ArithmeticException e) { System.out.println("arithmetic exception occured " + e); } finally { System.out.println("finally block executed"); } } }

output arithmetic exception occured finally block executed java.lang.ArithmeticException:\by zero

out.b. } } } output 1 2 3 5 8 13 21 34 55 89 .i<10. System.// Program of Fabonaci series public class Fab { public static void main(String args[]) { long a. a=b.i++) { c=a+b. a=0. b=1. b=c.c. for(int i=0.print(" "+c).

n++) { f=f*n. } } Output: factorial is 120 . } System.println("factorial is" + f).n<=m. for(n=1.out. int n=1.// Find factorial of number public class Factorial { public static void main(String s[]) { int m = 5. int f=1.

System. } catch(IOException e) { System.io.// Program showing I/O concept import java. } } } output Enter Your Name : ram Hello ram . InputStreamReader isr=new InputStreamReader(System.out.println("Exception caught "+e).close(). BufferedReader br=new BufferedReader(isr). import java.awt.readLine(). pr. class Helloo { public static void main(String args[]) { try { PrintWriter pr=new PrintWriter(System. name=br.out).*.println("Enter Your Name : ").*.out. String name.in).println("Hello "+name). pr.

//program to show the concept of interface interface Intr { public void show().out. } } public class IntrEx { public static void main(String.show(). ar) { Conc obj = new Conc(). obj... } } output Interface Method defined in child class implementing interface . } class Conc implements Intr { public void show() { System.println("Interface Method defined in child class implementing interface").

i<3.j<3. } } .print(" sum="+sum[i]).j<3. k=k+1.i++) { System. } System.println("MATRIX IS AS FOLLOWS").out. //loop to initlize the matrix for(int i=0.out.0}. sum[i] = sum[i]+mat[j][i].print(" "+mat[i][j]).out.println(). System.j++) { System.i<3.out.i<3.out.println().i++) { for(int j=0. int[] sum ={0. } for(int i=0. } } //loop to display the matrix System.0. for(int i=0.i++) { for(int j=0. int k=0...println(). ar) { int[][] mat = new int[3][3]. System.j++) { mat[i][j]=k.// PROGRAM TO CREATE AND DISPLAY SUM COLUMN WISE public class MatrixColumn { public static void main(String.out.

output MATRIX IS AS FOLLOWS 0 3 6 sum=9 1 4 7 2 5 8 sum=12 sum=15 .

} } } output MATRIX IS AS FOLLOWS 0 1 2 3 4 5 6 7 8 . for(int i=0.i++) { for(int j=0. ar) { int[][] mat = new int[3][3].out.j<3. } } //loop to display the matrix System.out. k=k+1.println().println(). //loop to initlize the matrix for(int i=0.j++) { System.. } System.// PROGRAM TO CREATE AND DISPLAY A 3*3 MATRIX public class MatrixM { public static void main(String. System.println("MATRIX IS AS FOLLOWS").j<3.i<3.println(). int k = 0..out.i++) { for(int j=0.i<3.out. System.j++) { mat[i][j]=k.print(" "+mat[i][j]).out.

out.out.i++) { for(int j=0.out. System.out. int sum=0.. System. for(int i=0. k=k+1.// Program to show sum of all matrix row public class MatrixRow { public static void main(String.i<3. } } System.k=0.print(" "+mat[i][j]). sum = 0. ar) { int[][] mat = new int[3][3]. sum = sum+mat[i][j].println().print(" sum = "+sum).j++) { mat[i][j]=k.println().. } System.out.j++) { System. } } } .j<3.println("MATRIX IS AS FOLLOWS"). for(int i=0.i<3. System.i++) { for(int j=0.out.println().j<3.

output MATRIX IS AS FOLLOWS 0 1 2 3 4 5 6 7 8 sum=3 sum=12 sum=21 .

3..println("PATTERN IS AS FOLLOWS").{1.println().i<4. } } } output 1 12 123 1234 .print(arr[i][j]). ar) { int[][] arr = {{1}.length.out..4}}.i++) { for(int j=0.println().j++) { System.2.out.{1.2}. System.3}. System.out.out./* PROGRAM TO PRINT THE FOLLOWING PATTERN ON THE SCREEN WITH ARRAY 1 12 123 1234 */ public class Pattern { public static void main(String.{1.j<arr[i]. } System. for(int i=0.2.

out.println("InterruptedException caught").n = n.println("InterruptedException caught").out.out.println("Put: " + n). } } . } catch(InterruptedException e) { System.println("Got: " + n).out. valueSet = true. valueSet = false. notify(). } synchronized void put(int n) { if(valueSet) try { wait(). } System. } this.// Program showing the concept of Multithread class Q { int n. synchronized int get() { if(!valueSet) try { wait(). } catch(InterruptedException e) { System. System. notify(). return n. boolean valueSet = false.

} } } class PCFixed { . } public void run() { while(true) { q.start(). Consumer(Q q) { this.start(). while(true) { q.class Producer implements Runnable { Q q.q = q. "Producer").put(i++). "Consumer").q = q. new Thread(this. } public void run() { int i = 0. } } } class Consumer implements Runnable { Q q. new Thread(this. Producer(Q q) { this.get().

System.").public static void main(String args[]) { Q q = new Q().out. new Producer(q).println("Press Control-C to stop. new Consumer(q). } } output Put:10304 Got:10304 Put:10305 Got:10305 .

System. float fl = new Float("3.println("boolean ="+bb). Double d = new Double("344850998. Integer i = new Integer("6565"). Character c = new Character('a'). System. System. double dd = d. System. float ff = fl. char ch = c. boolean bb = b.out.out.. ar) { Boolean b = new Boolean("false").. short sh = s. System. int ii = i.println("long ="+ln). System. System.println("short ="+sh).out. Long l = new Long("46435").println("double ="+dd). } } .println("char ="+ch). Byte by = new Byte("3").6456").//program to create objects of each Wrapper class and convert it into primitive data types values public class Primi { public static void main(String.out.out.println("byte ="+byby).64768").println("float = "+ff). Short s = new Short("34").out.out.out.println("int ="+ii). long ln = l. System. byte byby = by.

4485099864768E8 byte =3 short =34 long =46435 char =a .6456 double =3.output boolean =false int =6565 float =3.

println("Area1 = "+ area1).rectArea(). Rectangle(int x. int area1 = rect1.int y) { length = x. } int rectArea() { return (length * width). width = y. } } class RectangleArea { public static void main(String args[]) { Rectangle rect1 = new Rectangle(15.width.10). System.// Program to show concept of constructor class Rectangle { int length. } } output: Area1 = 150 .out.

println("Exception occured").out.readLine()).parseInt(br.println("Enter first integer").// Sum of numbers using I/O import java.readLine()). System.println("Sum of the three numbers is: "+sum). b=Integer. a=Integer. try { System.out. } } } .out.out. class Sum { public static void main(String args[]) { int a.readLine()). } catch(IOException e) { System. System.io.out.println("Enter third integer").c.*.println("Enter second integer").parseInt(br.sum. System. sum=a+b+c. BufferedReader br=new BufferedReader(new InputStreamReader(System.b.in)). c=Integer.parseInt(br.

output Enter first integer 1 Enter second integer 2 Enter third integer 3 Sum of the three numbers is: 6 .

Sign up to vote on this title
UsefulNot useful