Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need Help with Operators/ logic

  1. #1
    Junior Member codekiller's Avatar
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
     
    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!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need Help with Operators/ logic

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

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

  4. #4
    Junior Member codekiller's Avatar
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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!
    Java is like a good woman:
    Shes fun to play with,
    She looks nice,
    She always tells the truth,
    And She even takes out the GARBAGE.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Operators/ logic

    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.

  6. #6
    Junior Member codekiller's Avatar
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Operators/ logic

    okay, all fixed. thanks for the help!
    Java is like a good woman:
    Shes fun to play with,
    She looks nice,
    She always tells the truth,
    And She even takes out the GARBAGE.

Similar Threads

  1. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  2. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM
  3. Simple recursion logic
    By chronoz13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 24th, 2009, 10:53 PM