2011-11-24

Designing a class to create real world objects of type Person

Identity is the hashcode of an object,it is a 32 bit integer number created randomly and assigned to an object by default by JVM. Each object will have a unique identity.
Developer can also generate hashcode of an object based on state of that object by overriding hashcode() method pf java.lang.objectclass. Then if state is changed automatically hashcode will be changed.
JVM generated hashcode will not be changed if object state is changed because it is not generated based on object state.Hashcode is used when object is stored in hashtable.Hashcode is used when object is stored in hashtable
To understand all above points, consider real world object person as an example.

Every person will have 
state - eyes, ears, hands, legs, name, height, weight, foodhabits
Behavior - getName(), setName(), sleep(), eat().

we should consider below things to design a class(blueprint) to create objects of type person
What are the common attributes and individual attributes ?
What are the common behaviors, and individual behaviors ?

Every person has 2 eyes, 2 ears, 2 legs, 2 hands, so create these variables as static final variables to store values common for all person objects,it will save memory. Every person has his/her own name, height, weight, foodHabits so create these variables as non-static variables to store value individual to every object.
Create behaviors getName(), setName() methods as non-static methods to change name specific to Person object. Also eat() method should be a non-static method as it uses object data,non-static variable foodHabits. Create sleep() method as static method to execute logic common for all objects.

Check below program

//Person.java

class Person
{
//static variables having values common for all objects
static final int eyes=2;

static final int ears=2;

static final int hands=2;

static final int legs=2;

//non-static variable having values individual to every object

String name;

double height;

double weight;

String foodHabits;

//parameterized constructor to initialize object state with user given values

Person(String name,double height,double weight,String foodHabits)
{
     this.name       = name;

     this.height     = height;

     this.weight     = weight;

     this.foodHabits = foodHabits;
}

//parameterized non-static methods to change object state with user given values

void setName(String name)
{
     this.name = name;
}

void setheight(double height)
{
     this.height = height;
}

void setWeight(double weight)
{
     this.weight = weight;
}

void setfoodhabits(String foodHabits)
{
     this.foodHabits = foodHabits;
}

void printNHW()
{
     System.out.println("name: "+name);

     System.out.println("height: "+height);

     System.out.println("weight: "+weight);

     System.out.println("foodHabits: "+foodHabits);

}

}
//PersonUser.java
class PersonUser

{

public static void main(String[] args)

{

Person p1=new Person("venkatesh",6.0,80,"veg");

Person p2=new Person("balakrishna",5.9,180,"non-veg");

System.out.println("objects default state");

p1.printNHW();

p2.printNHW();

System.out.println(p1);

System.out.println(p2);

System.out.println("==================================");

p2.setName("balayya");

p2.setWeight(120);

System.out.println(p1);

System.out.println(p2);

System.out.println("objects changed state"); 

p1.printNHW();

p2.printNHW();

}

}



Loading

Enter your Email here: