Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

GIVEN THIS JAVA CODE USING ECLIPSE IDE, WHY IS THE BMI TOTAL SHOWING THREE DOTS AND NOT THE BMI TOTAL AS IT IS EXPECTED?? HELP

GIVEN THIS JAVA CODE USING ECLIPSE IDE, WHY IS THE BMI TOTAL SHOWING THREE DOTS
AND NOT THE BMI TOTAL AS IT IS EXPECTED?? HELP FIX CODE
package edu.pupr.BMIClaculator;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BMIClaculator {
public static void main(String[] args){
JFrame frame = new JFrame("BMI Calculator");
frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel){
panel.setLayout(null);
JLabel userLabel = new JLabel("BMI");
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
JLabel weightLabel = new JLabel("How many pounds you weigh?");
weightLabel.setBounds(10,50,200,25);
panel.add(weightLabel);
JTextField weightText = new JTextField(20);
weightText.setBounds(220,50,50,25);
panel.add(weightText);
JLabel heightLabel = new JLabel("What is your height in inches?");
heightLabel.setBounds(10,80,200,25);
panel.add(heightLabel);
JTextField heightText = new JTextField(20);
heightText.setBounds(220,80,50,25);
panel.add(heightText);
JLabel bmiLabel = new JLabel("Your BMI is");
bmiLabel.setBounds(10,110,80,25);
panel.add(bmiLabel);
JLabel categoryLabel = new JLabel("(Obese)");
categoryLabel.setBounds(10,140,200,25);
panel.add(categoryLabel);
JButton calculateButton = new JButton("Calculate BMI");
calculateButton.setBounds(10,170,150,25);
panel.add(calculateButton);
calculateButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
try {
double weight = Double.parseDouble(weightText.getText());
double height = Double.parseDouble(heightText.getText());
double bmi = calculateBMI(weight, height);
bmiLabel.setText("Your BMI is: "+ bmi);
categoryLabel.setText(getBMICategory(bmi));
} catch (NumberFormatException ex){
JOptionPane.showMessageDialog(panel, "Please enter valid numerical values for weight and height.", "Input Error", JOptionPane.ERROR_MESSAGE);
}
}
});
}
private static double calculateBMI(double weight, double height){
return (weight /(height * height))*703;
}
private static String getBMICategory(double bmi){
if (bmi 18.5){
return "Underweight";
} else if (bmi >=18.5 && bmi 24.9){
return "Normal Weight";
} else if (bmi >=25 && bmi 29.9){
return "Overweight";
} else {
return "Obese";
}
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions