Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 29th, 2005, 8:21 AM   #1
newdude
Newbie
 
Join Date: Mar 2005
Posts: 4
Rep Power: 0 newdude is on a distinguished road
BMI calculator with responses

Hey guys,

I made a BMI calculatorand it works fine. Now I want it to give responses based on the bmi index that you get to an answer. Basically All I need is to figure out the right if then statment combination.

Here is my code so far


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class bmi2 extends JApplet
implements ActionListener
{
JTextField inputLbs, inputInches, displayBmi;

public void init()
{
JLabel labelLbs = new JLabel("Weight (lbs):", SwingConstants.RIGHT);
inputLbs = new JTextField(5);
JLabel labelInches = new JLabel("Height (inches):", SwingConstants.RIGHT);
inputInches = new JTextField(5);
JLabel labelBmi = new JLabel("BMI = ", SwingConstants.RIGHT);
displayBmi = new JTextField(5);
displayBmi.setEditable(false);
JButton go = new JButton("Compute");
go.addActionListener(this);

Container c = getContentPane();
c.setBackground(Color.white);
JPanel p = new JPanel();
p.setLayout(new GridLayout(3, 2, 5, 5));
p.add(labelLbs);
p.add(inputLbs);
p.add(labelInches);
p.add(inputInches);
p.add(labelBmi);
p.add(displayBmi);
c.add(p, BorderLayout.CENTER);
c.add(go, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent e)
{
int lbs = Integer.parseInt(inputLbs.getText());
int inches = Integer.parseInt(inputInches.getText());
double bmi = calculateBmi(lbs, inches);
DecimalFormat df = new DecimalFormat("00.0");
displayBmi.setText(df.format(bmi));
}

private double calculateBmi(int lbs, int inches)
{
return (double) (lbs/2.2046226) / ((double)(inches*0.0254)*(double)(inches*0.0254));
}
}

Thanks alot


P.S.: for the BMI indexes it should be:
less than 18.5 = underweight
Normal weight = 18.5-24.9
Overweight = 25-29.9
Obesity = BMI of 30 or greater
newdude is offline   Reply With Quote
Old Mar 29th, 2005, 8:55 AM   #2
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
well .. it should be like this :
if(bmi<18.5)System.out.println("Underweight");
    else if((bmi>18.5)&&(bmi<24.9))System.out.println("normal");
          else if((bmi>25)&&(bmi<29.9))System.out.println("Overweight");
               else System.out.println("Obesis");
You make some modifications to suit your needs ... and that should do it, if i understood what you'r asking
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Mar 31st, 2005, 7:59 AM   #3
newdude
Newbie
 
Join Date: Mar 2005
Posts: 4
Rep Power: 0 newdude is on a distinguished road
Thats what i was tying. and it compils fine but it simply wont display the message how should I go about this??


Thanks
Andrea
newdude is offline   Reply With Quote
Old Mar 31st, 2005, 8:31 AM   #4
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Well, you should have another JTextField, let's say displayIndexBmi
and instead of System.out.println() - you should use
displayIndexBmi.setText("Underweight");
or "normal" or"Obesis" - u get the picture.

That's what I meant by saing -- "make some modifications to suit your needs"
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Mar 31st, 2005, 9:25 AM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Also, when you don't use >= and <= and instead < and > it's excluding the numbers given to it as a test. Ex. if 25, 29.9, 18.5, etc... any of those numbers were the BMI it would show "obesis".. it should really be this...

if(bmi < 18.5)
    displayIndexBmi.setText"Underweight");
else if((bmi >= 18.5) && (bmi <= 24.9))
   displayIndexBmi.setText("normal");
else if((bmi >= 25) && (bmi <= 29.9))
    displayIndexBmi.setText("Overweight");
else
    displayIndexBmi.setText("Obesis");
tempest is offline   Reply With Quote
Old Mar 31st, 2005, 6:11 PM   #6
Donald Ferrone
Newbie
 
Donald Ferrone's Avatar
 
Join Date: Mar 2005
Posts: 19
Rep Power: 0 Donald Ferrone is on a distinguished road
Send a message via MSN to Donald Ferrone
I guess you would use the full duplex operators to create world-class synergy using an ERP pipeline over your BBQ bus.
Donald Ferrone is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC