Java Helper Class: BMI calculator, lb to kg converter, kg to lb converter, feet to cm converter, cm to feet converter

Hey Guy’s,

Long time no seen. Today I want to share with you Java helper class that I have designed for one  of the projects I am working on. This class will be useful for you, if your application works with height and weight conversions, also if you need to calculate Body Mass Index.

My helper class has following functionality:

– Kg to Lb Converter
– Lb to Kg Converter
– BMI (Body Mass Index) Calculator and “Classificator
– Feet to Cm Converter
– Cm to Feet Converter

Here is the class:

import java.text.DecimalFormat;
 
/**
* This class is used for any calculations connected with Weight and Height
*  - KG to LB converter (and vice versa)
*  - Feet to Cm converter (and vice versa)
*  - BMI Calculation
*
*/
public class HeightWeightHelper {
 
/**
*
* @param value double that is formatted
* @return double that has 1 decimal place
*/
private double format ( double value) {
  if ( value != 0){
   DecimalFormat df = new DecimalFormat("###.#");
   return Double.valueOf(df.format(value));
 } else {
   return -1;
 }
}
 
/**
*
* @param lb - pounds
* @return kg rounded to 1 decimal place
*/
public double lbToKgConverter(double lb) {
  return format(lb * 0.45359237 );
}
 
/**
*
* @param kg - kilograms
* @return lb rounded to 1 decimal place
*/
public double kgToLbConverter(double kg) {
  return format(kg * 2.20462262);
}
 
/**
*
* @param cm - centimeters
* @return feet rounded to 1 decimal place
*/
public double cmToFeetConverter(double cm) {
  return format(cm * 0.032808399 );
}
 
/**
*
* @param feet - feet
* @return centimeters rounded to 1 decimal place
*/
public double feetToCmConverter(double feet) {
  return format(feet * 30.48 );
}
 
/**
*
* @param height in cm
* @param weight in kilograms
* @return BMI index with 1 decimal place
*/
public double getBMIKg (double height, double weight) {
  double meters = height/100;
  return format( weight / Math.pow(meters,2));
}
 
/**
*
* @param height in feet
* @param weight in pounds
* @return BMI index with 1 decimal place
*/
public double getBMILb (double height, double weight) {
  int inch = (int)(height *12);
  return format((weight*703) / Math.pow(inch, 2));
}
 
/**
*
* @param bmi (Body Mass Index)
* @return BMI classification based on the bmi number
*/
public String getBMIClassification (double bmi) {
 
 if (bmi <= 0) return "unknown";
 String classification;
 
 if (bmi < 18.5) {
  classification = "underweight";
 } else if (bmi < 25) {
  classification = "normal";
 } else if (bmi < 30) {
  classification = "overweight";
 } else {
  classification = "obese";
 }
 
 return classification;
}
 
}//end of class

P.S If you want to have JUnit tests for this class let me know, I have them too 🙂

Regards,

Anatoly

Thanks for installing the Bottom of every post plugin by Corey Salzano. Contact me if you need custom WordPress plugins or website design.

Anatoly Spektor

IT Consultant with 6 years experience in Software Development and IT Leadership. Participated in such projects as Eclipse IDE, Big Blue Button, Toronto 2015 Panam Games.

Join the Discussion

Your email address will not be published. Required fields are marked *

Discussion

  1. Ankit

    Hey nice post
    I would like to know how can i write code with the syntax highlighter in my blogs if you can help

      1. Ankit

        i was asking how to do that in a wordpress.com blog
        but i got it anyway.. but thanks for your reply 🙂

      2. Ankit

        i was asking how to do that in a wordpress.com blog
        but i got it anyway.. but thanks for your reply 🙂

    1. Anatoly Spektor

      Sorry Ankit, I haven’t seen your comment! Here is the link on how to do it: http://en.support.wordpress.com/code/posting-source-code/

      1. Ankit

        Thanks 🙂

  2. Anatoly Spektor

    I am using standard web hosting provided by Word Press 🙂

  3. woodworking projects

    Hello! Would you mind if I share your blog with my myspace group?

    There’s a lot of people that I think would really enjoy your content.
    Please let me know. Many thanks

    1. Anatoly Spektor

      Go ahead

  4. Hi, I read your new stuff daily. Your writing style is awesome,
    keep it up!

  5. Telecharger GTA 5

    With havin so much content do you ever run into any
    issues of plagorism or copyright violation? My site has a lot of exclusive
    content I’ve either written myself or outsourced
    but it seems a lot of it is popping it up all over the internet without my
    permission. Do you know any solutions to help prevent
    content from being ripped off? I’d genuinely appreciate it.

  6. Hey I know this is off topic but I was wondering if you knew of
    any widgets I could add to my blog that automatically tweet my newest
    twitter updates. I’ve been looking for a plug-in like this for
    quite some time and was hoping maybe you would have some
    experience with something like this. Please let me know
    if you run into anything. I truly enjoy reading your blog and
    I look forward to your new updates.

  7. sodomisatrice

    J’ai trouvé ѵotre ѕitе internet par mégardе et puis je ne le
    regyrette pas ɗuu tout !!!

  8. Parthenia

    Alternatively, you can simply attach decorative wrought iron pieces on top of the rack so it becomes more
    of an ornament as much as it is a coat rack. When planning your home theater system you will need
    to account for the audio system and how it will be integrated into the entertainment room.
    In order to ensure low loss of power energy the HDMI cables are designed with the connectors that are
    gold plated.

  9. Avis

    It’s remarkable for me to have a web page, which is helpful in support
    of my know-how. thanks admin

arrow