Friday 2 January 2015

JAVA program for creating a minimum spanning tree using Kruskal's algorithm for Shortest Path


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct edge
{
    int u;
    int v;
    int weight;
    struct edge *link;
};

class kruskals
{
private:
    struct edge *front,tree[20];
    int father[20],n,wt_tree,count;
public:
    kruskals()
    {
       front=NULL;
       wt_tree=0;
       count=0;
       cout<<"Enter total no. of nodes in graph : ";
       cin>>n;
    }
    void create_graph();
    void make_tree();
    void insert_tree(int i,int j, int wt);
    void insert_priorityQ(int i,int j,int wt);
    struct edge *del_priorityQ();
    void display();
};

void kruskals::create_graph()
{
    int wt,max_edges,origin,destin;
    max_edges=n*(n-1)/2;
    for(int i=1;i<=max_edges;i++)
    {
        cout<<"Enter edge "<<i<<" (0 0 to quit) : ";
        cin>>origin>>destin;
        if((origin==0)&&(destin==0))
              break;
        cout<<"Enter weight of this edge : ";
        cin>>wt;
        if(origin>n || destin>n || origin<=0 || destin<=0)
        {
            cout<<"Invalid edge! "<<endl;
            i--;
        }
        else
        insert_priorityQ(origin,destin,wt);
    }
    if(i<n-1)
    {
        cout<<"Spanning tree is not possible "<<endl;
                getch();
        exit(1);
    }
}

void kruskals::make_tree()
{
    struct edge *temp;
    int node1,node2, root_n1,root_n2;

    while(count<n-1)
    {
        for(int i=0;i<20;i++)
                      father[i]=NULL;
        temp=del_priorityQ();
        node1=temp->u;
        node2=temp->v;
        cout<<endl<<"n1="<<node1<<"    "<<"n2="<<node2<<"     ";
        while(node1>0)
        {
             root_n1=node1;
             node1=father[node1];
        }
        while(node2>0)
        {
             root_n2=node2;
             node2=father[node2];
        }
        cout<<"rootn1="<<root_n1<<"     "<<"rootn2="<<root_n2<<endl;

        if(root_n1 != root_n2)
        {
              insert_tree(temp->u,temp->v,temp->weight);
              wt_tree=wt_tree+temp->weight;
              father[root_n2]=root_n1;
        }
    }
}

void kruskals::insert_tree(int i, int j, int wt)
{
    cout<<"This edge inserted in the spanning tree "<<endl;
    count++;
    tree[count].u=i;
    tree[count].v=j;
    tree[count].weight=wt;
}

void kruskals::insert_priorityQ(int i, int j, int wt)
{
    struct edge *temp,*q;

    temp=new edge;
    temp->u=i;
    temp->v=j;
    temp->weight=wt;

    if(front==NULL || temp->weight<front->weight)
    {
        temp->link =front;
        front=temp;
    }
    else
    {
        q=front;
        while(q->link !=NULL && q->link->weight<=temp->weight)
              q=q->link;
        temp->link=q->link;
        q->link=temp;
        if(q->link==NULL)
               temp->link=NULL;
    }
}

struct edge* kruskals::del_priorityQ()
{
    struct edge *temp;
    temp=front;
    cout<<"Edge processed is "<<temp->u<<"->"<<temp->v<<":"<<temp->weight;
    front=front->link;
    return temp;
}

void kruskals::display()
{
    cout<<"Edges to be included in spanning tree are : "<<endl;
    for(int i=1;i<=count;i++)
    {
       cout<<"("<<tree[i].u<<" ";
       cout<<tree[i].v<<")   ";
    }
    cout<<endl
        <<"Weight of this minimum spanning tree is : " <<wt_tree;
}

void main()
{
clrscr();
kruskals k;
k.create_graph();
k.make_tree();
k.display();
getch();
}

Program to find largest and smallest number in an array in JAVA


public class FindLargestSmallestNumber {
public static void main(String[] args) {

//array of 10 numbers

int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};

//assign first element of an array to largest and smallest

int smallest = numbers[0];

int largetst = numbers[0];

for(int i=1; i< numbers.length; i++) { if(numbers[i] > largetst)

largetst = numbers[i];

else if (numbers[i] < smallest) smallest = numbers[i]; } System.out.println("Largest Number is : " + largetst); System.out.println("Smallest Number is : " + smallest); } }

Java Bank Program Just see the logic and it will be easy for you.

