Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Build the interface: A BMI calculator First: add a Layered Pane; drag it from th

ID: 3862255 • Letter: B

Question

Build the interface: A BMI calculator First: add a Layered Pane; drag it from the right window to your blank frame. Then enlarge the pane to make it cover your entire frame. Use the text fields and labels to build the following application. Example: String inch = jTextField1.getText(); You need to convert value in the text field from String to integer when you do the calculation. How to Calculate BMI 1 feet = 12 inches BMI= weight in pounds x703/height in inches Example: Jane weighs 150lbs and is 5 feet 4 inches tall. Jane's height in inches is (5 * 12) + 4 = 64" multiply the weight by 703. 150 * 703 = 105450 multiply the height by itself 64 times 64 = 4096 Finally divide the first figure by the second. 105450/4096 = 25.7 Jane's BMI s 25.7 Bonus points (2pts): If the user enters invalid or empty input, you need to popup an invalid input message.

Explanation / Answer

public class BMI extends JFrame implements ActionListener {

    private static final JButton JButton = null;
    private JFrame frame;
    private JPanel panel;
    private JLabel heightLabel, weightLabel, BMILabel;
    private JTextField height, weight, result;
    private JButton calculate;
    String Height, Weight;
    double hght,wght, BMI;
    static String output = "Results";
    static int jopIcon = JOptionPane.QUESTION_MESSAGE;
    boolean bFlag = true;
    public BMI() {

        frame = new JFrame("BMI Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        heightLabel = new JLabel("Your height in inches:");
        weightLabel = new JLabel("Your weight in lbs: ");
        BMILabel = new JLabel("Your BMI is ");
        result = new JTextField("");
        height = new JTextField(1);
        weight = new JTextField(1);


        calculate = new JButton("Calculate BMI");
        panel = new JPanel();
        panel.add(heightLabel);
        panel.add(height);
        panel.add(weightLabel);
        panel.add(weight);
        panel.add(BMILabel);
        panel.add(result);
        panel.add(calculate);
        frame.getContentPane().add(panel);
        JPanel p1 = new JPanel();
        panel.setLayout(new GridLayout(4, 1));
        add(p1, BorderLayout.SOUTH);
        calculate.addActionListener(this);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }

    public String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

    public void calculateBMI(double wght, hght number2) {
        try {
            BMI = wght*703 / ((hght) ^ 2);

        } catch (NumberFormatException nfe) {
            output += " Whoa! Input error: must enter valid integers";
            jopIcon = JOptionPane.ERROR_MESSAGE;
        }
    }

    public void calculate() {
        Height = height.getText();
        Weight = weight.getText();

        try {
            hght = Double.parseDouble(Height);
            wght = Double.parseDouble(Weight);

            calculateBMI(hght, wght);

        } finally {

            if (BMI >= 27.5) {

                output += " You're UnHealthy.";

            } else if (BMI <= 23 || BMI < 27.4) {

                output += " You're in the Moderate Risk zone. ";

            } else if (BMI <= 18.5 || BMI < 22.9) {
                output += " You're Healthy. ";

            } else if (BMI < 18.4) {
                output += " You really need to start eating more. ";

            }
        }

    }

output:

your height in inches:64

your weight in lbs:150

your BMI is:25

You're in the Moderate Risk zone

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote