In: Computer Science
Java Language Only
Use Java FX
In this project we will build a BMI calculator. BMI is calculated using the following formulas:
Measurement Units | Formula and Calculation |
---|---|
Kilograms and meters (or centimetres) |
Formula: weight (kg) / [height (m)]2 The formula for BMI is weight in kilograms divided by height in meters squared. If height has been measured in centimetres, divide by 100 to convert this to meters. |
Pounds and inches |
Formula: 703 x weight (lbs) / [height (in)]2 When using English measurements, pounds should be divided by inches squared. This should then be multiplied by 703 to convert from lbs/inches2 to kg/m2. |
Please update the BMIController.java file. Only modify the changeUnits and calculateResult methods.
BMI Controller code below:
package bmi;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
public class BMIController {
@FXML
private Button calculate;
@FXML
private Label heightLBL;
@FXML
private Label result;
@FXML
private Label weightLBL;
@FXML
private RadioButton us;
@FXML
private RadioButton metric;
@FXML
private TextField weightTF;
@FXML
private TextField heightTF;
@FXML
public void changeUnits(ActionEvent event)
{
//This method should
update the labels to display correct unites
}
@FXML
public void calculateResult(ActionEvent event)
{
// This method should
calculate the BMI
}
}
HI,
please find the code below.
@FXML
public void changeUnits(ActionEvent event)
{
//This method should update the labels to display correct
unites
if(metric.isSelected())
{
weightLBL.setText(String.format("%.2f Metric System",w));
heightLBL.setText(String.format("%.2f Metric System",h));
}
else if(us.isSelected())
{
weightLBL.setText(String.format("%.2f US System",w));
heightLBL.setText(String.format("%.2f US System",h));
}
}
@FXML
public void calculateResult(ActionEvent event)
{
try
{
Double w = new Double(weight.getText());
Double h = new Double(height.getText());
//weight.setText(String.format("%.2f",w));
//height.setText(String.format("%.2f",h));
Double bmi;
if(metric.isSelected())
{
bmi = (w * 703.0)/(h*h);
result.setText(String.format("%.2f",bmi));
}
else if(us.isSelected())
{
bmi = w /(h*h);
result.setText(String.format("%.2f",bmi));
}
}catch(NumberFormatException nf)
{
weightTF.setText("Enter valid value");
weightTF.selectAll();
weightTF.requestFocus();
heightTF.setText("Enter valid value");
heightTF.selectAll();
}
}
thanks,
kindly upvote