Question
Java program
11:21 AM .oooo Sprint F O 76% westernonline.wiu.edu a 3. 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. or obtains the est displayed by the text Example: String inch jTextFieldI.getTexto; 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 weight in pounds x703 BMI height in inches Example: Jane weighs 150lbs and is 5 feet 4 inches tall. Janes height in inches is (5 12) 4 64 1. multiply the weight by 703. 150 703 105450 2. multiply the height by itself 64 x 64 4096 3. Finally divide the first figure by the second. 105450 L4096- 25.7 Janes BMI s 25.7 Bonus points (2pts): If the user enters invalid or empty input, you need to popup an invalid input message.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 += "\n\n 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 += "\n\n You're UnHealthy.";

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

                output += "\n\n 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 += "\n\n 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

Add a comment
Know the answer?
Add Answer to:
Java program Build the interface: A BMI calculator First: add a Layered Pane; drag it from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT