Posts

Showing posts from September 18, 2011

JAVA PROGRAM 8

JAVA PROGRAM 8: Create 2 threads such that one of thread prints even nos. and another prints odd nos. up to 50. itprofessionalsrocks.blogspot.com class A extends Thread { public void run() {  System.out.println("even number from 1 to 50 are given below"); for (int i=1;i<=50;i++) { if(i%2==0) { System.out.println("\n"+i);   }   }   System.out.println("exit from thread a");   }   }   class B extends Thread   {   public void run()   {    System.out.println("odd number from 1 to 50 are given below ");   for(int i=1;i<=50;i++)   {if(i%2!=0)  {    System.out.println("\n"+i);   }   }   System.out.println("exit form thread b");   }   }     class thread     {     public static void main(String args[])     {     A threadA=new A();     B threadB=new B();     System.out.println("Start thread A");     th...

java program 7

java program 7: Create a user defined exception named “CheckArguments” to check the arguments passed through command line. If no of arguments are less than 5, throw the “CheckArgument” exception else print addition and avg. of all 5 nos. import java.lang.Exception; import java.util.Scanner; import java.io.*; class CheckArguments extends Exception { CheckArguments(String message) {super(message); } } class arguments { public static void main(String args[]) { int a,count=0,sum=0,avg; System.out.println("enter numbers") ; for(int i=0;i<args.length;i++) { a=Integer.parseInt(args[i]); count=count+1; sum=sum+a; } avg=sum/i; try { if(count<5) { throw new CheckArguments("Arguments are greater than 5"); }  else {     System.out.println(sum);     System.out.println(avg); }  } catch (CheckArguments e) {System.out.println("Caught the exception"); } finally { System.out.println("i am always here"); } } }

java program 6

java program 6: itprofessionalsrocks.blogspot.com  WAP to define an exception called "MarksOutOfBound" exception that is thrown if the marks are out of bound. import java.lang.Exception; import java.util.Scanner; import java.io.*; class MarksOutOfBound extends Exception { MarksOutOfBound(String message) {super(message); } } class marksexcep { public static void main(String args[]) { Scanner obj=new Scanner(System.in); int[] a=new int[6]; System.out.println("enter marks for five subjects") ; for(int i=0;i<5;i++) {a[i]=obj.nextInt(); try {if(a[i]>100) { throw new MarksOutOfBound("marks are out of bound"); } } catch (MarksOutOfBound e) {System.out.println("Caught the exception"); } } } }

java program 5:

java program 5: WAP to catch any 3 built in exceptions in java. class exception { public static void main(String args[]) { try { int a=args.length; System.out.println("a="+a); int b=42/a; try { int c[]={1}; c[42]=99; } catch(ArrayIndexOutOfBoundsException e) {System.out.println("Arrays index oob"+ e); } } catch(ArithmeticException e ) { System.out.println("divide by 0"+e); } catch(ArrayStoreException e) { System.out.println("Wrong data type"); }catch(Exception e) { System.out.println("recaught"+ e); } } }

java program 3:

itprofessionalsrocks.blogspot.com java program 3: a simple program demonstrating the concept of inheritance .. class a { int i,j; void show() { System.out.println("i and j"+i+""+j); } } class b extends a {  int k;  void showk(){  System.out.println("k:"+k);  }  void sum()  {  System.out.println("i+j+k:"+(i+j+k));  }  }   class inherit   {   public static void main(String args[])  { a superob=new  a();   b subob=new b();   superob.i=10;   subob.j=20;   subob.showk();   System.out.println("Contents of superOb") ;   subob.i=7;   subob.j=10;   subob.k=11;   System.out.println("Contents of subob");   subob.showk();   System.out.println("Sum of i,j,k in subob");   subob.sum();   }   }

java program 4

itprofessionalsrocks.blogspot.com java program 4: a simple program demonstrating  use of super . class rectangle { float height; float breadth; rectangle(float h,float b) { height=h; breadth=b; } double area() {return height*breadth; } class rectweight extends rectangle { float weight; rectweight(float h,float b,float w) { super(h,b); weight=w; } } class super1 { public static void main(String args[]) { rectweight obj=new rectweight(10,20,50); double are; are=obj.area(); System.out.println("area is"+are); } }

JAVA PROGRAM 2

itprofessionalsrocks.blogspot.com java program 2: To find out whether the roots of the quadratic equation are imaginary,real and equal and unequal .  import java.util.*; import java.lang.Math; class roots { public static void main(String args[]) { double d,d1,d2; Scanner obj=new Scanner(System.in); System.out.println("enter the valur of a"); int a=obj.nextInt(); System.out.println("enter the valur of b"); int b=obj.nextInt(); System.out.println("enter the valur of c"); int c=obj.nextInt();  d=((b*b)-(4*a*c));  if(d>0)  { System.out.println("roots are real and unequal");  System.out.println("value of d"+ d);  d1=(-b+Math.sqrt(d))/2*a;  d2=(-b-Math.sqrt(d))/2*a ;  System.out.println(d1);  }  else if(d==0) { System.out.println("roots are real and equal");  d1=(-b+Math.sqrt(d))/2*a;  System.out.println("roots are"+d1);  }  else if(d<0)  { System.out.println("roots are imaginary ...

JAVA PROGRAM 1

itprofessionalsrocks.blogspot.com JAVA PROGRAM 1: TO CALCULATE FIBONACCI SERIES IN JAVA RECURSIVELY. import java.util.*; class fibo { public static int fib(int num) {int f; if(num==1) return 0; if(num==2) return 1; else f=fib(num-1)+fib(num-2); return f; }        public static void main(String args[])        {        Scanner obj=new Scanner(System.in);         int i;        System.out.println("enter number of elements to be generated");        int num=obj.nextInt();        for(i=1;i<=num;i++)        System.out.println(fib(i));        }     } FOR OUTPUT JUST PASTE THIS IN BIN FOLDER AND DO AS BELOW: JAVAC FIBO.JAVA JAVA FIBO THEN BY PRINTSCREEN AND PASTE IT IN PAINT .....