Java Assignment
Java Assignment
Java Assignment
Anagram
Write a program to find if the given two strings are anagram are not. An anagram of a string is a
string obtained by permuting the letters of a string. For example aaba and aaab are anagrams,
while abcd and deba are not.
PROGRAM:
package assi;
import java.util.Scanner;
/**
*
* @author jainil
*/
public class Assi {
public static void bubblesort(char [] elements)
{
for(int i=0;i<elements.length;i++)
for(int j=0;j<i;j++)
{
if(elements[i]<elements[j])
{
char temp=elements[i];
elements[i]=elements[j];
elements[j]=temp;
}
}
}
static boolean ana(String a,String b)
{
if(a.length()==b.length()){
char c[]=a.toCharArray();
char d[]=b.toCharArray();
bubblesort(c);
bubblesort(d);
boolean flag=true;
for(int i=0;i<a.length()&&flag==true;i++)
{
if(c[i]!=d[i])
{
flag=false;
}
}
if(flag)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public static void main(String[] args) {
}
OUTPUT:
2. Longest Common Subsequence
LCS Problem Statement: Given two sequences, find the length of longest subsequence present
in both of them. A subsequence is a sequence that appears in the same relative order, but not
necessarily contiguous. For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are
subsequences of “abcdefg”. So a string of length n has 2^n different possible subsequences.
It is a classic computer science problem, the basis of diff (a file comparison program that
outputs the differences between two files), and has applications in bioinformatics.
Examples:
LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3.
LCS for input Sequences “AGGTAB” and “GXTXAYB” is “GTAB” of length 4.
PROGRAM:
import java.util.*;
class Main{
static int max(int num1,int num2)
{
if(num1> num2)
return num1;
else
return num2;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n,i,j,count=0;
String str1,str2;
System.out.println("Enter String 1");
str1=sc.nextLine();
System.out.println("Enter String 2");
str2=sc.nextLine();
int row=str1.length();
int col=str2.length();
char LCS[]=new char[row+1];
int c[][]=new int[row+1][col+1];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==0||j==0)
{
c[i][j]=0;
}
}
}
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
if(str1.charAt(i-1)==str2.charAt(j-1))
{
c[i][j]=c[i-1][j-1]+1;
}
else
{
c[i][j]=max(c[i][j-1],c[i-1][j]);
}
}
}
for(i=0;i<=row;i++)
{
}
for(i=row,j=col;i>=0&&j>=1;)
{
if(c[i][j]!=c[i][j-1])
{
LCS[count]=str1.charAt(i-1);
j--;
i--;
count++;
}
else if(c[i][j]==c[i][j-1])
{
j--;
}
}
System.out.println(count);
}
}
OUTPUT:
3. Implement a program in reading and writing from a file using multiple threads.
PROGRAM:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
class Q{
String filename;
public Q(String filename) {
this.filename = filename;
}
int n;
boolean valueSet = false;
synchronized int get(){
if(!valueSet)
try{
wait();
}catch(InterruptedException ie){
}
System.out.println( "reading a file:" + this.filename );
try { BufferedReader bufferedReader =new BufferedReader(new FileReader(filename));
String line;
while((line = bufferedReader.readLine()) != null) {
System.out.println(line); }
bufferedReader.close();
}
catch (Exception ex) { }
valueSet = false;
notify();
return n;
}
synchronized void put(int n){
if(valueSet)
try{
wait();
}catch(InterruptedException ie){
}
System.out.println( "writing to a file:" + this.filename );
try {
Scanner sc = new Scanner(System.in);
File f = new File(filename);
BufferedWriter bufferedWriter =new BufferedWriter(new FileWriter(f,true));
String s;
s=sc.nextLine();
bufferedWriter.write(s+"\n");
bufferedWriter.close();
}
catch(Exception ex) {
}
valueSet = true;
notify();
}
}
}
}
}
public class Client {