MPAA | Books | Myspace Proxy | Mobile Phones | Car Finance
Easy Java Help please [Archive] - PCMech Forums

PDA

View Full Version : Easy Java Help please


Thonger
05-15-2004, 08:26 PM
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/..

mxfury
05-15-2004, 11:36 PM
try going to www.sitepoint.com or www.webmaster-forums.net

DrZaius
05-16-2004, 01:08 AM
Hi Thonger,

You need to use the lbs and inches variables and do the needed calculations and conversions (inches to meters, etc.) to return the BMI.

For example, lets say that the BMI was defined as the weight times the height (it's not, but this is for a simple examples.) Your missing code would simply be return (lbs * inches).

The description mentions the use of the square root function, which is available in the java.math library which you may need to import to make use of. Hope this helps.

Jaggannath
05-16-2004, 11:12 PM
Try working out what it is you need to do in English first
So you'll need to convert from lbs to kg, and convert from inches to metres. You'll also need to use the metric calculations to calculate the BMI and output it

Then you look at what you have and work from there :)