Friday, 13 September 2013

Write a Program Merge Sort in C++


Merge Sort: Merge Sort algorithm is based on Divide and Conquer theory. In this methodology, unsorted array list is divided into small sub-arrays. The sub-array elements are sorted and then merge into a single sorted array. Dividing of array into sub-array is done with the help of merge-sort method and sorting sub-array element and merging it to a single sorted array is done with the help of merge method. Time complexity of merge sort algorithm is O(n) where n=r-p+1. Have a look at below diagram, it is the buttom-up view of merge sort algorithm.




 Merge Sort:Algorithm:

                        MERGE-SORT (Apr)
                                 1.     IF p < r                            // Check for base case
                                 2.         THEN q = FLOOR[(p + r)/2]                 // Divide step
                                 3.                 MERGE (A, p, q)                          // Conquer step.
                                 4.                 MERGE (A, q + 1, r)                     // Conquer step.
                                 5.                 MERGE (A, p, q, r)                       // Conquer step.



Program:
 






















































 Output:



Tuesday, 13 August 2013

Java Scientific Calculator

class Calculator{
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable(){
            public void run(){
                CalculatorFrame frame = new CalculatorFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            }
        );
    }
}


class CalculatorFrame extends JFrame{
    public CalculatorFrame(){
    setTitle("Calculator");
    CalculatorPanel panel = new CalculatorPanel();
    add(panel);
    pack();
    }
}


class CalculatorPanel extends JPanel{
    public CalculatorPanel(){
        setLayout(new BorderLayout());
        result = 0;
        lastCommand = "=";
        start = true;
        // add the display
        display = new JButton("");
        display.setEnabled(false);
        add(display, BorderLayout.NORTH);
        ActionListener insert = new InsertAction();
        ActionListener command = new CommandAction();
        // add the buttons in a  x  grid
        panel = new JPanel();
        panel.setLayout(new GridLayout(4,4 ));
        addButton("7", insert);
        addButton("8", insert);
        addButton("9", insert);
        addButton("/", command);

        addButton("4", insert);
        addButton("5", insert);
        addButton("6", insert);
        addButton("*", command);

        addButton("1", insert);
        addButton("2", insert);
        addButton("3", insert);
        addButton("-", command);
        addButton("0", insert);
        addButton(".", insert);
        addButton("=", command);
        addButton("+", command);
        add(panel, BorderLayout.CENTER);
    }

    private void addButton(String label, ActionListener listener){
        JButton button = new JButton(label);
        button.addActionListener(listener);
        panel.add(button);
    }

    private class InsertAction implements ActionListener{
        public void actionPerformed(ActionEvent event){
            String input = event.getActionCommand();
            if (start){
                display.setText("");
                start = false;
            }
            display.setText(display.getText() + input);
        }
    }


    private class CommandAction implements ActionListener{
        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();

            if (start){
                if (command.equals("-")){
                display.setText(command);
                start = false;
                }
                else lastCommand = command;
            }
            else{
                calculate(Double.parseDouble(display.getText()));
                lastCommand = command;
                start = true;
            }
        }
    }


    public void calculate(double x){
        if (lastCommand.equals("+")) result += x;
        else if (lastCommand.equals("-")) result -= x;
        else if (lastCommand.equals("*")) result *= x;
        else if (lastCommand.equals("/")) result /= x;
        else if (lastCommand.equals("=")) result = x;
        display.setText("" + result);
    }

    private JButton display;
    private JPanel panel;
    private double result;
    private String lastCommand;
    private boolean start;
}

Monday, 12 August 2013

W.A.P to Find ODD or EVEN in Java

import java.util.Scanner;
class oddeven
{
    public static void main(String []args)
    {
        int x;
        System.out.println("Enter the Number");
        Scanner obj=new Scanner(System.in);
        x=obj.nextInt();
       
        if(x%2==0)
        {   
            System.out.println("Even Number");
        }
        else
        {
            System.out.println("Odd Number");
        }
    }
}



W.A.P. to Prints multiplication table of a number entered by the user using a for loop.

