This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Sunday, May 5, 2013

Write a java program on Bubble Sort


/* Bubble Sort program in java */
import java.lang.*;
import java.io.*;
class BubbleSort
{
        public static void main(String args[])throws IOException
        {
               BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
               int n,i,j,temp;
               System.out.println("----Welcome to the Bubble Sort Prog----");
               System.out.println("Hi mani how many elements you have");
               n=Integer.parseInt(br.readLine());
               int a[]=new int[n];
               System.out.println("Enter " +n+ " Elements");
               for(i=0;i<n;i++)
                a[i]=Integer.parseInt(br.readLine());
               System.out.println("Before Sorting");
               for(i=0;i<n;i++)
                System.out.print("\t"+a[i]);

               for(i=0;i<n;i++)
               {
                for(j=i+1;j<n;j++)
                {
                        if(a[i]>a[j])
                        {
                                temp=a[j];
                                a[j]=a[i];
                                a[i]=temp;
                        }
                 }
                }
                System.out.println("\nAfter Sorting");
                for(i=0;i<n;i++)
                 System.out.print("\t" +a[i]);
       }
}
              
Output:

----Welcome to the Bubble Sort Prog----
Hi mani how many elements you have
5
Enter 5 Elements
20
30
20
15
20
Before Sorting
        20      30      20      15      20
After Sorting
        15      20      20      20      30

Write a java program to find the area of the Circle


/* Program to find the Area of the Circle using pi*r*r formula */
import java.lang.*;
import java.io.*;
class Circlearea
{
    public static void main(String args[])
    {
        float pi=3.14159f,area;
        int r=5;
        area=pi*r*r;
        System.out.println("The area of the circle is:"+area);
    }
}

Output:

E:\javamani>java Circlearea
The area of the circle is:78.53975

The Fibonacci sequence is defined by the following rule. The first 2 values in the sequenc are 1,1. Every subsequent value is the sum of the 2 values preceding it. Write A Java Program (WAJP) that uses both recursive and non-recursive functions to print the n th value of the Fibonacci sequence.



/*1.Fibonacci program using Recursive Functions*/
import java.lang.*;
import java.io.*;
class FibonacciRecursive
{
    public static void main(String args[])throws IOException
    {
    int n;
    BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter The range");
    n=Integer.parseInt(d.readLine());
    System.out.println("The fibonacci series is:");
    Fibonaccino(n);//funcion Calling
    }//Main method End.
  
    static void Fibonaccino(int n)
    {
        int i,sum=0,f=1,res;  
        for(i=0;i<n;i++)
        {
        res=sum+f;
        System.out.println(sum);
        sum=f;
        f=res;
        }
        System.out.println("\nTask completed.");
    }
}

Output:

E:\javamani>java FibonacciRecursive
Enter The range
10
The fibonacci series is:
0
1
1
2
3
5
8
13
21
34
Task completed.

Write a Java program that allows the user to draw lines, rectangles and ovals.


/*Write a Java program that allows the user to draw lines, 

rectangles and ovals.*/
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class LinesRectsOvals extends JApplet
{
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.red);        

g.drawLine(5,30,350,30);
        g.setColor(Color.blue);
        g.drawRect(5,40,90,55);
        g.fillRect(100,40,90,55);
        g.setColor(Color.cyan);
        g.fillRoundRect(195,40,90,55,50,50);
        g.drawRoundRect(290,40,90,55,20,20);
        g.setColor(Color.yellow);
        g.draw3DRect(5,100,90,55,true);
        g.fill3DRect(100,100,90,55,false);
        g.setColor(Color.magenta);
        g.drawOval(195,100,90,55);
        g.fillOval(290,100,90,55);
    }
}
        

Output

Demonstrate the functionality of packages.


/*Write a java program to create and demonstrate packages.*/
package MyPack;

public class Balance
{
      String name;
      double bal;
      public Balance(String n,double b)
      {
                  name=n;
                  bal=b;
      }
      public void Show()
      {
                  if(bal<0)
                  {
                              System.out.print("--> ");
                  }
                  System.out.println(name + ": $" + bal);
      }
}

   TESTBALANCE.JAVA

   import MyPack.*;

class TestBalance
{
            public static void main(String args[])
            {
                        Balance test=new Balance("J.J.Jaspers",99.88);
                        test.Show();
            }
}

Twitter Delicious Facebook Digg Stumbleupon Favorites More