/*
    program:bankAccount
    Name:JustinLubarsky
    Date:Dec 2010
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class bankAccount extends JFrame
{
    /* ------------------------- declarations */

    // color objects
   
    Color black = new Color(0, 0, 0);
    Color white = new Color(255, 255, 255);
    Color light_gray = new Color(192, 192, 192);

    // inputs
   
    JLabel depositAmmountJLabel;
    JTextField depositAmmountJTextField;
   
    JLabel withdrawAmmountJLabel;
    JTextField withdrawAmmountJTextField;

    // outputs
   
    JLabel currentBalanceJLabel;
    JTextField currentBalanceJTextField;
   
    JLabel currentStatusJLabel;
    JTextField currentStatusJTextField;

    // controls
   
    JButton enterJButton;
    JButton clearJButton;
    JButton closeJButton;

    // variables
   
    int startingBalance = 0;
    Double depositAmmount;
    Double withdrawAmmount;
    Double currentBalance;
    String currentStatus;

    // objects classes
   
    DecimalFormat decimalFormat;

    public bankAccount()   
    {
        createUserInterface();
    }

    public void createUserInterface()
    {
        Container contentPane = getContentPane();
        contentPane.setBackground(white);
        contentPane.setLayout(null);
       
        //*--------------------- initialize *\

        // inputs
       
        depositAmmountJLabel = new JLabel();
        depositAmmountJLabel.setBounds(50, 50, 120, 20);
        depositAmmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        depositAmmountJLabel.setText("Deposit Ammount");
        depositAmmountJLabel.setForeground(black);
        depositAmmountJLabel.setHorizontalAlignment(JLabel.LEFT);
        contentPane.add(depositAmmountJLabel);

        depositAmmountJTextField = new JTextField();
        depositAmmountJTextField.setBounds(200, 50, 80, 20);
        depositAmmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        depositAmmountJTextField.setHorizontalAlignment(JTextField.CENTER);
        depositAmmountJTextField.setForeground(black);
        depositAmmountJTextField.setBackground(white);
        depositAmmountJTextField.setEditable(true);
        contentPane.add(depositAmmountJTextField);
       
        withdrawAmmountJLabel = new JLabel();
        withdrawAmmountJLabel.setBounds(50, 80, 120, 20);
        withdrawAmmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        withdrawAmmountJLabel.setText("Withdraw Ammount");
        withdrawAmmountJLabel.setForeground(black);
        withdrawAmmountJLabel.setHorizontalAlignment(JLabel.LEFT);
        contentPane.add(withdrawAmmountJLabel);

        withdrawAmmountJTextField = new JTextField();
        withdrawAmmountJTextField.setBounds(200, 80, 80, 20);
        withdrawAmmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        withdrawAmmountJTextField.setHorizontalAlignment(JTextField.CENTER);
        withdrawAmmountJTextField.setForeground(black);
        withdrawAmmountJTextField.setBackground(white);
        withdrawAmmountJTextField.setEditable(true);
        contentPane.add(withdrawAmmountJTextField);

        // outputs
       
        currentBalanceJLabel = new JLabel();
        currentBalanceJLabel.setBounds(50, 110, 100, 20);
        currentBalanceJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        currentBalanceJLabel.setText("Current Balance");
        currentBalanceJLabel.setForeground(black);
        currentBalanceJLabel.setHorizontalAlignment(JLabel.LEFT);
        contentPane.add(currentBalanceJLabel);

        currentBalanceJTextField = new JTextField();
        currentBalanceJTextField.setBounds(200, 110, 80, 20);
        currentBalanceJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        currentBalanceJTextField.setHorizontalAlignment(JTextField.CENTER);
        currentBalanceJTextField.setForeground(black);
        currentBalanceJTextField.setBackground(white);
        currentBalanceJTextField.setEditable(false);
        contentPane.add(currentBalanceJTextField);
       
        currentStatusJLabel = new JLabel();
        currentStatusJLabel.setBounds(50, 140, 100, 20);
        currentStatusJLabel.setFont(new Font("Default", Font.PLAIN, 12));
        currentStatusJLabel.setText("Account Status");
        currentStatusJLabel.setForeground(black);
        currentStatusJLabel.setHorizontalAlignment(JLabel.LEFT);
        contentPane.add(currentStatusJLabel);

        currentStatusJTextField = new JTextField();
        currentStatusJTextField.setBounds(200, 140, 120, 20);
        currentStatusJTextField.setFont(new Font("Default", Font.PLAIN, 12));
        currentStatusJTextField.setHorizontalAlignment(JTextField.CENTER);
        currentStatusJTextField.setForeground(black);
        currentStatusJTextField.setBackground(white);
        currentStatusJTextField.setEditable(false);
        contentPane.add(currentStatusJTextField);

        // controls
       
        enterJButton = new JButton();
        enterJButton.setBounds(20, 300, 100, 20);
        enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
        enterJButton.setText("Enter");
        enterJButton.setForeground(black);
        enterJButton.setBackground(white);
        contentPane.add(enterJButton);
        enterJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    enterJButtonActionPerformed(event);
                }
            }
        );

        clearJButton = new JButton();
        clearJButton.setBounds(150, 300, 100, 20);
        clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
        clearJButton.setText("Clear");
        clearJButton.setForeground(black);
        clearJButton.setBackground(white);
        contentPane.add(clearJButton);
        clearJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    clearJButtonActionPerformed(event);
                }
            }
        );

        closeJButton = new JButton();
        closeJButton.setBounds(280, 300, 100, 20);
        closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
        closeJButton.setText("Close");
        closeJButton.setForeground(black);
        closeJButton.setBackground(white);
        contentPane.add(closeJButton);
        closeJButton.addActionListener(

            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    closeJButtonActionPerformed(event);
                }
            }
        );

        setTitle("bankAccount");
        setSize(400, 400);
        setVisible(true);
    }

    // main method
   
    public  static void main(String[] args)
    {
        bankAccount application = new bankAccount();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    public void enterJButtonActionPerformed(ActionEvent event)
    {
        getDepositAmmount();
        getWithdrawAmount();
    }
   
    public void getDepositAmmount()
    {
        try
        {
            depositAmmount = Double.parseDouble(depositAmmountJTextField.getText());
            getWithdrawAmount();
         }
        catch(NumberFormatException exception)
        {
            JOptionPane.showMessageDialog(this,
            "Please enter a deposit amount!",
            "Number format Error", JOptionPane.ERROR_MESSAGE);
            depositAmmountJTextField.setText("");
            depositAmmountJTextField.requestFocusInWindow();
        }
    }
   
    public void getWithdrawAmount()
    {
        try
        {
            withdrawAmmount = Double.parseDouble(withdrawAmmountJTextField.getText());
            getCurrentBalance();
         }
        catch(NumberFormatException exception)
        {
            JOptionPane.showMessageDialog(this,
            "Please enter a withdraw amount!",
            "Number format Error", JOptionPane.ERROR_MESSAGE);
            withdrawAmmountJTextField.setText("");
            withdrawAmmountJTextField.requestFocusInWindow();
        }
    }
   
    /*public void getCurrentBalance()
    {
        currentBalance = withdrawAmmount + startingBalance;
        displayCurrentBalance();
    } // for withdraw*/
   
    public void getCurrentBalance()
    {
        currentBalance = startingBalance + depositAmmount - withdrawAmmount;
        displayCurrentBalance();
    }
   
    public void displayCurrentBalance()
    {
        decimalFormat = new DecimalFormat("$0.00");
        currentBalanceJTextField.setText("" + decimalFormat.format(currentBalance));
        getCurrentStatus();
    }
   
    public void getCurrentStatus()
    {
        if(currentBalance > 0 ) 
        {
            currentStatus = "Sufficient Funds";
        }
        else if (currentBalance <= 0) 
        {
            currentStatus = "Insufficient Funds";
        }
       
        currentStatusJTextField.setText("" + currentStatus);
        displayCurrentStatus(); 
    }
   
    public void displayCurrentStatus()
    {
        currentStatusJTextField.setText("" + currentStatus);
    }
   
    /*public void getWithdrawAmmount()
    {
        try
        {
            withdrawAmmount = Integer.parseInt(withdrawAmmountJTextField.getText());
            //getWithdrawAmmount();
         }
        catch(NumberFormatException exception)
        {
            JOptionPane.showMessageDialog(this,
            "Please enter a number!",
            "Number format Error", JOptionPane.ERROR_MESSAGE);
            withdrawAmmountJTextField.setText("");
            withdrawAmmountJTextField.requestFocusInWindow();
        }
    }*/
   
        public void clearJButtonActionPerformed(ActionEvent event)
    {
        depositAmmountJTextField.setText("");
        withdrawAmmountJTextField.setText("");
    }

    public void closeJButtonActionPerformed(ActionEvent event)
    {
        bankAccount.this.dispose();
    }
}
JAVA Program for creating a Sub-menu

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

