Friday 2 January 2015

Sort In Java

Odd Even Transposition Sort In Java

Introduction  about Transpositions
In this example we are going to sort integer values of an array using odd even transposition sort.

Odd even transposition sort is a parallel sorting algorithm. Odd Even is based on the Bubble Sort technique of comparing two numbers and swapping them and put higher value at larger index .In each parallel computational steps can pair off either the odd or even neighboring pairs. Each number (In Processing Element-PE) would look to it's right neighbor and if it were greater, it would swap them.

Code description:
The odd even transposition sort is a parallel sorting algorithm. That mean more than one compression can made at one iteration. The comparison is same as bubble sort.

Working of odd even transposition sort:

The code of the program :

public class OddEvenTranspositionSort{
  public static void main(String a[]){
  int i;
  int array[] {12,9,4,99,120,1,3,10,13};
  
  System.out.println("\n\n RoseIndia\n\n");
  System.out.println(" Odd Even Transposition Sort\n\n");
  System.out.println("Values Before the sort:\n");
  for(i = 0; i < array.length; i++)
  System.out.printarray[i]+"  ");
  System.out.println();
  odd_even_srt(array,array.length);
  System.out.print("Values after the sort:\n");
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println();
  System.out.println("PAUSE");
  }

  public static void odd_even_srt(int array[],int n){
  for (int i = 0; i < n/2; i++ ) {
  for (int j = 0; j+< n; j += 2)
  if (array[j> array[j+1]) {
  int T = array[j];
  array[j= array[j+1];
  array[j+1= T;
  }
  for (int j = 1; j+< array.length; j += 2)
  if (array[j> array[j+1]) {
  int T = array[j];
  array[j= array[j+1];
  array[j+1= T;
  }
  }
  }
}
Output of the example:
C:\array\sorting>javac OddEvenTranspositionSort.java
C:\array\sorting>java OddEvenTranspositionSort
       RoseIndia
       Odd Even Transposition Sort
Values Before the sort:
12  9  4  99  120  1  3  10  13
Values after the sort:
1  3  4  9  10  12  13  99  120
PAUSE
C:\array\sorting>_

Creating a java desktop pane or panel for data entry

How to use this
This Java program is used for creating a Desktop pane. Copy the following code and paste on a note pad, save it as "createJDesktoPane.java" and run the program.

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class createJDesktoPane extends JFrame {

 //Initializing program components
 private JDesktopPane desktopTest;
 private JLabel labels[];
 private JTextField inputs[];
 private JButton buttons[];
 private String labelName[]={"Enter Name: ","Enter Age: ","Enter Address: ","Enter Mobile#: "};
 private String buttonName[] = {"Open","Save","Exit"};
 private JPanel panel1, panel2;

 //Setting up GUI
    public createJDesktoPane() {
    
     //Setting up the Title of the Window
     super("Creating a JDesktopPane");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(600,500);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     JMenuBar bar = new JMenuBar(); //Constructing JMenuBar
     JMenu menu = new JMenu("File"); //Constructing JMenu name "File"
     JMenuItem newFile = new JMenuItem("Add New Data"); //Constructing JMenuItem with "Add New Data" label
    
     menu.add(newFile); //Adding JMenuItem in the JMenu
     bar.add(menu); //Adding JMenu in the JMenuBar
    
  setJMenuBar(bar); //Adding JMenuBar in the container
 
  desktopTest = new JDesktopPane(); //Creating a JDesktopPane
  desktopTest.setBackground(Color.BLACK); //Setting JDesktopPane background color
 
     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
    
     pane.add(desktopTest); //Adding JDesktopPane in the container
    
     //Implemeting Even-Listener on newFile JMenuItem
     newFile.addActionListener(
  new ActionListener() {
  
   //Handle JMenuItem "newFile" event if it is clicked
   public void actionPerformed(ActionEvent e) {
  
   //Constructing an Internal Frame inside JDesktopPane
   JInternalFrame frame = new JInternalFrame(null,true,true,true,true);
  
   Container container = frame.getContentPane(); //Creating a container inside the JInternalFrame
  
   //Constructing JLabel, JButton, and JTextField inside JInternalFrame
   labels = new JLabel[4];
   inputs = new JTextField[4];
   buttons = new JButton[3];
  
   //Creating a JPanel 1 with GridLayout of 4 rows and 2 columns inside JInternalFrame
   panel1 = new JPanel();
   panel1.setLayout(new GridLayout(4,2));
  
   //Constructing JLabel and JTextField using "for loop" and add to JPanel 1
   for(int count=0; count<labels.length && count<inputs.length; count++) {
    labels[count] = new JLabel(labelName[count]);
    inputs[count] = new JTextField(10);
    panel1.add(labels[count]);
    panel1.add(inputs[count]);
   }
  
   //Creating a JPanel 2 with GridLayout of 1 row and 3 columns inside JInternalFrame
   panel2 = new JPanel();
   panel2.setLayout(new GridLayout(1,3));
  
   //Constructing JButton using "for loop" and add to JPanel 2
   for(int count=0; count<buttons.length; count++) {
    buttons[count] = new JButton(buttonName[count]);
    panel2.add(buttons[count]);
   }
  
   //Adding JPanel 1 and 2 to the JInternalFrame container
   container.add(panel1,BorderLayout.NORTH);
   container.add(panel2,BorderLayout.CENTER);
  
   frame.setTitle("Add New Data"); //Set the Title of the JInternalFrame
   frame.setResizable(false); //Lock the size of the JInternalFrame
   frame.setMaximizable(false); //Disable the Maximize function of JInternalFrame
  
   //Set the size of JInternalFrame to the size of its content
   frame.pack();
  
   //Attached the JInternalFrame to JDesktopPane and show it by setting the visible in to "true"
   desktopTest.add(frame);
   frame.setVisible(true);
   }
  }
  );

     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
      */
     setVisible(true);
    }
   
 //Main Method
    public static void main (String[] args) {
     createJDesktoPane dp = new createJDesktoPane();
 }
}

Java Program. Demo for Password Field - Using Package

/*Copyright Right Reserved MIT*/
package javaapplication1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

public class JavaApplication1 extends JPanel implements ActionListener{

    private static String OK = "ok";
    private static String HELP = "help";

    private JFrame controllingFrame; //needed for dialogs
    private JPasswordField passwordField;

    public JavaApplication1(JFrame f) {
        //Use the default FlowLayout.
        controllingFrame = f;

        //Create everything.
        passwordField = new JPasswordField(10);
        passwordField.setActionCommand(OK);
        passwordField.addActionListener(this);

        JLabel label = new JLabel("Enter the password: ");
        label.setLabelFor(passwordField);

        JComponent buttonPane = createButtonPanel();

        //Lay out everything.
        JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        textPane.add(label);
        textPane.add(passwordField);

        add(textPane);
        add(buttonPane);
    }

    protected JComponent createButtonPanel() {
        JPanel p = new JPanel(new GridLayout(0,1));
        JButton okButton = new JButton("OK");
        JButton helpButton = new JButton("Help");

        okButton.setActionCommand(OK);
        helpButton.setActionCommand(HELP);
        okButton.addActionListener(this);
        helpButton.addActionListener(this);

        p.add(okButton);
        p.add(helpButton);

        return p;
    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (OK.equals(cmd)) { //Process the password.
            char[] input = passwordField.getPassword();
            if (isPasswordCorrect(input)) {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Success! You typed the right password.");
            } else {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Invalid password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            }

            //Zero out the possible password, for security.
            Arrays.fill(input, '0');

            passwordField.selectAll();
            resetFocus();
        } else { //The user has asked for help.
            JOptionPane.showMessageDialog(controllingFrame,
                "You can get the password by searching this example's\n"
              + "source code for the string \"correctPassword\".\n"
              + "Or look at the section How to Use Password Fields in\n"
              + "the components section of The Java Tutorial.");
        }
    }

    /**
     * Checks the passed-in array against the correct password.
     * After this method returns, you should invoke eraseArray
     * on the passed-in array.
     */
    private static boolean isPasswordCorrect(char[] input) {
        boolean isCorrect = true;
        char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };

        if (input.length != correctPassword.length) {
            isCorrect = false;
        } else {
            isCorrect = Arrays.equals (input, correctPassword);
        }

        //Zero out the password.
        Arrays.fill(correctPassword,'0');

        return isCorrect;
    }

    //Must be called from the event dispatch thread.
    protected void resetFocus() {
        passwordField.requestFocusInWindow();
    }

    /**
     * Create the GUI and show it.  For thread safety,     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("PasswordDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        final JavaApplication1 newContentPane = new JavaApplication1(frame);
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Make sure the focus goes to the right component
        //whenever the frame is initially given the focus.
        frame.addWindowListener(new WindowAdapter() {
            public void windowActivated(WindowEvent e) {
                newContentPane.resetFocus();
            }
        });

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        createAndShowGUI();
            }
        });
    }
}

Check for errors );
PASSWORD AS "bugaboo"

JAVA DATABASE CONNECTIVITY Program to connect with Oracle for faculty 

import java.awt.*;

import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.event.*;
import java.sql.*;


public class Lab7 extends JFrame implements ActionListener
{
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b1,b2,b3;

Lab7()
        {
        super("DataBases");
        setSize(500,450);
        setResizable(false);
        setLayout(null);


        l1=new JLabel("Name           :");
        l2=new JLabel("Department :");
        l3=new JLabel("Experience  :");

        t1=new JTextField(20);
        t2=new JTextField(20);
        t3=new JTextField(20);
       
        b1=new JButton("Next");       
        b2=new JButton("Prev");
        b3=new JButton("Close");
       
       add(l1).setBounds(10,60,150,20);
       add(t1).setBounds(150,60,200,20);
  
      add(l2).setBounds(10,90,150,20);
      add(t2).setBounds(150,90,200,20);
 
  add(l3).setBounds(10,110,150,20);
  add(t3).setBounds(150,130,200,20);

   b1.setMnemonic('N');
   b2.setMnemonic('P');
   b3.setMnemonic('x');               
  
       add(b1).setBounds(100,180,120,20);       
  add(b2).setBounds(240,180,120,20);      
  add(b3).setBounds(380,180,120,20);

   b1.addActionListener(this);   
   b2.addActionListener(this);
   b3.addActionListener(this);
       
  addWindowListener(new WindowAdapter(){public void  windowClosing(WindowEvent e){System.exit(0);}});
   }

public void actionPerformed(ActionEvent ae)
        {   
try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");     
   Connection   dbcon=DriverManager.getConnection ("jdbc:odbc:ram","scott","tiger");
   
 String query="select  name,dept,exp   from   faculty";
     Statement stmt=dbcon.createStatement();
     ResultSet rs=stmt.executeQuery(query);
   
     if(ae.getSource()==b1)
        {
            rs.next();
            t1.setText(rs.getString("name"));
            t2.setText(rs.getString("dept"));
            t3.setText(rs.getString("exp"));
        }
   
if(ae.getSource()==b2)
        {
            rs.previous();
            t1.setText(rs.getString("name"));
            t2.setText(rs.getString("dept"));
            t3.setText(rs.getString("exp"));
        }


     if(ae.getSource()==b3)
{
            System.exit(0);
        }
    }
    catch(ClassNotFoundException e)
    {
    System.out.println(e);
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
        }

public static void main(String str[])
        {
        Lab7  l=new Lab7();
        l.setVisible(true);
        }
}

Saturday 27 December 2014

Famous National Parks in India


StateNational ParksStartedArea (in Km)Attractions
Andaman Nicobar IslandsWandur National Park1983281.50Estuarine Crocodiles, Coconut Crab
Arunachal PradeshNamdapha National Park19831985.23Leopard, Gaur, Himalayan Black Bear
Assam
Kaziranga National Park1974471.71Rhinos, Elephants, Tigers
Manas National Park1990500Assam Roofed Turtle, Golden Langur
ChhattisgarhIndravati National Park19811258.37Tiger, Leopard, Blue Bull,
GujaratGir National Park1975258.71Asiatic Lion
Marine National Park1980162.89 
Himachal PradeshGreat Himalayan National Park1984754.40 
Pin Valley National Park1987675Himalayan Snowcock, Chukar
Jammu And KashmirDachigam National Park1981141Himalayan Black Bears, Leopard
Hemis National Park19814100Snow Leopard
Kishtwar National Park1981400Himalayan Jungle Crow
JharkhandHazaribagh National Park  Tigers, Wild Boar, Nilgai
Palamau National Park  Tigers, Dhole, Elephants
KarnatakaBandipur National Park1974874.20Asian Elephants, Tiger
Bannerghatta National Park1974104.27Tiger, Lion
Nagarhole National Park1988643.39Elephant, Jackal, Tiger
KeralaEravikulam National Park197897Nilgiri Tahr, Atlas Moth, Elephant
Periyar National Park1982350Nilgiri Langur, Flying Squirrel
Silent Valley National Park198489.52Nilgiri Tahr, Niligiri Langur,Tiger
Madhya PradeshBandhavgarh National Park1982448.85Tigers, Leopards, Bears
Kanha National Park1955940Tigers, Leopards, Elephant
Madhav National Park1959375.22Indian Gazelle, Nilgai, Sambar
Panna National Park1973542.67Tiger, Wolf, Chital, Sloth Bear
Pench National Park1975292.85Tiger, Leopard, Sloth Bear
MaharashtraNavegaon National Park1975133.88Tiger, Panther, Bisons
Tadoba National Park1955116.55Tiger, Leopards, Sloth Bears
OdishaChandaka Elephant Reserve  Elephant, Hital, Bear, Pea-Fowl
Nandankanan National Park  White Tiger, Asiatic Lion, Crocodiles
Simlipal National Park1980845.70Tiger, Leopard, Elephants
RajasthanDesert National Park19803162Great Indian Bustard, Harriers
Keoladeo National Park198128.73Siberian Cranes, Ruddy Shelducks
Ranthambore National Park1980392Tigers, Leopards, Boars
Sariska National Park1982273.80Four-Horned Deer, Carecal, Leopard
SikkimKhangchendzonga National Park19771829Wild Ass, Snow Leopard, Musk Deer, Himalayan Tahr
Uttar PradeshDudhwa National Park1977490.29Tiger, Rhinoceros
UttarakhandCorbett National Park1936520.82Tigers, Leopards, Elephants
Govind National Park1990472.08Black Bear, Leopard, Snow Cock
Nanda Devi National Park19885,860.69Tiger, Leopard
Rajaji National Park1983820.42Tigers, Leopards, Elephants,
Valley of Flowers National Park198087.50Snow Leopard, Musk Deer, Red Fox
West BengalSundarbans National Park19841330.10Royal Bengal Tiger; Fishing Cats
MeghalayaBalpakram National Park   
Nokrek National Park
- See more at: http://www.indiawildliferesorts.com/national-parks/#sthash.RelZtpn6.dpuf

Friday 26 December 2014

Dr Shanti Swarup Bhatnagar



Dr Shanti Swarup Bhatnagar
 
Dr Shanti Swarup Bhatnagar one of the most renowned scientist from India was a member of the academic staff of the
Science College BHU, teaching Chemistry much before 1930.The Britisher honored him for his Scientific acumen and
exemplary contribution to the Scientific world, by conferring him the title of Knighthood and he became
Sir S.S.Bhatnagar he
had an extraordinary command on Hindi, English and Urdu literature as well and he was a natural poet
writing spontaneous Urdu poetry and he has witten the BHU Anthem Madhur Monohar

DOWNLOAD MUSIC

Meaning in English
English Version
Kulgeet

So sweet serene, infinitely beautiful
This is the presiding center of all learning
Radiant Kashi, wonder of the three worlds
Treasure-Chest of Jnana , Dharma and Satya
Nesting on Ganga`s bank, center of all disciplines.
(So sweet, serene, infinitely beautiful)
No Recent work of brick and stone
Primordial design of divinity alone
Mansions of Vidya, center of all creation.
(So sweet, serene, infinitely beautiful)
Clear here is the doctrine pure
Truth first, then only one' self
Home of Harishchandra, Truth's testing ground.
(So sweet, serene, infinitely beautiful)
The voice of God in Vedic record
Constant Inspiration for soul-accord
Work-shop of Veda Vyasa, center freedom for Bhrahma Vidya
(So sweet, serene, infinitely beautiful)
Find here the steps of freedom
Tread here the path of Dharma
Flaming trail Budha`s and Shankara`s center for philosopher-kings.
(So sweet, serene, infinitely beautiful)
Life-Giving waters of Varuna and Assi
Sustenance of Kabir and Tulsi
Fountainhead of eloquent speech and poetry.
(So sweet, serene, infinitely beautiful)
Music, Economics, other arts so many
Maths, Mining, Medicine and Chemistry
Fraternal forum of East and West , university in trust sense.
(So sweet, serene, infinitely beautiful)
Patriotism of Malviyaji
His intrepidity and energy
All in youthful manifestation, centre for men of action
(So sweet, serene, infinitely beautiful)

                                                   Written By Dr S.S.Bhtnagar