Friday 2 January 2015

Simple but Strong Program on Indian Flag in JAVA using Applet

import java.awt.*;
import java.applet.*;
/*<applet code=Flag.class width=300 height=200></applet>*/
public class Flag extends Applet {
 public void paint(Graphics g) {
  g.setColor(Color.orange);
  g.fillRect(20,10,270,30);
  g.setColor(Color.white);
  g.fillRect(20,40,270,30);
  g.setColor(Color.green);
  g.fillRect(20,70,270,30);
  g.setColor(Color.blue);
  g.drawOval(135,42,25,25);
  g.drawLine(148,42,148,67);
  g.drawLine(136,54,161,54);
  g.drawLine(140,46,156,62);
  g.drawLine(154,46,140,64);
  g.setColor(Color.black);
  g.fillRect(15,10,5,200);
 }
}

Oldest and Best Visual Basic

Visual Basic - Check Box, Option Button and Frame


Step1: Place One Label, one Text Box, Two Frames, Two Option Buttons, Three Check Boxes and a Command Button on the Form1
Step2: Rename the Control Caption in the Property Window as follows
 
Form1 -> Option & Check
Label1 -> Enter Text Here:
Frame1 -> Case
Frame2 -> Effects
Option1 -> Upper
Option2 -> Lower
Check1 -> Strike Through
Check2 -> Under Line
Check3 -> Bold
Command1 -> Exit
 
Change the Text1 Properties as multiline True and ScrollBars as Both
Enter the following codes in the corresponding controls
Private Sub Check1_Click()
If Check1.Value = 1 Then
Text1.Font.Strikethrough = True
Else
Text1.Font.Strikethrough = False
End If
End Sub

Private Sub Check2_Click()
If Check2.Value = 1 Then
Text1.Font.Underline = True
Else
Text1.Font.Underline = False
End If
End Sub

Private Sub Check3_Click()
If Check3.Value = 1 Then
Text1.Font.Bold = True
Else
Text1.Font.Bold = False
End If
End Sub

Private Sub Command1_Click()
End
End Sub

Private Sub Form_Load()
Option2.Value = True
End Sub

Private Sub Option1_Click()
If Option1.Value = True Then
 Text1.Text = UCase(Text1)
 End If
 End Sub

Private Sub Option2_Click()
If Option2.Value = True Then
Text1.Text = LCase(Text1)
End If
End Sub

 Step3: Run the Application by pressing F5

DATA STRUCTURE 

Java program for Linked list (Lists)


import java.util.*;

public class LinkedListDemo{
public static void main(String[] args){
LinkedList link=new LinkedList();
link.add("a");
link.add("b");
link.add(new Integer(10));
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.addFirst(new Integer(20));
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.addLast("c");
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.add(2,"j");
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.add(1,"t");
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());

link.remove(3);
System.out.println("The contents of array is" + link);
System.out.println("The size of an linkedlist is" + link.size());
}
}

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>_