Results 1 to 4 of 4
Thread: Body Mass Index Calculator
- 11-01-2010, 03:24 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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
-
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]
Best of luck!Last edited by Fubarable; 11-01-2010 at 03:48 AM.
- 11-01-2010, 03:55 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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
-
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.
Thanks
Similar Threads
-
Pulling an Index made with Lucene to dev batch code for index listings
By txgeekgirl in forum LuceneReplies: 0Last Post: 10-29-2010, 09:15 PM -
Mass storage
By MIA6 in forum New To JavaReplies: 4Last Post: 11-02-2009, 07:22 PM -
Hi..every body
By hamudi in forum IntroductionsReplies: 2Last Post: 09-07-2008, 05:38 PM -
Can any body help me ?
By Shyam Singh in forum New To JavaReplies: 2Last Post: 08-22-2008, 01:04 AM -
Best GUI Element to Display Mass Data
By hannehomuth in forum Advanced JavaReplies: 3Last Post: 07-30-2008, 12:18 AM
Bookmarks