Results 1 to 4 of 4
  1. #1
    bentho is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Question Body Mass Index Calculator

    I am working on a bmi calculator that is a revision of an example program in my textbook. The program lets user enter weight (in pounds) and height (in feet and inches) and then calculates bmi. Weight is converted to kilograms and height is converted to meters.

    I have this so far:
    Java Code:
    import java.util.*;
    public class ExerciseSix
    {
        //Attributes
        private double weight, feet, inches;
        final double kgPerPound = 0.45359237;
        final double metPerInch = 0.0254;
        //Constructors
        public ExerciseSix()
        {
            Scanner reader = new Scanner(System.in);
            System.out.println("Enter weight in pounds: ");
            weight = reader.nextDouble();
            System.out.println("Enter height in feet and inches: ");
            feet = reader.nextDouble();
            inches = reader.nextDouble();
        }
        //Methods
        public void bodyMassIndex()
        {
            double wtInKilos = weight * kgPerPound;
            double htInInches = ((feet / 12) + inches);
            double htInMeters = htInInches * metPerInch;
            double bmi = wtInKilos / (htInMeters * htInMeters);
            System.out.printf("Your BMI is %5.2f\n" , bmi);
        }
    }

    Only difference from example in the book is they used height in inches from start, everything else is same. I am getting large numbers like 946 for bmi when it should be like 20.95. Problem may be in conversion from feet to inches and then inches to meters. Anybody see what I am missing.
    Last edited by Fubarable; 11-01-2010 at 03:33 AM. Reason: moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bentho View Post
    Java Code:
    double htInInches = ((feet / 12) + inches);
    How do you calculate the number of inches in each foot? Is it feet / 12, or is it perhaps something else? ;)

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    I've added code tags to your post above so you should be able to see the improvement for yourself.

    Best of luck!
    Last edited by Fubarable; 11-01-2010 at 03:48 AM.

  3. #3
    bentho is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    I figured it out. Not sure why I was dividing feet by 12 instead of multiplying by 12 guess it was a brain fart. I didn't know that about the code tags and formatting.

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bentho View Post
    I figured it out. Not sure why I was dividing feet by 12 instead of multiplying by 12 guess it was a brain fart.
    Sometimes it's hard to see when it's buried deep in your code. The way that I solved it was to place println statements to check the values of variables to see if they made sense. For example:

    Java Code:
       public void bodyMassIndex() {
          double wtInKilos = weight * kgPerPound;
          System.out.println("wtInKilos: " + wtInKilos);
          double htInInches = ((feet / 12) + inches);
          System.out.println("htInInches: " + htInInches); // *** this result didn't make sense
          double htInMeters = htInInches * metPerInch;
          double bmi = wtInKilos / (htInMeters * htInMeters);
          System.out.printf("Your BMI is %5.2f\n", bmi);
       }

    I didn't know that about the code tags and formatting.
    No problem. You do now. :)


    Thanks
    You're welcome!

Similar Threads

  1. Replies: 0
    Last Post: 10-29-2010, 09:15 PM
  2. Mass storage
    By MIA6 in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 07:22 PM
  3. Hi..every body
    By hamudi in forum Introductions
    Replies: 2
    Last Post: 09-07-2008, 05:38 PM
  4. Can any body help me ?
    By Shyam Singh in forum New To Java
    Replies: 2
    Last Post: 08-22-2008, 01:04 AM
  5. Best GUI Element to Display Mass Data
    By hannehomuth in forum Advanced Java
    Replies: 3
    Last Post: 07-30-2008, 12:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •