Friday 20 January 2012

How to calculate an age in Java?

Can we calculate an age in java without worrying about number of leap years between birth date and current date? Is there simple way to calculate it? …. The answer is yes. When we calculate somebody’s age manually, we do not think about number of leap year or number of days in each year. Same logic can be applies for calculating age programmatically.

Following is a code snippet for calculating the age. Please leave comment if it has helped you. Also please let me know if you feel this can be further optimized or this can be achieved by more simpler way...

  int years = 0;
  int months = 0;
  int days = 0;
  
  //create calendar object for birth day
  Calendar birthDay = Calendar.getInstance();
  birthDay.set(Calendar.YEAR, 2005);
  birthDay.set(Calendar.MONTH, Calendar.SEPTEMBER);
  birthDay.set(Calendar.DATE, 29);
  
  //create calendar object for current day
  long currentTime = System.currentTimeMillis();
  Calendar currentDay = Calendar.getInstance();
  currentDay.setTimeInMillis(currentTime);

  //Get difference between years
  years = currentDay.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);

  
  int currMonth = currentDay.get(Calendar.MONTH)+1;
  int birthMonth = birthDay.get(Calendar.MONTH)+1;
  
  //Get difference between months
  months = currMonth - birthMonth;
  
  //if month difference is in negative then reduce years by one and calculate the number of months. 
  if(months < 0)
  {
   years--;
   months = 12 - birthMonth + currMonth;
   
   if(currentDay.get(Calendar.DATE)<birthDay.get(Calendar.DATE))
    months--;
   
  }else if(months == 0 && currentDay.get(Calendar.DATE) < birthDay.get(Calendar.DATE)){
   years--;
   months = 11;
  }
  
  
  //Calculate the days
  if(currentDay.get(Calendar.DATE)>birthDay.get(Calendar.DATE))
   days = currentDay.get(Calendar.DATE) -  birthDay.get(Calendar.DATE);
  else if(currentDay.get(Calendar.DATE)<birthDay.get(Calendar.DATE)){
   int today = currentDay.get(Calendar.DAY_OF_MONTH); 
   currentDay.add(Calendar.MONTH, -1);
   days = currentDay.getActualMaximum(Calendar.DAY_OF_MONTH)-birthDay.get(Calendar.DAY_OF_MONTH)+today;
  }else{
   days=0;
   
   if(months == 12){
    years++;
    months = 0;
   }
  }
  
  System.out.println("The age is : "+years+" years, "+months+" months and "+days+" days" );


Tuesday 10 January 2012

JBox2D with JavaFX tutorial: Applying Force and Impulse on body

Many times there will be situation when in JBox2D world we want to move bodies manually. So how do we do it? In real world we apply force to move anything. It holds good in JBox2D world too. To move bodies in JBox2D world we have to apply Force or Impulse.

There is a difference between force and Impulse. Force act gradually on bodies’ velocity. Imagine in real world if we wants to move one big box from one location to other then we will start pushing it. It means we will gradually apply force on that box; as a result box will start moving. Force in JBox2D world also works in similar fashion.

Following is a code snippet for applying force on body.
Body body = (Body)ball.getUserData();
Vec2 force  = new Vec2(0, 150.0f);
Vec2 point = body.getWorldPoint(body.getWorldCenter());
body.applyForce(force ,point);

Impulse act instantaneously, in a fraction of time step. Impulse is like we strike snooker ball with a cue or move objects with the help of explosives. Applying impulse is similar to applying force. We need to use applyLinearImpulse() function in place of applyForce()

Following sample code snippet is for applying the impulse.
Body body = (Body)ball.getUserData();
Vec2 force  = new Vec2(0, 50.0f);
Vec2 point = body.getWorldPoint(body.getWorldCenter());
body. applyLinearImpulse (force ,point);

The applyForce() and applyLinearImpulse() function takes two parameters force and point. Force parameter defines a direction and magnitude of the force. And second parameter defines on which point of the body force should be applied. In above snippet, we are applying force/impulse on center of the body.

<<JBox2D With JavaFX : Write your first JBox2D with JavaFX 2 program