Monday 30 September 2013

DNA,Client server

PROGRAM FOR DNA CONCEPT

PROGRAM:

      import java.util.*;
      import java.io.*;


         public class DNAdescending
          
           {

              public static void main(String args[])
           {
              final String DNAfilname="dnasequence.txt";
              final String outputFile="dnaout1.txt";
              DNAdescending dnaTest=new DNAdescending();
              try
              {
              File f=new File(outputFile);
BufferedOutputStream outputstream=new BufferedOutputStream(new FileOutputStream(f));
PrintWriter pw=new PrintWriter(outputstream);
              dnaTest.readfileAsstring(DNAfilname,pw);
                   pw.close();
              }
catch(Exception e)
{
e.printStackTrace();
}
}
private void readfileAsstring(String filename,PrintWriter pw)throws java.io.IOException
{
BufferedReader read=new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(filename)));
String strline;
int count,i,j,temp;
boolean flag;
String[][] line=new String[15][6];
while((strline=read.readLine())!=null)
{
String[] word=strline.split(" ");
count=0;
flag=false;
for(i=0;i<word.length;i++)
          {
if(word[i].contains("TATA"))
          {
count++;
flag=true;
          }
     }
if(flag)
              {
for(int k=0;k<6;k++)
              {
if(line[count][k]==null)
                   {
line[count][k]=strline;
System.out.println(line[count][k]);
break;
                   }
              }
}
}
for(i=4;i>0;i--)
for(j=5;j>=0;j--)
     {
if(line[i][j]!=null)
writeIntoFile(line[i][j],pw);
}
}
private void writeIntoFile(String str,PrintWriter pw)
{
try
{
pw.println(str);
System.out.println("The dna pattern has been written");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


OUTPUT:
Z:\javapgms>javac DNAdescending.java

Z:\javapgms>java DNAdescending
TATAHH TATAHH TATFH
TATASG TATAGG TATAG
TATGGG TATAGG TAGGG
The dna pattern has been written
The dna pattern has been written

The dna pattern has been written

Thursday 26 September 2013

Client Server Communication

CLIENT SERVER COMMUNICATION:
EchoClient.java:

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

public class EchoClient extends JFrame implements ActionListener
{
    JTextField tf=new JTextField(15);
    JTextField tf2=new JTextField(15);
    BufferedReader in;
    PrintWriter out;
    JButton b;
    public EchoClient()
    {
        try
        {
            JFrame f=new JFrame();
            f.setSize(100,100);
            Socket socket=new Socket("localhost",8008);
            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
            f.setTitle("client"+socket);
            tf.setText(" ");
            tf2.setText(" ");
            JPanel p=new JPanel();
            JLabel l=new JLabel("enter the text to sent to the server");
            JLabel l2=new JLabel("Text echoed from server in uppercase");
            b=new JButton("send");
            p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
            p.add(l);
            p.add(tf);
            p.add(l2);
            p.add(tf2);
            p.add(b);
            f.add(p);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            b.addActionListener(this);
            out.flush();
            while(true)
            {
                String str=in.readLine();
                tf2.setText(str);
            }
        }catch(Exception e)
        {}
    }
public void actionPerformed(ActionEvent event)
{
    String str1=tf.getText();
    System.out.println("sending "+str1);
    out.println(str1.toString());
    out.flush();
    tf.setText(" ");
}
public static void main(String args[])
{
    EchoClient f=new EchoClient();
}
}

EchoServer.java:
import java.io.*;
import java.net.*;
public class EchoServer
{
public static void main(String args[])throws Exception
{
    try
    {
        ServerSocket s=new ServerSocket(8008);
        while(true)
        {
            Socket incoming=s.accept();
            new ClientHandler(incoming).start();
        }
    }catch(Exception e)
    {}
    }
}
class ClientHandler extends Thread
{
    Socket incoming;
    ClientHandler(Socket incoming)
    {
        this.incoming=incoming;
    }
    public void run()
    {
        try
        {
            BufferedReader in=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            PrintWriter out=new PrintWriter(new PrintWriter(new OutputStreamWriter(incoming.getOutputStream())));
            out.flush();
            while(true)
            {
                String str=in.readLine();
                System.out.println("receiving from "+incoming+" "+str);
                out.println(str.toUpperCase());
                out.flush();
                if(str.trim().endsWith("BYE"))
                    break;
            }
            incoming.close();
        }catch(Exception e)
        {}
    }
}

CLIENT SEVER COMMUNICATION

CLIENT SERVER COMMUNICATION:
EchoClient.java:
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;

public class EchoClient extends JFrame implements ActionListener
{
    JTextField tf=new JTextField(15);
    JTextField tf2=new JTextField(15);
    BufferedReader in;
    PrintWriter out;
    JButton b;
    public EchoClient()
    {
        try
        {
            JFrame f=new JFrame();
            f.setSize(100,100);
            Socket socket=new Socket("localhost",8008);
            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
            f.setTitle("client"+socket);
            tf.setText(" ");
            tf2.setText(" ");
            JPanel p=new JPanel();
            JLabel l=new JLabel("enter the text to sent to the server");
            JLabel l2=new JLabel("Text echoed from server in uppercase");
            b=new JButton("send");
            p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
            p.add(l);
            p.add(tf);
            p.add(l2);
            p.add(tf2);
            p.add(b);
            f.add(p);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            b.addActionListener(this);
            out.flush();
            while(true)
            {
                String str=in.readLine();
                tf2.setText(str);
            }
        }catch(Exception e)
        {}
    }
public void actionPerformed(ActionEvent event)
{
    String str1=tf.getText();
    System.out.println("sending "+str1);
    out.println(str1.toString());
    out.flush();
    tf.setText(" ");
}
public static void main(String args[])
{
    EchoClient f=new EchoClient();
}
}

EchoServer.java:
import java.io.*;
import java.net.*;
public class EchoServer
{
public static void main(String args[])throws Exception
{
    try
    {
        ServerSocket s=new ServerSocket(8008);
        while(true)
        {
            Socket incoming=s.accept();
            new ClientHandler(incoming).start();
        }
    }catch(Exception e)
    {}
    }
}
class ClientHandler extends Thread
{
    Socket incoming;
    ClientHandler(Socket incoming)
    {
        this.incoming=incoming;
    }
    public void run()
    {
        try
        {
            BufferedReader in=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            PrintWriter out=new PrintWriter(new PrintWriter(new OutputStreamWriter(incoming.getOutputStream())));
            out.flush();
            while(true)
            {
                String str=in.readLine();
                System.out.println("receiving from "+incoming+" "+str);
                out.println(str.toUpperCase());
                out.flush();
                if(str.trim().endsWith("BYE"))
                    break;
            }
            incoming.close();
        }catch(Exception e)
        {}
    }
}

Friday 13 September 2013

Infosys Aspiration 2020- Practice Session

Question 1:

A string (example "I am writing an email") is entered through the keyboard, write a program in C to get its reverse in a column as output i.e.:
email
an
writing
am
I

Question 2:

A vampire number has an even number of digits and is formed by multiplying a pair of numbers containing half the number of digits of the result. The digits are taken from the original number in any order. Pairs of trailing zeroes are not allowed. Example include:
1260 = 21 * 60
1827 = 21 * 87
2187 = 27 * 81

Question 3:

Anagram is a  word that is spelled with the exact same letters as another word. Example: RIDES is an anagram of SIRED and vice versa.

Question 4:
We will call a positive integer "nasty" if it has at least two pairs of positive integer factors such that the difference of one pair equals the sum of the other pair.For example, 6 is nasty since 6 x 1 = 6, 2 x 3 = 6, 6 - 1 = 2 + 3; and 24 is also nasty since 12 - 2 = 6 + 4.Debug a program which accepts as input a list of positive integers and determines if each one is nasty or not.