Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-15-2004, 07:26 PM   #1
Member (5 bit)
 
Join Date: Aug 2002
Posts: 24
Easy Java Help please

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/..
Thonger is offline   Reply With Quote
Old 05-15-2004, 10:36 PM   #2
Member (6 bit)
 
Join Date: Jan 2004
Posts: 52
try going to www.sitepoint.com or www.webmaster-forums.net
mxfury is offline   Reply With Quote
Old 05-16-2004, 12:08 AM   #3
Member (13 bit)
 
DrZaius's Avatar
 
Join Date: Jul 2000
Location: Fullerton, CA
Posts: 7,030
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.
DrZaius is offline   Reply With Quote
Old 05-16-2004, 10:12 PM   #4
Gremlin Overlord
 
Jaggannath's Avatar
 
Join Date: Apr 2003
Location: Australia
Posts: 2,382
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
Jaggannath is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:54 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2