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 <b>cm</b>
* @param weight in <b>kilograms</b>
* @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 <b>feet</b>
* @param weight in <b>pounds</b>
* @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

Advertisements

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module, Red Hat and some other places :) I am an author of the book 'Eclipse Debugging How To', Muay Thai fighter and amateur photographer ;)
This entry was posted in Helper Class, Java, open-source, Programming Languages, Useful Functions and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

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

  1. Ankit says:

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

  2. Howdy would you mind letting me know which webhost you’re using?
    I’ve loadsed your blog in 3 different internet
    browsers and I must say this blog loads a lot quicker then most.
    Can you recommend a good hosting provider at a air price?
    Kudos, I appreciate it!

  3. I am using standard web hosting provided by Word Press 🙂

  4. 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

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

  6. 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.

  7. I don’t even know how I stopped up here, however I thought this submit was once great.
    I don’t recognize who you’re but definitely you are going to a well-known blogger when you are not already.
    Cheers!

  8. Remarkable! Its actually amazing paragraph, I have got much clear idfea about from
    this paragraph.

  9. Latonya says:

    I’m gone to tell my little brother, that he should also go
    to see this webpage on regular basis to get updated from hottest information.

  10. Gwendolyn says:

    Fantastic iitems from you, man. Ihave understand your stuff prior to and you are simpply too excellent.
    I actually like what you’ve bought right here, certainly like what you’re saying and the way in which you
    assert it. You make it enjoyable annd you continue to take care oof to stay it smart.
    I can’t wait to read much more from you.
    This is really a tremendous site.

  11. Pretty section of content. I just stumbled upon your blog and in accession capital to assert that I
    get in fact enjoyed account your blog posts. Anyway I will be subscribing to your augment and
    even I achievement you access consistently rapidly.

  12. This blog was… how do I say it? Relevant!! Finally I’ve found something which helped me.
    Thanks a lot!

  13. Good article. I’m experiencing many of these issues
    as well..

  14. Thanks for one’s marvelous posting! I quite enjoyed reading it, you may be a great author.
    I will remember to bookmark your blog and definitely will come back at
    some point. I want to encourage yourself to continue your great writing, have a nice holiday weekend!

  15. Cody says:

    Remarkable stuff…. Can you please share the link to the Junit test cases ?

  16. Thanks for sharing your thoughts about dog clippers.
    Regards

  17. Bradly says:

    I hardly write remarks, however i did a few searching and wound up
    here Java Helper Class: BMI calculator, lb to kg converter, kg to lb converter, feet to
    cm converter, cm to feet converter | My Programming Blog.
    And I actually do have 2 questions for you if you do not mind.
    Could it be just me or does it appear like a few of
    the comments come across as if they are left by brain dead folks?
    😛 And, if you are writing on additional sites, I’d like to keep
    up with everything new you have to post. Could you list of all of your communal
    pages like your Facebook page, twitter feed, or linkedin profile?

  18. Raymond says:

    Thanks for a marvelous posting! I quite enjoyed reading it,
    you will be a great author.I will be sure to bookmark your blog and definitely
    will come back in the foreseeable future. I want to encourage you to definitely continue your great job, have a
    nice weekend!

  19. It’s awesome to pay a visit this web site and reading the views of all colleagues on the topic
    of this paragraph, while I am also zealous of
    getting familiarity.

  20. It’s really a nice and helpful piece of info. I am
    happy that you simply shared this helpful information with us.
    Please keep us informed like this. Thanks for
    sharing.

  21. Hi would you mind letting me know which web host you’re using?
    I’ve loaded your blog in 3 different web browsers and I must
    say this blog loads a lot faster then most.
    Can you suggest a good internet hosting provider at a reasonable price?

    Thanks a lot, I appreciate it!

  22. I’m amazed, I have to admit. Rarely do I encounter a blog
    that’s both equally educative and entertaining, and let me tell you, you’ve hit the nail on the head.

    The problem is something too few folks are speaking intelligently about.
    I’m very happy I stumbled across this during my hunt for something concerning this.

  23. What’s up to all, how is everything, I think every one
    is getting more from this web site, and your views are
    pleasant in favor of new visitors.

  24. 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.

  25. Meats, especially red meats, should also be avoided because they are
    hard to digest. But the ingredient list is extremely long and even five minutes in the kitchen would produce something palatable, tasty and also SANE, certainly something a tad more appetising than a veggie smoothie.
    This is why most people want to know how to transition to a vegetarian lifestyle.

  26. My brother suggested I may like this blog. He was entirely right.
    This publish truly made my day. You cann’t believe simply how
    much time I had spent for this information! Thank you!

  27. It’s hard too come by experienced people about this subject,
    however, you soud like you know what you’re talking about!
    Thanks

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

  29. Je suis venue ѕur ce site web par channce et puis je ne lle regrette nullement !

  30. Une fois dе pus un article effectivement plaisant

  31. Parthenia says:

    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.

  32. Avis says:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s