-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBMICalculator.java
100 lines (68 loc) · 3.74 KB
/
BMICalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//Name: Bob wang, frame.java
//Description: Create a BMI Calculator application that reads the user’s weight and height (either Metric or Imperial system) and then calculates and displays the user’s BMI.
//date: September 22,2024
//Skills: imports, GUI, variables, classes, objects, Java syntax
import javax.swing.JButton; //import jbutton
import javax.swing.JFrame; //import jframe
import javax.swing.JLabel; //import jlabel
import javax.swing.JOptionPane; //import JOPtionpane
import javax.swing.JTextField; //import JTextField
import java.awt.Color; //Import color
import java.awt.event.ActionEvent; //Import Actionevent
import java.awt.event.ActionListener; //Import actionlistener
public class frame extends JFrame implements ActionListener {
JLabel heightLabel=new JLabel("Height"); //Height label
JLabel weightLabel=new JLabel("Weight"); //Weight label
JLabel metersLabel=new JLabel("Meters"); //Meters label
JLabel kgLabel=new JLabel("Kilograms"); //Kilograms label
JTextField kgTextField=new JTextField("Kilograms"); //Kilograms text field
JTextField metersTextField=new JTextField("Meters"); //Meters text
JButton calculateButton=new JButton("Calculate"); //Creates a calculate button
public frame() { //Frame constructor class
setSize(550,500); //Default window size
setTitle("Bmi Calculator(adult)"); //Title that goes onto the window
setLayout(null);
//These comments are for my future reference
//getContentPane().setBackground(Color.RED);
//Color thecolor=new Color(10,10,10);
//getContentPane().setBackground(thecolor);
//Font labelfont=new font("Arial",Font.BOLD,24);
//heightlabel.setFont(labelfont)
heightLabel.setBounds(50,50,100,50); //Set the location of the height label
add(heightLabel); //Adds a heightlabel
weightLabel.setBounds(50,200,100,50); //Set the location of the weight label
add(weightLabel); //Add a weight label
metersLabel.setBounds(75,75,100,50); //Set the location of the meters label
add(metersLabel); //adds a meters label
kgLabel.setBounds(75,225,100,50); //Set the location of the kilograms label
add(kgLabel); //Adds the kilograms label
kgTextField.setBounds(75,275,100,50); //Set the location of the kilogram entry field
add(kgTextField); //Adds a kilograms text field
metersTextField.setBounds(75,125,100,50); //Set the location of the meters text field
add(metersTextField); //Add a meters text field
calculateButton.setBounds(200,350,100,50); //Sets the location of the calculate BMI button
calculateButton.addActionListener(this); //Adds an action listener to help the program know when its clicked
add(calculateButton); //Adds the calculate button
setVisible(true);
}
//this method run every time an action occur on the frame
@Override
public void actionPerformed(ActionEvent event) { //Method that performs the BMI calculation and displays it
//formula source: centers for disease control
if (event.getSource()==calculateButton) {
double bmi; //BMI variable
double weight=Double.valueOf(kgTextField.getText()); //Weight variable
double height=Double.valueOf(metersTextField.getText()); //Height variable
bmi=weight/Math.pow(height,2); //Calculates the BMI using the metric BMI formula
JOptionPane.showMessageDialog(this,bmi); //Displays a popup telling the user what the BMI value is
//JOptionPane.showMessageDialog(this,String.format("%.1f",bmi));
//string category;
//if (bmi<18.5){
//category="underweight"}
//if (bmi>18.5 && bmi<25{
// category="healthy"}
//else:
//{category="obese"}
}
}
}