Wednesday, 14 September 2011

Write a Java program to read a file that contains DNA sequences of arbitrary length one per line (note that each DNA sequence is just a String). Your program should sort the sequences in descending order with respect to the number of 'TATA' subsequences present. Finally write the sequences in sorted order into another file


 PROGRAM                       

import java.io.*;
class files
{
String s[]=new String[100];
String str="",m,t;
int x,size=0,i,tem,j=0,chg;
int a[]=new int[100];
int b[]=new int[100];
int c[]=new int[100];
files(String s1,String s2)throws IOException
{
File r=new File(s1);
File w=new File(s2);
FileInputStream fin=new FileInputStream(r);
FileOutputStream fout=new FileOutputStream(w);
do{
i=fin.read();
if(i!=-1)
{
str=str+(char)i;
if((char)i=='\n')
{
s[j]=str;
j++;
size++;
str="";
}
}
}
while(i!=-1);
for(int l=0;l<size;l++)
{
if(s[l]==null)
break;
m=s[l];
t="tata";
b[l]=a[l]=search(m,t);
}
for(int p=0;p<size;p++)
{
for(int q=0;q<size;q++)
{
if(b[q]<b[q+1])
{
tem=b[q];
b[q]=b[q+1];
b[q+1]=tem;
}
}
}
for(int h=0;h<size;h++)
{
x=b[h];
c[h]=linear(x,a,size);
}
for(int y=0;y<size;y++)
{
chg=c[y];
byte buf[]=s[chg].getBytes();
fout.write(buf);
}
}
static int linear(int x,int a[],int size)
{
int m,d,item=x,siz=size;
for(d=0;d<=siz;d++)
{
if(item==a[d])
{
m=d;
break;
}
}
return d;
}
static int search(String m,String c)
{
int ch,ma,inc=0;
String pat,main,check;
pat=main=m;
check=c;
do{
pat=main.substring(0,4);
if(pat.equalsIgnoreCase(check))
++inc;
main=main.substring(1);
ch=check.length();
ma=main.length();
}
while(ch<ma);
return inc;
}
}
class filehand
{
public static void main(String a[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
String s1,s2;
System.out.println("enter the read file name :");
s1=in.readLine();
System.out.println("enter the write file name:");
s2=in.readLine();
files f=new files(s1,s2);
System.out.println("successfully writed");
}
}

OUTPUT:

C:\j2sdk1.4.0\bin>javac filehand.java
C:\j2sdk1.4.0\bin>java filehand

enter the read file name :
input.txt
enter the write file name:
output.txt
successfully writed



“Input.txt”

jfjksdf tata fji
fjksdfi tatt fg tata taata tata
jfijsdf tata tjkltj tatta tata tata





“Output.txt”

jfijsdf tata tjkltj tatta tata tata
fjksdfi tatt fg tata taata tata
jfjksdf tata fji

Tuesday, 13 September 2011

Login form in java

Here is the code of NextPage.java

import javax.swing.*;
import java.awt.*;

class NextPage extends JFrame
{
NextPage()
  {
  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  setTitle("Welcome");
       setSize(400, 200);
   
}
 }
Here is the code of LoginDemo.java

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

class Login extends JFrame implements ActionListener
{
 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 final JTextField  text1,text2;
Login()
{
   label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);


label2 = new JLabel();
label2.setText("Password:");
   text2 = new JPasswordField(15);
   //this.setLayout(new BorderLayout());

SUBMIT=new JButton("SUBMIT");

        panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
   add(panel,BorderLayout.CENTER);
        SUBMIT.addActionListener(this);
        setTitle("LOGIN FORM");
}
   public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
        if (value1.equals("roseindia") && value2.equals("roseindia")) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
        page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
 class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
output:






Bar Chart in Java


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

  public class SimpleBarChart extends JPanel {
  private double[] value;
  private String[] languages;
  private String title;

