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.

Saturday, 10 August 2013

Lab Programs & Viva Questions:

1.Design a Java interface for ADT Stack. Develop two different classes that implement this interface, one using array and the other using linked-list. Provide necessary exception handling in both the implementations.

ALGORITHM
STEP 1: Create an interface which consists of three methods namely PUSH, POP and
    DISPLAY
STEP 2: Create a class which implements the above interface to implement the concept
              of stack through Array
STEP 3: Define all the methods of the interface to push any element, to pop the top
               element and to display the elements present in the stack.
STEP 4: Create another class which implements the same interface to implement the
                concept of stack through linked list.
STEP 5: Repeat STEP 4 for the above said class also.
STEP 6: In the main class, get the choice from the user to choose whether array
                implementation or linked list implementation of the stack.
STEP 7: Call the methods appropriately according to the choices made by the user in the
                previous step.
STEP 8: Repeat step 6 and step 7 until the user stops his/her execution

2.Implement a java program to implement the reflection concept for java.lang.String class
ALGORITHM:
STEP 1: Import all necessary packages.
STEP 2: create a class “TestreReflect”
STEP 3: In the main function use class Class  and use the Class class method forName()
               for declaring the class.
STEP 4: Use the Constructors[],Fields[] and Methods[] class for getting information
               about the class.
STEP 5: Print all information about the class.
3.Consider a class person with attributes firstname,lastname and personid.Implement a java program to create and clone instances  of the Person class.
ALGORITHM:
STEP 1: Import all necessary packages.
STEP 2: create a class “CloneTest” which implements Clonable interface
STEP 3: Using parameterized constructor get the input values specified in the main().
STEP 4: Use default method
                Public Object clone()
               To return the superv class clone and use exception handling mechanism.
STEP 5: In the main function,get the input values using scanner class
STEP 6: Create object for CloneTest class and call methods for it.
STEP 7: Use typecasting, and create another object for the same class.
STEP 8:Display the original and cloned variable value.

4.Develop a java interface for implement the multiple inheritance for student mark analysis system.(Define 2 interface-one interface method for reading student basic information(name{String},rollno{int},register number{long},phone num{long},second interface method for reading 5 sub marks{use array} and calculate total,avg).


viva questions:

1.1.What is interface and mention its use
2.Define marker interface.
3.Write down the syntax for defining interface?
4.What is meant by reflection?
5.What is object cloning? Why it is needed? 
6.why do we need static members and how to access them? 
7.What is a string buffer class and how does it differs from string class?
8.How to create one dimensional array?
9.What is dynamic binding?
10.What is the difference between static and non-static variables?

Tuesday, 30 July 2013

SCJP QUESTIONS

SUN CERTIFIED JAVA PROGRAMMER

Question - 1
What is the output for the below code ?
1. public class A {
2. int add(int i, int j){
3. return i+j;
4. }
5.}
6.public class B extends A{
7. public static void main(String argv[]){
8. short s = 9;
9. System.out.println(add(s,6));
10. }
11.}

Options are 
A.Compile fail due to error on line no 2
B.Compile fail due to error on line no 9
C.Compile fail due to error on line no 8
D.15

Question - 2
What is the output for the below code ?
public class A {
int k;
 boolean istrue;
 static int p;
public void printValue() {
System.out.print(k);
System.out.print(istrue);
System.out.print(p);
}
}
public class Test{
 public static void main(String argv[]){
A a = new A();
a.printValue();
 }
}
Options are 
A.0 false 0
B.0 true 0
C.0 0 0
D.Compile error - static variable must be initialized before use


Question - 3
What is the output for the below code ?
public class Test{
int _$;
int $7;
int do;
 public static void main(String argv[]){
 Test test = new Test();
 test.$7=7;
 test.do=9;
 System.out.println(test.$7);
 System.out.println(test.do);
 System.out.println(test._$);
 }
}
Options are
A.7 9 0
B.7 0 0
C.Compile error - $7 is not valid identifier.
D.Compile error - do is not valid identifier

Question -4:
What is the output for the below code ?
package com;
class Animal {
 public void printName(){
 System.out.println("Animal");
 }
}

package exam;
import com.Animal;
public class Cat extends Animal {
 public void printName(){
 System.out.println("Cat");
 }
}

package exam;
import com.Animal;
public class Test {
public static void main(String[] args){
 Animal a = new Cat();
 a.printName();
}
}
Options are 
A.Animal
B.Cat
C.Animal Cat
D.Compile Error

Question No-5:
What is the output for the below code ?

public class A {
int i = 10;
public void printValue() {System.out.println("Value-A");
};
}
public class B extends A{
int i = 12;
public void printValue() {
System.out.print("Value-B");
}
}
public class Test{
 public static void main(String argv[]){
 A a = new B();
 a.printValue();
 System.out.println(a.i);
 }
}
Options are 
A.Value-B 11
B.Value-B 10
C.Value-A 10
D.Value-A 11

Monday, 29 July 2013

JAVA LAB PROGRAM ALGORITHM:

1. Develop a Java package with simple Stack and Queue classes.  Use JavaDoc comments for documentation

ALGORITHM:
step 1:Define package pack1 with the keyword package.
step 2:Define the class ArrayQueue with the integers.
step 3:Using a 1 argument constructor,the max size of the queue is assigned.
step 4:Define the method insert() with an item in object type as argument to insert an item.
step 5:Define the method remove() to delete an item from the queue.
step 6:Define the method peek() to return the item which is at the top of stack.
Step 7:Define the method displayAll() to display the contents of the queue.
step 8:Terminate the class .Create an another package pack1 using the keyword package.
step 9:Define the class ArrayStack with an object array'a' and an integer top as datamembers.
step 10:Using a one argument constructor define the size of the array.
step 11:Define the method push() to push an item into the stack.
step 12:Define the method pop() to delete an item from the stack.
step 13:Define the method peek() to return the item at the top of the stack.
step 14:Terminate the class.
step 15:Import the package pack1,define the class StackQueueDemo.
step 16:Define the main() in the class.
Create objects for the classes ArrayStack and ArrayQueue and access the methods using the objects.
step 17:Display the result.
step 18: terminte the main() and terminate the class.


2. Design a Date class similar to the one provided in the java.util package

ALGORITHM
step 1:Define the class MyDate with members date,month,year,hour,min,sec.
step 2:USing a default constructor,set the values using the predefined Dateclass.
step 3:use parameterised constructors to set the values for the members.
step 4:Define the methods getYear(),getMonth(),getDate(),  to return the year,month,date .
step 5:Define the methods setYear(),setMonth(),setDate. to set the values for year,month,date,.
step 6:Define the method after() with the return type boolean to check whether the given date is after the current date.
step 7:Define the method before() with the return type boolean to check whether the given date is before the current date.
step 8:Define the method compareTo() with the return type integer to compare two dates.it will return 0 if both are same,will return 1 if date is after currrent date else it returns -1.
step 9:terminate the class.
step 10:Define the datetest and define the main() in it.
step 11:Create many objects for the class MyDate and access the methods of the class using different objects.
step 12:Display the result.
step 13:terminate the main() and class.