You are on page 1of 2

/*this program is a calculator that computes for the Body Mass Index and prints the wether you

are normal, underweight, overweight or obese*/ import import import import import java.awt.*; java.awt.event.*; java.awt.event.ActionListener; javax.swing.*; javax.swing.event.*;

public class BMICalculator extends JFrame{ //textfields private private private private JTextField JTextField JTextField JTextField mField = new JTextField(4); // kgField = new JTextField(4); // bmiField = new JTextField(4); // resultField = new JTextField(4); height weight BMI // result

//buttons public BMICalculator() { JPanel content = new JPanel(); content.setLayout(new FlowLayout()); //number buttons JButton button1 = new JButton(" 1 "); JButton button2 = new JButton(" 2 "); JButton button3 = new JButton(" 3 "); JButton button4 = new JButton(" 4 "); JButton button5 = new JButton(" 5 "); JButton button6 = new JButton(" 6 "); JButton button7 = new JButton(" 7 "); JButton button8 = new JButton(" 8 "); JButton button9 = new JButton(" 9 "); JButton button0 = new JButton(" 0 "); JButton dotButton = new JButton(" . "); content.add(button1); content.add(button2); content.add(button3); content.add(button4); content.add(button5); content.add(button6); content.add(button7); content.add(button8); content.add(button9); content.add(button0); content.add(dotButton); button1.addActionListener(new numberListener()); //Create button for computing BMI and add action listener. JButton bmiButton = new JButton("Compute BMI"); bmiButton.addActionListener(new BMIListener()); //Set layout and add components. content.add(new JLabel("\nWeight in kilograms")); content.add(kgField); content.add(new JLabel("\nHeight in meters")); content.add(mField); content.add(bmiButton); content.add(new JLabel("\nYour BMI is")); content.add(bmiField);

content.add(new JLabel("\nYou are")); content.add(resultField); //Set the window characteristics. setContentPane(content); setTitle("Body Mass Index"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); } //BMIListener private class BMIListener implements ActionListener { public void actionPerformed(ActionEvent e) { double kilograms = Double.parseDouble(kgField.getText()); double meters = Double.parseDouble(mField.getText()); int bmi = (int)computeBMI(kilograms, meters); //value of BMI bmiField.setText("" + bmi); //conditions for the result if(bmi<20){ resultField.setText("Underweight"); }else if((bmi>20)&&(bmi<25)){ resultField.setText("Normal"); }else if((bmi>25)&&(bmi<30)){ resultField.setText("Overweight"); }else{ resultField.setText("Obese"); } } } //computes for BMI public static double computeBMI(double weight, double height) { return weight / (height * height); } private class numberListener implements ActionListener { public void actionPerformed(ActionEvent e) { kgField.setText("1"); mField.setText("1"); } } //main method public static void main(String[] args) { BMICalculator calculator = new BMICalculator(); calculator.setVisible(true); calculator.setSize(130,450); } }

You might also like