  public SimpleBarChart(double[] val, String[] lang, String t) {
  languages = lang;
  value = val;
  title = t;
  }
  public void paintComponent(Graphics graphics) {
  super.paintComponent(graphics);
  if (value == null || value.length == 0)
  return;
  double minValue = 0;
  double maxValue = 0;
  for (int i = 0; i < value.length; i++) {
  if (minValue > value[i])
  minValue = value[i];
  if (maxValue < value[i])
  maxValue = value[i];
  }
  Dimension dim = getSize();
  int clientWidth = dim.width;
  int clientHeight = dim.height;
  int barWidth = clientWidth / value.length;
  Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
  FontMetrics titleFontMetrics = graphics.getFontMetrics(titleFont);
  Font labelFont = new Font("Book Antiqua", Font.PLAIN, 10);
  FontMetrics labelFontMetrics = graphics.getFontMetrics(labelFont);
  int titleWidth = titleFontMetrics.stringWidth(title);
  int q = titleFontMetrics.getAscent();
  int p = (clientWidth - titleWidth) / 2;
  graphics.setFont(titleFont);
  graphics.drawString(title, p, q);
  int top = titleFontMetrics.getHeight();
  int bottom = labelFontMetrics.getHeight();
  if (maxValue == minValue)
  return;
  double scale = (clientHeight - top - bottom) / (maxValue - minValue);
  q = clientHeight - labelFontMetrics.getDescent();
  graphics.setFont(labelFont);
  for (int j = 0; j < value.length; j++) {
  int valueP = j * barWidth + 1;
  int valueQ = top;
  int height = (int) (value[j] * scale);
  if (value[j] >= 0)
  valueQ += (int) ((maxValue - value[j]) * scale);
  else {
  valueQ += (int) (maxValue * scale);
  height = -height;
  }
  graphics.setColor(Color.blue);
  graphics.fillRect(valueP, valueQ, barWidth - 2, height);
  graphics.setColor(Color.black);
  graphics.drawRect(valueP, valueQ, barWidth - 2, height);
  int labelWidth = labelFontMetrics.stringWidth(languages[j]);
  p = j * barWidth + (barWidth - labelWidth) / 2;
  graphics.drawString(languages[j], p, q);
  }
  }
 public static void main(String[] args) {
  JFrame frame = new JFrame();
  frame.setSize(350, 300);
  double[] value= new double[5];
  String[] languages = new String[5];
  value[0] = 1;
  languages[0] = "Visual Basic";

  value[1] = 2;
  languages[1] = "PHP";

  value[2] = 3;
  languages[2] = "C++";

  value[3] = 4;
  languages[3] = "C";

  value[4] = 5;
  languages[4] = "Java";
  frame.getContentPane().add(new SimpleBarChart(value, languages,
    "Programming Languages"));

  WindowListener winListener = new WindowAdapter() {
  public void windowClosing(WindowEvent event) {
  System.exit(0);
  }
  };
  frame.addWindowListener(winListener);
  frame.setVisible(true);
  }
}
output:

Chess Application In Java Swing


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ChessGameDemo extends JFrame implements MouseListener, MouseMotionListener {
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;

    public ChessGameDemo(){
        Dimension boardSize = new Dimension(600, 600);

        //  Use a Layered Pane for this this application
         layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);
        layeredPane.addMouseListener(this);
        layeredPane.addMouseMotionListener(this);

        //Add a chess board to the Layered Pane

        chessBoard = new JPanel();
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
        chessBoard.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel( new BorderLayout() );
            chessBoard.add( square );

            int row = (i / 8) % 2;
            if (row == 0)
                square.setBackground( i % 2 == 0 ? Color.blue : Color.white );
            else
                square.setBackground( i % 2 == 0 ? Color.white : Color.blue );
        }

        //Add a few pieces to the board

        JLabel piece = new JLabel( new ImageIcon("/home/vinod/amarexamples/chess.jpg") );
        JPanel panel = (JPanel)chessBoard.getComponent(0);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/chess1.jpg"));
        panel = (JPanel)chessBoard.getComponent(15);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/king.jpg"));
        panel = (JPanel)chessBoard.getComponent(16);
        panel.add(piece);
  piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/camel.jpg"));
        panel = (JPanel)chessBoard.getComponent(20);
        panel.add(piece);

    }

    public void mousePressed(MouseEvent e){
        chessPiece = null;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

        if (c instanceof JPanel)
  return;

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel)c;
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    }
 
    //Move the chess piece around
   
    public void mouseDragged(MouseEvent me) {
        if (chessPiece == null) return;
         chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
     }
   
  //Drop the chess piece back onto the chess board

    public void mouseReleased(MouseEvent e) {
        if(chessPiece == null) return;

        chessPiece.setVisible(false);
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

        if (c instanceof JLabel){
            Container parent = c.getParent();
            parent.remove(0);
            parent.add( chessPiece );
        }
        else {
            Container parent = (Container)c;
            parent.add( chessPiece );
        }

        chessPiece.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
 
    }
    public void mouseMoved(MouseEvent e) {
   }
    public void mouseEntered(MouseEvent e){
 
    }
    public void mouseExited(MouseEvent e) {
 
    }

    public static void main(String[] args) {
        JFrame frame = new ChessGameDemo();
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}
 output:

Crate a Popup Menu in Java




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


public class PopUpMenu{
JPopupMenu Pmenu;
JMenuItem menuItem;
public static void main(String[] args) {
PopUpMenu p = new PopUpMenu();
}


public PopUpMenu(){
JFrame frame = new JFrame("Creating a Popup Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pmenu = new JPopupMenu();
menuItem = new JMenuItem("Cut");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Copy");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Paste");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Delete");
Pmenu.add(menuItem);
menuItem = new JMenuItem("Undo");
Pmenu.add(menuItem);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){}
});
frame.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
if(me.isPopupTrigger()){
Pmenu.show(me.getComponent(), me.getX(), me.getY());
}
}
public void mouseReleased(MouseEvent Me){
if(Me.isPopupTrigger()){
Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
}
}
});
frame.setSize(400,400);
frame.setVisible(true);
}
}
output:


Create a ToolBar in Java


import javax.swing.*;
import java.awt.*;

public class CreateToolbar{
public static void main(String[] args) {
JFrame frame = new JFrame("Create a toolbar Which have three buttons Such as: Cut, Copy, Paste");
JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton cutbutton = new JButton(new ImageIcon("cut.gif"));
toolbar.add(cutbutton);
JButton copybutton = new JButton(new ImageIcon("copy.gif"));
toolbar.add(copybutton);
JButton pastebutton = new JButton(new ImageIcon("paste.gif"));
toolbar.add(pastebutton);
frame.getContentPane().add(toolbar,BorderLayout.NORTH);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setVisible(true);
}
}

Wednesday, 7 September 2011

create a checkbox


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
public class SampleFrame2
{
public static void main(String args[])
{
 MyFrame F=new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener
{

   JPanel p=new JPanel();
    JCheckBox a=new JCheckBox ("YELLOW");
    JCheckBox b=new JCheckBox("BLUE");
    JCheckBox c=new JCheckBox("RED");
    JCheckBox d=new JCheckBox("GREEN");

MyFrame()
{
    p.add(a);
    p.add(b);
    p.add(c);
    p.add(d);
    a.addActionListener(this);
    b.addActionListener(this);
    c.addActionListener(this);
    d.addActionListener(this);
    add(p);
    setVisible(true);

}

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==a)
    {
        p.setBackground(Color.yellow);
    }
    if(e.getSource()==b)
    {
        p.setBackground(Color.blue);
    }
      if(e.getSource()==c)
      {
           p.setBackground(Color.red);
    }
    if(e.getSource()==d)
    {
     p.setBackground(Color.green);
}
}
}

output: