P. 1
BMI Calculator Java

BMI Calculator Java

|Views: 81|Likes:
Published by eyaknara
Java program for solving Body Mass Index
Java program for solving Body Mass Index

More info:

Published by: eyaknara on Oct 09, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

10/09/2012

pdf

text

original

/*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);

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

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->