Wednesday, 28 May 2014

concept of factory method and Singleton process

Singleton :

                A singleton class is one which allows  us to create only one object for JVM.
                we can use it to create a connection pool.

class Stest {
    private static Stest st;  // private variable

    private Stest() {       // private constructor
        System.out.println("OBJECT CREATED FIRST TIME");
    }

    public static Stest create()  //Factory method
    {
        if (st == null)
        {
            st = new Stest();
        } else
                {
                System.out.println("OBJECT ALREADY CREATED");
            }
       
        return st;
    }

    public static void main(String[] args) {
        Stest st1 = Stest.create();
        Stest st2 = Stest.create();
        Stest st3 = Stest.create();
        if ((st1 == st2) && (st2 == st3) && (st3 == st1)) {
            System.out.println("ALL OBJECTS ARE SAME");
        } else {
            System.out.println("ALL OBJECTS ARE NOT SAME");
        }
    }
}

OUTPUT :

OBJECT CREATED FIRST TIME
OBJECT ALREADY CREATED
OBJECT ALREADY CREATED
ALL OBJECTS ARE SAME

Tuesday, 27 May 2014

Algorithm to find if linked list contains loops or cycles

1) Use two pointers fast and slow
2) Move fast two nodes and slow one node in each iteration
3) If fast and slow meet then linked list contains cycle
4) if fast points to null or fast.next points to null then linked list is not cyclic



boolean hasLoop(Node first) {

    if(first == null) // list does not exist..so no loop either.
        return false;

    Node slow, fast; // create two references.

    slow = fast = first; // make both refer to the start of the list.

    while(true) {

        slow = slow.next;          // 1 hop.

        if(fast.next != null)
            fast = fast.next.next; // 2 hops.
        else
            return false;          // next node null => no loop.

        if(slow == null || fast == null) // if either hits null..no loop.
            return false;

        if(slow == fast) // if the two ever meet...we must have a loop.
            return true;
    }
}

Monday, 26 May 2014

When to use abstract class

public abstract Animal
{
   public void eat(Food food)
   {
        // do something with food.... 
   }

   public void sleep(int hours)
   {
        try
 {
  // 1000 milliseconds * 60 seconds * 60 minutes * hours
  Thread.sleep ( 1000 * 60 * 60 * hours);
 }
 catch (InterruptedException ie) { /* ignore */ } 
   }

   public abstract void makeNoise();
}
Note that the abstract keyword is used to denote both an abstract method, and an abstract class. Now, any animal that wants to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.
public Dog extends Animal
{
   public void makeNoise() { System.out.println ("Bark! Bark!"); }
}

public Cow extends Animal
{
   public void makeNoise() { System.out.println ("Moo! Moo!"); }
}
Now you may be wondering why not declare an abstract class as an interface, and have the Dog and Cow implement the interface. Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces - an interface cannot provide any method implementations.

Saturday, 24 May 2014

How to make unhidden folder using java


Note : Keep folder jill .

public class hidedir

    public static void main(String[] args) throws Exception
    {
        String cmd="attrib -h C:/Users/Flutebala/Desktop/jill";
        Runtime.getRuntime().exec(cmd);

           }
}

How to show files list inside the folder

 NOTE : Keep folder jill and inside jill keep files .

public class ListFiles
{

 public static void main(String[] args)
{

  // Directory path here
  String path = "C:/Users/Flutebala/Desktop/jill";

  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles();

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

   if (listOfFiles[i].isFile())
   {
   files = listOfFiles[i].getName();
   System.out.println(files);
      }
  }
}
}

                                                           OR

public class ListFiles {

    public static void main(String[] args) {

        // Directory path here
        String path = "C:/Users/Flutebala/Desktop/jill";

        File f = new File(path);
        File[] listOfFiles = f.listFiles();
      
        for(File f1 : listOfFiles)
        {
            System.out.println(f1.getAbsolutePath());
        }
    }
}

How to make hidden folder using java

Note : Keep folder jill .

public class hidedir

    public static void main(String[] args) throws Exception
    {
        String cmd="attrib +h C:/Users/Flutebala/Desktop/jill";
        Runtime.getRuntime().exec(cmd);

           }
}

Sunday, 18 May 2014

Duplicate words in string

public class DuplicateWordSearcher {
@SuppressWarnings("unchecked")
public static void main(String[] args) {

String text = "java android java java android";

List<String> list = Arrays.asList(text.split(" "));

Set<String> uniqueWords = new HashSet<String>(list);
for (String word : uniqueWords)
{
System.out.println(word + ": " + Collections.frequency(list, word));
}
}
}


Output :
   android: 2
   java: 3