public class creatingSubJMenuItem extends JFrame {

 //Initializing program components
 private JMenu menus;
 private JMenuBar bar;
 private JMenu mainItem[];
 private JMenuItem subItems[];

 //Setting up GUI
    public creatingSubJMenuItem() {
   
     //Setting up the Title of the Window
     super("Creating a Sub JMenuItem");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(350,200);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  //Constructing JMenu "File" with mnemonic 'F'
     menus = new JMenu("File");
     menus.setMnemonic('F');
   
     //Constructing main JMenu and add it in JMenu "File"
     mainItem = new JMenu[1];
   
     for(int count=0; count<mainItem.length; count++){
      mainItem[count] = new JMenu("Main Menu "+(count+1));
      menus.add(mainItem[count]); //Adding JMenu "mainItem" in the JMenu "File"
     }
   
     //Constructing JMenuItem "subItems" as a Sub Menu to the main JMenu
     subItems = new JMenuItem[5];
   
     for(int count=0; count<subItems.length; count++){
      subItems[count] = new JMenuItem("Sub Menu "+(count+1));
      mainItem[0].add(subItems[count]); //Adding JMenuItem "subItems" in the JMenu "mainItem"
     }
   
     //Constructing JMenuBar
     bar = new JMenuBar();
     bar.add(menus); //Adding the JMenu "File" in the JMenuBar
   
     //Setting up the JMenuBar in the container
     setJMenuBar(bar);

     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);

     /**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) {
     creatingSubJMenuItem csjmi = new creatingSubJMenuItem();
 }
}

After Execution

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);
        }
}