In: Computer Science
ONLY IN JAVAFX. The application will calculate Body Mass Index (BMI) for people. It must be able to accept as input weights (in pounds or kilos), and height (in inches or centimeters). The application should have a calculate button, and should display the result as well as if the data puts the person in one of 4 categories underweight ( BMI < 18.5) , normal weight (BMI 18.5-24.9), overweight (BMI 25.0 - 29.9) or overweight (BMI > 30)
BMIController.java
package bmi;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
public class BMIController implements Initializable {
@FXML
private Label label;
@FXML
private RadioButton pi;
@FXML
private RadioButton km;
@FXML
private TextField weight;
@FXML
private TextField height;
@FXML
private void handleButtonAction(ActionEvent event) {
try{
Double w = new Double(weight.getText());
Double h = new Double(height.getText());
Double bmi;
if(pi.isSelected()){
bmi = (w * 703.0)/(h*h);
label.setText(String.format("%.2f",bmi));
}
else if(km.isSelected()) {
bmi = w /(h*h);
label.setText(String.format("%.2f",bmi));
}
}catch(NumberFormatException nf){
weight.setText("Enter valid value");
weight.selectAll();
weight.requestFocus();
height.setText("Enter valid value");
height.selectAll();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
BMI.java
package bmi;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class BMI extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root =
FXMLLoader.load(getClass().getResource("BMI.fxml"));
Scene scene = new Scene(root);
stage.setTitle("BMI CALCULATOR");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
BMI.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="480.0"
prefWidth="412.0" xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8"
fx:controller="bmi.BMIController">
<children>
<Button fx:id="button" layoutX="155.0" layoutY="272.0"
onAction="#handleButtonAction" text="Calculate" />
<Label fx:id="label" alignment="CENTER" layoutX="14.0"
layoutY="381.0" minHeight="16" minWidth="69" prefHeight="64.0"
prefWidth="180.0" text="0.0">
<font>
<Font name="System Bold Italic" size="24.0" />
</font></Label>
<TextArea layoutX="193.0" layoutY="319.0" prefHeight="147.0"
prefWidth="206.0" text="BMI VALUES Underweight:	less
than 18.5 Normal:		between 18.5 and
24.9 Overweight:	between 25 and
29.9 Obese:		30 or
greater ">
<font>
<Font size="11.0" />
</font>
</TextArea>
<TextField fx:id="weight" layoutX="110.0" layoutY="145.0"
/>
<TextField fx:id="height" layoutX="110.0" layoutY="213.0"
/>
<RadioButton fx:id="pi" layoutX="60.0" layoutY="47.0"
mnemonicParsing="false" selected="true"
text="Pounds-Inches">
<toggleGroup>
<ToggleGroup fx:id="ip" />
</toggleGroup>
</RadioButton>
<RadioButton fx:id="km" layoutX="227.0" layoutY="47.0"
mnemonicParsing="false" text="Kilograms-Meters" toggleGroup="$ip"
/>
<Label layoutX="119.0" layoutY="117.0" text="Weight" />
<Label layoutX="119.0" layoutY="190.0" text="Height" />
<Label layoutX="28.0" layoutY="319.0" prefHeight="50.0"
prefWidth="152.0" text="Your BMI is:">
<font>
<Font size="24.0" />
</font>
</Label>
</children>
</AnchorPane>