Saturday, June 2, 2012

String Permutations


import java.util.ArrayList;

public class StringPermutation {

public static ArrayList<String> PermutationFinder(String s) {

ArrayList<String> perm = new ArrayList<String>();

if (s == null) { // error case

    return null;

} else if (s.length() == 0)

{

    perm.add(""); // initial

    return perm;

}

char initial = s.charAt(0); // first character

String rem = s.substring(1); // Full string without first character

ArrayList<String> words = PermutationFinder(rem);

for (String str : words)

{

    for (int i = 0; i <= str.length(); i++)

    {

        perm.add(charinsert(str, initial, i));

    }

}

return perm;

}

public static String charinsert(String str, char c, int j) {

String begin = str.substring(0, j);

String end = str.substring(j);

return begin + c + end;

}

public static  void main(String[] args) {

    String s = "CODE";

ArrayList<String> value = PermutationFinder(s);

System.out.println("\nThe Permutations are : \n" + value);

}

}

Friday, June 1, 2012

Exceptions and return types in overriding.


import java.io.IOException;
import java.sql.*;
public class Test1 {
public Test4 myMetohd1() throws Exception
{
return null;
}

}

class Test2 extends Test1 {
public Test5  myMetohd1() throws SQLException, IOException
{
return null;
}
}

class Test3 extends Test1 {
public Test5 myMetohd1()
{
return null;
}
}

class Test4
{
}

class Test5 extends Test4
{
}