import java.util.Scanner;
class Mul
{
    public static void main(String []args)  
    {
        int n,c;
        System.out.println("Enter an integer to print it's

multiplication table");
  
    Scanner in=new Scanner(System.in);
    n=in.nextInt();
    System.out.println("Multiplication Table Of "+n+" is :-");

    for(c=1;c<=10;c++)

    System.out.println(n+"*"+c+" = "+(n*c));
    }


}














Friday, 9 August 2013

How to open notepad using Java program

import java.util.*;
import java.io.*;
 
class Notepad {
  public static void main(String[] args) {
    Runtime rs = Runtime.getRuntime();
 
    try {
      rs.exec("notepad");
    }
    catch (IOException e) {
      System.out.println(e);
    }   
  }
}

W.A.P Bubble Sort in JAVA

import java.util.Scanner;
 
class BubbleSort {
  public static void main(String []args) {
    int n, c, d, swap;
    Scanner in = new Scanner(System.in);
 
    System.out.println("Input number of integers to sort");
    n = in.nextInt();
 
    int array[] = new int[n];
 
    System.out.println("Enter " + n + " integers");
 
    for (c = 0; c < n; c++) 
      array[c] = in.nextInt();
 
    for (c = 0; c < ( n - 1 ); c++) {
      for (d = 0; d < n - c - 1; d++) {
        if (array[d] > array[d+1]) /* For descending order use < */
        {
          swap       = array[d];
          array[d]   = array[d+1];
          array[d+1] = swap;
        }
      }
    }
 
    System.out.println("Sorted list of numbers");
 
    for (c = 0; c < n; c++) 
      System.out.println(array[c]);
  }
}

Monday, 29 July 2013

W.A.P. to Make Calculator Using Applet in JAVA

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Calculator extends Applet
implements ActionListener
{
    String msg=" ";
    int v1,v2,result;
    TextField t1;
    Button b[]=new Button[10];
    Button add,sub,mul,div,clear,mod,EQ;
    char OP;
   
    public void init()
    {
        Color k=new Color(120,89,90);
        setBackground(k);
        t1=new TextField();
        GridLayout g1=new GridLayout(4,5);
        setLayout(g1);
        for(int i=0;i<10;i++)
        {
            b[i]=new Button(""+i);
        }
        add=new Button("add");
        sub=new Button("sub");
        mul=new Button("mul");
        div=new Button("div");
        mod=new Button("mod");
        clear=new Button("clear");
        EQ=new Button("EQ");
        t1.addActionListener(this);
        add(t1);
        for(int i=0;i<10;i++)
        {
                add(b[i]);
        }
       
        add(add);
        add(sub);
        add(mul);
        add(div);
        add(mod);
        add(clear);
        add(EQ);
        for(int i=0;i<10;i++)
        {
            b[i].addActionListener(this);
        }
        add.addActionListener(this);
        sub.addActionListener(this);
        mul.addActionListener(this);
        div.addActionListener(this);
        mod.addActionListener(this);
        clear.addActionListener(this);
        EQ.addActionListener(this);
    }
   
    public void actionPerformed(ActionEvent ae)
    {
        String str=ae.getActionCommand();
        char ch=str.charAt(0);
        if(Character.isDigit(ch))
        t1.setText(t1.getText()+str);
        else
            if(str.equals("add"))
            {
                v1=Integer.parseInt(t1.getText());
                OP='+';
                t1.setText("");
            }
            else if(str.equals("sub"))
        {
            v1=Integer.parseInt(t1.getText());
            OP='-';
            t1.setText("");
        }
        else if(str.equals("mul"))
        {
            v1=Integer.parseInt(t1.getText());
            OP='*';
            t1.setText("");
        }
        else if(str.equals("div"))
        {
            v1=Integer.parseInt(t1.getText());
            OP='/';
            t1.setText("");
        }
        else if(str.equals("mod"))
        {
            v1=Integer.parseInt(t1.getText());
            OP='%';
            t1.setText("");
        }
        if(str.equals("EQ"))
        {
            v2 = Integer.parseInt(t1.getText());
            if(OP= ='+')
                result = v1+v2;
            else if(OP= =' - ')
                result = v1- v2;
            else if(OP= =' * ')
                result = v1 * v2;
            else if(OP = = '/')
                result = v1 / v2;
            else if(OP = =' %')
                result = v1 % v2;
            t1.setText(""+result);
        }
        if(str.equals("clear"))
        {
            t1.setText("");
        }
    }
}



Sunday, 28 July 2013

Write a Applet for Drawing Human Face

import java.awt.*;
import java.applet.*;
public class face extends Applet
{
    public void paint(Graphics g)
    {
        g.drawOval(40, 40, 120, 150);
        g.drawOval(57, 75, 30, 20);
        g.drawOval(110, 75, 30, 20);
        g.fillOval(68, 81, 10, 10);
        g.fillOval(121, 81, 10, 10);
        g.drawOval(85, 100, 30, 30);
        g.fillArc(60, 125, 80, 40, 180, 180);
        g.drawOval(25, 92, 15, 30);
        g.drawOval(160, 92, 15, 30);
    }

}


Output




Applet in Interactive Input to an Applete

import java.awt.*;
import java.applet.*;

public class Bhargav extends Applet
{
                       TextField text1,text2;
                       
                      public void init( )
                        {
                                    text1 = new TextField(8);
                                    text2 = new TextField(8);
                                    add(text1);
                                    add(text2);
                                    text1.setText("0");
                                    text2.setText("0");                             
                       }

                       public void paint(Graphics g)
                       {
                                 int x = 0, y = 0, z = 0;
                                 String s1,s2,s;
                                 g.drawString("Input a number in each box",10,50);
                         
                                  try
                                 {
                                                      s1 = text1.getText( );
                                                      x = Integer.parseInt(s1);
                                                      s2 = text2.getText( );
                                                      y= Integer.parseInt(s2);
                                  }
                                  catch( Exception ex) { }
                                  z = x + y;
                                  s = String.valueOf (z);
                                  g.drawString ("The Sum is :", 10, 75);
                                  g.drawString (s,100,75);

                       }
                 
                           public Boolean action (Event event, Object object)
                           {
                                         repaint ( );
                                         return true;
                            }
                      
}

W.A.P. to find Merge Sort using C++


 

Saturday, 27 July 2013

W.A.P to Print Hello Word in C++

#include<iostream.h>
#include<conio.h>
int main()
{
           cout<<"Hello World:";
           getch();
            return 0;
}

Output:

HelloWorld:

Write a Program Bubble Sort using Sorting Algorithm in C++ Language

#include<iostream.h>
#include<conio.h>
int main()
{
           int arr[5],i,j,temp;
           cout<<"Enter the element of Array:";
           for(i=0;i<5;i++)
           {
                    cin>>arr[i];
           }

           cout<<"Array are Sorted:";
           for(i=0;i<5;i++)
           {
                      for(j=o;j<4-i;j++)
                      {
                                  if(arr[j]<arr[j+1)
                                  {
                                              temp=arr[j];
                                              arr[j]=arr[j+1];
                                              arr[j+1]=temp;
                                  }
                      }
                    cout<<arr[j];
            }

         getch();
         return 0;
}



Output : 

Enter the element of Array:2
8
9
4
6

Array are Sorted :24689

Followers