Monday, 28 October 2013

linked list in java without using collection framework.


public class TestLinkedList {

/**
* @param args
*/
public static void main(String[] args) {

int[] data = { 10, 20, 30, 15, 40, 66 };
Object[][] linkedList = new Object[1][2];
createLinkedList(data, linkedList);
printLinkedList(linkedList);

}

public static void createLinkedList(int[] data, Object[][] linkedList) {
Object node[][] = null;
Object newnode[][] = null;
node = new Object[1][2];
linkedList[0][1] = node;
for (int i = 0; i < data.length; i++) {
node[0][0] = new Integer(data[i]);
newnode = new Object[1][2];
node[0][1] = newnode;
node = newnode;
}
node = null;
newnode = null;
}

public static void printLinkedList(Object[][] linkedList) {

Object node[][] = null;
node = linkedList;
node = (Object[][]) node[0][1];
while (node[0][1] != null) {
System.out.print("  " + node[0][0] + " --->");
node = (Object[][]) node[0][1];

}
System.out.print("null");

}
}

Monday, 14 October 2013

It is true ?that you can't change a value in the final variable. Well it is partially correct. Lets have a look at the following code
1 public static final int PI=3.141;
 This is how we create a constant in Java, It is pretty much true that you can't modify the value PI no matter how.
 But saying "You can't change the vlaue of a final variable" is wrong, so very wrong when the variable type is not a primitive type .
Have a look at the following code.
 public class Test {
         public static final State STATE=new State();
         public static void main(String args[]){
                 System.out.println("Status: "+STATE.getStatus());
                 STATE.setStatus("But,See I got changed");
                 System.out.println("Status after reassignment:"+STATE.getStatus());
         }
 }
class State {
         private String status="My Object is assigned to a final variable,And I think I can't change";
 public String getStatus() {
         return status;
 }
 public void setStatus(String status) {
                this.status = status;
         }
}
 In the above class Test, we have a final variable STATE, which is assigned an object reference of State class. And then in main method we are trying to modify the status field of State object which is assigned to a STATE constant(technically).The State class is nothing special , we have a instance field status as type String with some value assigned.And a getter/setter to access the private field state. Now execute this program and you will see the result yourself. Bottom line: If a final variable is a reference type then the object it is referring to is still mutable,meaning that we can modify the states(with a condition that the class/object is mutable) A final variable can't be reassigned once assigned no matter if the variable is primitive type of reference type. But if a final variable of reference type doesn't restrict you to modify the state of the reference type ( applicable only if the reference type i.e the class allows modification).

Sunday, 13 October 2013

Abstraction vs Generalization vs Realization vs Dependency


Abstraction

Abstraction is specifying the framework and hiding the implementation level information. Concreteness will be built on top of the abstraction. It gives you a blueprint to follow to while implementing the details. Abstraction reduces the complexity by hiding low level details.
Example: A wire frame model of a car.

Generalization

Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behaviour are used from the specializtion to the generalized class. At a very broader level you can understand this as inheritance. Why I take the term inheritance is, you can relate this term very well. Generalization is also called a “Is-a” relationship.
Example: Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore here the relationship between student and person, similarly faculty and person is generalization.

Realization

Realization is a relationship between the blueprint class and the object containing its respective implementation level details. This object is said to realize the blueprint class. In other words, you can understand this as the relationship between the interface and the implementing class.
Example: A particular model of a car ‘GTB Fiorano’ that implements the blueprint of a car realizes the abstraction.

Dependency

Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other class it this happens.
Example: Relationship between shape and circle is dependency.

Association vs Aggregation vs Composition


These terms signify the relationships between classes. These are the building blocks of object oriented programming and very basic stuff. But still for some, these terms look like Latin and Greek. Just wanted to refresh these terms and explain in simpler terms.

Association

Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.
Example: A Student and a Faculty are having an association.

Aggregation

Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.

Composition

Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.

Difference between aggregation and composition

Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. Though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional. In both aggregation and composition, direction is must. The direction specifies, which object contains the other object.
Example: A Library contains students and books. Relationship between library and student is aggregation. Relationship between library and book is composition. A student can exist without a library and therefore it is aggregation. A book cannot exist without a library and therefore its a composition. For easy understanding I am picking this example. Don’t go deeper into example and justify relationships!

Wednesday, 9 October 2013

What is Tripleton design pattern , and write a tripleton class ?

Instanciate your class via a factory (see design patterns) and limit it to 3 instances using a simple counter.


public class LimitClass {

    private static LimitClass limInstance;
    public static int objCount = 0;

    private LimitClass(){
        objCount++;
    }

    public static synchronized LimitClass getLimInstance(){
        if(objCount < 3 ){
            limInstance = new LimitClass();
        }
        return limInstance;
    }
}

public class LimitObjectCreationTest {

    public static void main(String[] args) {

        LimitClass obj1 = LimitClass.getLimInstance();
        LimitClass obj2 = LimitClass.getLimInstance();
        LimitClass obj3 = LimitClass.getLimInstance();
        LimitClass obj4 = LimitClass.getLimInstance();
        LimitClass obj5 = LimitClass.getLimInstance();
        LimitClass obj6 = LimitClass.getLimInstance();

        System.out.println(obj1);
        System.out.println(obj2);

        System.out.println(obj3);
        System.out.println(obj4);
        System.out.println(obj5);
        System.out.println(obj6);
      }
}
Output of above code is like:
com.pack2.LimitClass@19821f
com.pack2.LimitClass@addbf1
com.pack2.LimitClass@42e816
com.pack2.LimitClass@42e816
com.pack2.LimitClass@42e816
com.pack2.LimitClass@42e816