Need some easy Java Help  | |
May 15th, 2004, 09:25 PM
|
#1 (permalink)
| | Member
Join Date: Mar 2002
Posts: 153
|
Hello,,, well I am learning java in school and the teacher is terrible, I have a hw assignment I have no idea how to do.
I have to finish writing the code to this program. the question is.
"The BMI applet computes a persons body mass index. BMI Applet is defined as the weight, in kg divided by the square of the height, expressed in meters. A fragment of the BMI applet is show below.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class Bmi 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)
{
< ... missing code >
}
}
I have to supply the missing code for the calculateBmi method, which takes weight in lbs and hight in inches as arguments and returns the body mass index...
ANY ideas here thanks/.. |
| |
May 17th, 2004, 03:26 AM
|
#2 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
|
Ok well I just had to rewrite this because I thought you had to create and actionPerformed method but apparently its already created!!!!
Actually this problem is very easy:
All you need to do is write a few lines of code that do all the calculations of the lbs and inches that the method is taking. For instance, if the bmi was found by adding lbs and inches then the code for that method would look as follows: Code:
private double calculateBmi(int lbs, int inches)
{
double total = lbs + inches;
return total;
}
OR YOU CAN DO IT DIRECTLY...
private double calculateBmi(int lbs, int inches)
{
return Double.parseDouble(lbs + inches);
} Not too difficult. Just use that same methodology but you'll need to use the correct way to find the Bmi.
__________________
YAH! I knew you'd be jealous
Last edited by Tekk : May 19th, 2004 at 05:38 AM.
|
| |
May 17th, 2004, 04:53 AM
|
#3 (permalink)
| | Retired mostly.
Join Date: Oct 2001 Location: Finland
Posts: 5,144
|
I don't know anything about java or your problem, but why are you using pounds and inches when it says at the top that it has to be kilograms and meters? |
| |
May 17th, 2004, 01:42 PM
|
#4 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
|
the teacher obviously wants them to do some conversion. |
| |
May 17th, 2004, 06:48 PM
|
#5 (permalink)
| | Member
Join Date: Mar 2002
Posts: 153
|
Thanks a lot Tekk  |
| |
May 18th, 2004, 12:09 AM
|
#6 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
|
no problem...lemme know if you need any more help  |
| |
May 19th, 2004, 12:35 AM
|
#7 (permalink)
| | Member
Join Date: Sep 2002
Posts: 364
|
Last edited by Creosote : May 19th, 2004 at 01:13 AM.
|
| |
May 19th, 2004, 12:47 AM
|
#8 (permalink)
| | Now in the nicer ghetto
Join Date: Jul 2003 Location: PA
Posts: 10,527
|
EDIT due to other EDIT. nothing to see here, move along people
__________________
I don't like signatures.
Last edited by butch81385 : May 19th, 2004 at 12:47 PM.
|
| |
May 19th, 2004, 02:05 AM
|
#9 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,177
|
who you replyin to butch? |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |