Need Help with Operators/ logic

Okay, I'm very, very new to java. I'm in an intro to computer science class and we got out first assignment to write a program. its from "An Into to Object Oriented Programming with java" by C. Thomas Wu.

all we have to do is create a program that calculate body mass index using the formula Bmi=Weight/(Height/100)^2 where weight is in kg and height is in cm.

Here is my code:

Code java:

import javax.swing.*;
class Bmi
{
public static void main(String[] args)
{
//*****
// Declarations:
//*****
final double NUM1=100.0; //The constant 100 in the formula
String weightin; //weight input
String heightin; //height input
String description; //if the output bmi is normal
int weightp; //weight primitive
int heightp; //height primitive
double denom; //height/NUM1
double denommod; //(height/NUM1)(height/NUM1)
double bmi; //final value-bmi
heightin=JOptionPane.showInputDialog(null,"Please Enter your weight:\n (Kilograms)"); //get weight
weightin=JOptionPane.showInputDialog(null,"Thanks you. Now, Please enter your Height:\n (centimeters)"); //get height
weightp=Integer.parseInt(weightin); // convert height and weight to primitive values
heightp=Integer.parseInt(heightin);
denom=(heightp/NUM1); //calulations
denommod=(denom*denom);
bmi=(weightp/denommod);
if(bmi<=18.5); //Understanding what the value means
description="underweight";
if((bmi>18.5)&(bmi<=25));
description="normal";
if((bmi>25)&(bmi<=30));
description="overweight";
if(bmi>30);
description="obese";
JOptionPane.showMessageDialog(null,"Your Body mass index is:" +bmi+ " \n This is considered " +description+".");
}
}

I think the problem is in the middle chunk there with the operators labelled "calculations"

but it could be my data types?

my professor (who i think to be completely incompetent) told us to use these test parameters to start:

50 kg and 160 cm

i keep getting 640 with this program but the correct answer is roughly 19.5

if you could point out any convention errors, too, that would be great!

thanks!

Re: Need Help with Operators/ logic

I don't notice anything wrong. Your formula seems correct and there is nothing noticeably wrong syntactically.

Re: Need Help with Operators/ logic

Try debugging your code by printing out the values of all the variables used in your calculations to see where your formulas are going wrong.

Re: Need Help with Operators/ logic

Mahaa! i found it. if you look at the code, i had prompted for weight and stored it in height and vice versa.

but now theres another problem!

heres the new code:

Code java:

import javax.swing.*;
class Bmi
{
public static void main(String[] args)
{
//*****
// Declarations:
//*****
final double NUM1=100.0; //The constant 100 in the formula
String weightin; //weight input
String heightin; //height input
String description; //if the output bmi is normal
int weightp; //weight primitive
int heightp; //height primitive
double denom; //height/NUM1
double denommod; //(height/NUM1)(height/NUM1)
double bmi; //final value-bmi
heightin=JOptionPane.showInputDialog(null,"Please Enter your Height:\n (Centimeters)"); //get weight
weightin=JOptionPane.showInputDialog(null,"Thanks you. Now, Please enter your Weight:\n (Kilograms)"); //get height
weightp=Integer.parseInt(weightin); // convert height and weight to primitive values
heightp=Integer.parseInt(heightin);
denom=(heightp/NUM1); //calulations
denommod=(denom*denom);
bmi=(weightp/denommod);
if(bmi<=18.5); //Understanding what the value means
description="underweight";
if((bmi>18.5)&(bmi<=25));
description="normal";
if((bmi>25)&(bmi<=30));
description="overweight";
if(bmi>30);
description="obese";
JOptionPane.showMessageDialog(null,"Your Body mass index is:" +bmi+ " \n This is considered " +description+".");
}
}

This time the problem is that it always tells me the bmi is obese. obviously an issue with the boolean logic. this isnt required for the project but i wanted to experiment. also, how do i keep the program from delivering the bmi with more than just a few decimal places?

Thanks!

Re: Need Help with Operators/ logic

Quote:

how do i keep the program from delivering the bmi with more than just a few decimal places

Look at the DecimalFormat class for formatting doubles to desired formats.

Re: Need Help with Operators/ logic

okay, all fixed. thanks for the help!