0% found this document useful (0 votes)
12 views4 pages

CODING

Uploaded by

m-6683956
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

CODING

Uploaded by

m-6683956
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CODING

import javax.swing.*;

public class BMICalculator extends JFrame {


private JLabel heightLabel, weightLabel, resultLabel;
private JTextField heightField, weightField, resultField;
private JButton calculateButton;

public BMICalculator() {
setTitle("BMI Calculator");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);

heightLabel = new JLabel("Height (cm):");


heightLabel.setBounds(20, 20, 80, 20);
add(heightLabel);

heightField = new JTextField();


heightField.setBounds(110, 20, 150, 20);
add(heightField);

weightLabel = new JLabel("Weight (kg):");


weightLabel.setBounds(20, 50, 80, 20);
add(weightLabel);

weightField = new JTextField();


weightField.setBounds(110, 50, 150, 20);
add(weightField);

calculateButton = new JButton("Calculate");


calculateButton.setBounds(90, 80, 100, 20);
calculateButton.addActionListener(e -> calculateBMI());
add(calculateButton);

resultLabel = new JLabel("Result:");


resultLabel.setBounds(20, 110, 80, 20);
add(resultLabel);

resultField = new JTextField();


resultField.setBounds(110, 110, 150, 20);
resultField.setEditable(false);
add(resultField);

setVisible(true);
}

private void calculateBMI() {


try {
double heightCm = Double.parseDouble(heightField.getText());
double heightM = heightCm / 100;
double weight = Double.parseDouble(weightField.getText());

double bmi = weight / (heightM * heightM);


String status;

if (bmi < 18.5) {


status = "Underweight";
} else if (bmi < 24.9) {
status = "Normal weight";
} else if (bmi < 29.9) {
status = "Overweight";
} else {
status = "Obese";
}

resultField.setText(String.format("BMI: %.2f, Status: %s", bmi, status));


} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter valid numbers for height and weight.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String[] args) {


new BMICalculator();
}
}
OUTPUT
CARTA ALIR

MULA

Memaparkan GUI dengan label dan


input field untuk tinggi dan berat

Pengguna memasukkan tinggi dan


berat

Pengguna menekan button


"Calculate"

Program menyemak sama


ada ketinggian dan berat
input adalah nombor yang
sah

Program menentukan status BMI


berdasarkan hasil pengiraan

Program memaparkan hasil BMI


dan status pada GUI

TAMAT

You might also like