NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Sunday, October 21, 2012

Java program to print fibonacci series upto a given number

import java.io.*;
class Fibonacci
{ int next,curr,prev,num;
            Fibonacci(int a)
  { next=1;
     curr=1;
    prev=0;
    num=a;  }
  void calc()
  { while(prev<=num)
    { System.out.println(prev);
      prev=curr;
      curr=next;
      next=prev+curr;  }
  }
  public static void main(String s[])throws IOException
  { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the limit");
    int a=Integer.parseInt(br.readLine());
    Fibonacci f=new Fibonacci(a);
    f.calc(); }
}
Output


Java program to illustrate exception handling


import java.lang.ArithmeticException;
import java.util.*;                                //import util package
public class ExceptionDemo
{  public static void main(String s[])
  {   Scanner b1=new Scanner(System.in);
    try
     {  System.out.println("Enter the Divident?");
        double a=b1.nextDouble();
        System.out.println("Enter the Divisor?");
        double b=b1.nextDouble();
      if(b==0)
  //Throwing an Exception using throw
       throw new ArithmeticException("Division by zero!");
       double res=a/b;
       System.out.println("Result of"+a+"/"+b+"is"+res);
     }
  //Handles input mismatch exception caused by
 //entering a non-integer value at th point
  catch(InputMismatchException e)
       {  System.out.println("Error:"+e);}
//Handles the divide-by-zero ArithmeticException
  catch(ArithmeticException ae)
      {  System.out.println("Error:"+ae);}
   }
}
Output

Java program to find distance between two points in a plane


import java.io.*;
class distance
{
            int x,y,dist;
            distance()
             {  x=0;
              y=0;
 }
  void calc(distance c,distance d)
  {
     int xd=c.x-d.x;
     int xs=xd*xd;
     int yd=c.y-d.y;
     int ys=yd*yd;
     int sum=xs+ys;
     double dist=Math.sqrt(sum);
     System.out.println("The distance is "+dist);
  }
  void read()throws IOException
  { 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter the x coordinate");
     x=Integer.parseInt(br.readLine());
     System.out.println("Enter the y coordinate");
     y=Integer.parseInt(br.readLine());
     System.out.println("The entered point is "+"("+x+","+y+")");
  }
  public static void main(String s[])throws IOException
  {
     distance c=new distance();
     distance d=new distance();
     System.out.println("First point");
     c.read();
     System.out.println("Second point");
     d.read();
     d.calc(c,d);
  }
}
Output

Java program to perform complex number addition


import java.io.*;
class complex
{
   int r,i;
   complex()
{
   r =0;
   i=0;
}
  void sum(complex c,complex d)
{
complex a=new complex();
a.r=c.r+d.r;
a.i=c.i+d.i;
     System.out.println("The sum is "+a.r+"+"+a.i+"i");
  }
  void read()throws IOException
  {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("\nEnter the real part");
   r=Integer.parseInt(br.readLine());
  System.out.println("\nEnter the imaginary part");
    i=Integer.parseInt(br.readLine());
  System.out.println("\nYou have entered : "+r+"+"+i+"i");
  }
  public static void main(String s[])throws IOException
  { 
     complex c=new complex();
     complex d=new complex();
     System.out.println("First number");
     c.read();
     System.out.println("\nSecond number");
     d.read();
     d.sum(c,d);
  }
}
Output

Java program to perform string sorting


import java.io.*;
class stringsort
 { String p[];int i;
 
  stringsort(int q)
  {
    p=new String[q];
  }
 void read() throws IOException
  {BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
   for(int i= 0; i<p.length; i++)

   {System.out.println("Enter the "+(i+1)+"th element");
     p[i]=br.readLine();
   }
  }
 void sort()
  { for (int i=0;i<p.length-1;i++)
    for(int j=i+1;j<p.length;j++)
 
 {
   if (p[i].compareTo(p[j])>0)
  { String temp= p[i];
    p[i]=p[j];
    p[j]= temp;
  }
 }
}
 void disp()
 {System.out.println("The sorted string is:");
  for (int i=0;i<p.length;i++)
  System.out.println(p[i]);
 }
 public static void main(String s[]) throws IOException

 {BufferedReader b= new BufferedReader(new InputStreamReader(System.in));
  System.out.println("How many names?") ;
  int m =  Integer.parseInt(b.readLine());
  stringsort k= new stringsort(m);
  k.read();
  k.sort();
  k.disp();
}
}
Output

Java program to illustrate the concept of abstract class


abstract class figure
{float l,b;
 figure(float x,float y)
 {l=x;
  b=y; }
 abstract float area();
}
class rectangle extends figure
{ rectangle(float a,float b)
{ super(a,b); }
float area()
{ return(l*b); }
}
class triangle extends figure
{triangle(float a,float b)
  {   super(a,b);  }
  float area()
 {return(l*b/2); }
}
class figurearea
{ public static void main(String a[])
 {  rectangle r=new rectangle(2.5f,2.5f);
  triangle t=new triangle(1f,2.0f);
  figure f=r;
  System.out.println("area of rectangle="+f.area());
  f=t;
  System.out.println("area of triangle="+f.area()); }
}

Output