Skip to content

Commit 3aae196

Browse files
Create BMICalculator.java
1 parent cef83b0 commit 3aae196

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package bmi.calculator;
7+
8+
import java.awt.Color;
9+
import java.awt.Font;
10+
import java.awt.Image;
11+
import java.awt.Toolkit;
12+
import java.awt.event.ActionEvent;
13+
import java.awt.event.ActionListener;
14+
import java.text.DecimalFormat;
15+
import javax.swing.JButton;
16+
import javax.swing.JFrame;
17+
import javax.swing.JLabel;
18+
import javax.swing.JOptionPane;
19+
import javax.swing.JTextField;
20+
import javax.swing.SwingConstants;
21+
/**
22+
*
23+
* @author Reshma
24+
*/
25+
public class BMICalculator implements ActionListener{
26+
JTextField tfHeight,tfWeight;
27+
static JLabel labelHeight,labelWeight,status,result,result2;
28+
JButton btn;
29+
JFrame frame;
30+
/**
31+
* @param args the command line arguments
32+
*/
33+
BMICalculator(){
34+
frame = new JFrame();
35+
Color c = new Color(240,128,128);
36+
frame.getContentPane().setBackground(c);
37+
Image icon = Toolkit.getDefaultToolkit().getImage("C:\\Documents\\NetBeansProjects\\BMI Calculator\\src\\logo\\logo.png");
38+
labelWeight = new JLabel("Weight in kg");
39+
labelWeight.setFont(new Font("Serif",Font.PLAIN,16));
40+
labelWeight.setForeground(Color.BLACK);
41+
labelWeight.setBounds(100,70,100,20);
42+
tfWeight = new JTextField();
43+
tfWeight.setToolTipText("Enter weight");
44+
tfWeight.setBounds(90,95,100,30);
45+
labelHeight = new JLabel("Height in cm");
46+
labelHeight.setFont(new Font("Serif",Font.PLAIN,16));
47+
labelHeight.setForeground(Color.BLACK);
48+
labelHeight.setBounds(100,130,100,20);
49+
tfHeight = new JTextField();
50+
tfHeight.setToolTipText("Enter height");
51+
tfHeight.setBounds(90,152,100,30);
52+
status = new JLabel();
53+
status.setHorizontalAlignment(SwingConstants.CENTER);
54+
status.setBounds(60,185,190,40);
55+
btn=new JButton("Calculate");
56+
btn.setBounds(95,230,90,30);
57+
btn.setBackground(Color.BLACK);
58+
btn.setForeground(Color.WHITE);
59+
result = new JLabel();
60+
result.setBounds(95,270,100,30);
61+
result.setHorizontalAlignment(SwingConstants.CENTER);
62+
result2 = new JLabel();
63+
result2.setBounds(50,300,190,60);
64+
result2.setHorizontalAlignment(SwingConstants.CENTER);
65+
result2.setOpaque(false);
66+
btn.addActionListener(this);
67+
frame.setIconImage(icon);
68+
frame.add(labelHeight);
69+
frame.add(labelWeight);
70+
frame.add(tfHeight);
71+
frame.add(tfWeight);
72+
frame.add(btn);
73+
frame.add(status);
74+
frame.add(result);
75+
frame.add(result2);
76+
frame.setSize(300,450);
77+
frame.setLayout(null);
78+
frame.setTitle("Height Calculator");
79+
frame.setVisible(true);
80+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
81+
frame.setResizable(false);
82+
}
83+
// Convert height in centimeter to meter
84+
public static double centimeterToMeter(double cm){
85+
double metre = cm/100;
86+
return metre;
87+
}
88+
// Calculate BMI
89+
public static double findBMI(double height,double weight){
90+
double h = centimeterToMeter(height);
91+
double bmi = weight/h/h;
92+
DecimalFormat df = new DecimalFormat("##.##");
93+
String trimBmi=df.format(bmi);
94+
result.setFont(new Font("Serif",Font.PLAIN,16));
95+
result.setForeground(Color.black);
96+
result.setText("BMI is "+trimBmi);
97+
status.setOpaque(false);
98+
return bmi;
99+
}
100+
// Display bmi by JLabel
101+
public static void setResult(double bmi){
102+
result2.setFont(new Font("Serif",Font.PLAIN,20));
103+
result2.setOpaque(true);
104+
if(bmi<15){
105+
String s = "<html>Very severely<br> underweight</html>";
106+
result2.setBackground(new Color(0,0,255));
107+
result2.setForeground(Color.WHITE);
108+
result2.setText(s);
109+
}
110+
else if(bmi>=15 && bmi<16){
111+
result2.setBackground(new Color(102,153,255));
112+
result2.setForeground(Color.WHITE);
113+
result2.setText("Severely Underweight");
114+
}
115+
else if(bmi>=16 && bmi<18.5){
116+
result2.setBackground(new Color(102,255,255));
117+
result2.setForeground(Color.black);
118+
result2.setText("Underweight");
119+
}
120+
else if(bmi>=18.5 && bmi<=24.9){
121+
result2.setBackground(Color.GREEN);
122+
result2.setForeground(Color.black);
123+
result2.setText("Healthy");
124+
}
125+
else if(bmi>=25 && bmi<=29.9){
126+
result2.setBackground(Color.yellow);
127+
result2.setForeground(Color.black);
128+
result2.setText("Overweight");
129+
}
130+
else if(bmi>=30 && bmi<40){
131+
result2.setBackground(Color.orange);
132+
result2.setForeground(Color.black);
133+
result2.setText("Obese");
134+
}
135+
else if(bmi>=40){
136+
result2.setBackground(Color.red);
137+
result2.setForeground(Color.white);
138+
result2.setText("Extremely Obese");
139+
}
140+
}
141+
public static void main(String[] args) {
142+
// TODO code application logic here
143+
BMICalculator bmicalculator = new BMICalculator();
144+
}
145+
@Override
146+
public void actionPerformed(ActionEvent e) {
147+
if(tfWeight.getText().trim().isEmpty()){
148+
status.setOpaque(true);
149+
status.setBackground(new Color(204,153,255));
150+
status.setFont(new Font("Serif",Font.PLAIN,18));
151+
status.setText("Weight cannot be empty");
152+
}
153+
else if(tfHeight.getText().trim().isEmpty()){
154+
status.setOpaque(true);
155+
status.setBackground(new Color(204,153,255));
156+
status.setFont(new Font("Serif",Font.PLAIN,18));
157+
status.setText("Height cannot be empty");
158+
}
159+
else{
160+
try{
161+
double wtf = Double.parseDouble(tfWeight.getText());
162+
double htf = Double.parseDouble(tfHeight.getText());
163+
if(wtf==0 || htf==0){
164+
status.setText("");
165+
status.setOpaque(false);
166+
result.setText("");
167+
result2.setText("");
168+
result2.setOpaque(false);
169+
JOptionPane.showMessageDialog(frame,"Height and weight cannot be 0");
170+
}
171+
else{
172+
double userbmi=findBMI(htf,wtf);
173+
setResult(userbmi);
174+
status.setText("");
175+
}
176+
} catch(NumberFormatException nfe){
177+
status.setOpaque(true);
178+
status.setText("Enter only numbers");
179+
status.setForeground(Color.DARK_GRAY);
180+
status.setBackground(new Color(204,153,255));
181+
status.setFont(new Font("Serif",Font.PLAIN,18));
182+
}
183+
}
184+
}
185+
186+
}

0 commit comments

Comments
 (0)