Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1.  
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import java.text.DecimalFormat;
  8.  
  9. public class S0754012
  10. {
  11. static StringBuffer total = new StringBuffer();
  12. static JFrame frm = new JFrame("BMI calculator");
  13. static double height=0;
  14. static double weight=0;
  15. static double sum = 0;
  16. static String note = "";
  17.  
  18.  
  19. //static String heightAns;
  20. //static String weightAns;
  21.  
  22. public static void main(String args[])
  23. {
  24. DecimalFormat df = new DecimalFormat("#.00");
  25. Container contentPane = frm.getContentPane();
  26.  
  27. //declare
  28.  
  29. JLabel title = new JLabel("BMI Calculator",JLabel.CENTER);
  30. JLabel height_Label = new JLabel("Height(cm) ",JLabel.CENTER);
  31. JLabel weight_Label = new JLabel("Weight(kg) ",JLabel.CENTER);
  32. JLabel result = new JLabel("Result ",JLabel.CENTER);
  33. JLabel display = new JLabel("",JLabel.CENTER);
  34.  
  35.  
  36. JTextField height_TextField = new JTextField(Double.toString(height),JTextField.CENTER);
  37. JTextField weight_TextField = new JTextField(Double.toString(weight),JTextField.CENTER);
  38.  
  39. //adding
  40.  
  41. contentPane.add(title);
  42. contentPane.add(height_Label);
  43. contentPane.add(weight_Label);
  44. contentPane.add(result);
  45. contentPane.add(display);
  46.  
  47. contentPane.add(height_TextField);
  48. contentPane.add(weight_TextField);
  49.  
  50.  
  51. //setting attribute
  52.  
  53. frm.setLayout(null);
  54.  
  55. title.setBounds(70,0,200,50);
  56. title.setFont(new java.awt.Font("Arial",1,20));
  57.  
  58. height_Label.setBounds(-20,50,170,50);
  59. height_Label.setFont(new java.awt.Font("Arial",1,20));
  60.  
  61. weight_Label.setBounds(-20,100,170,50);
  62. weight_Label.setFont(new java.awt.Font("Arial",1,20));
  63.  
  64. result.setBounds(-30,170,170,50);
  65. result.setFont(new java.awt.Font("Arial",1,20));
  66.  
  67. display.setBounds(100,170,300,50);
  68. display.setFont(new java.awt.Font("Arial",1,20));
  69.  
  70.  
  71. height_TextField.setBounds(130,60,100,30);
  72. weight_TextField.setBounds(130,110,100,30);
  73.  
  74.  
  75. frm.setSize(450,300);
  76. frm.setVisible(true);
  77.  
  78. //setting behavior
  79.  
  80.  
  81. height_TextField.getDocument().addDocumentListener(new DocumentListener()
  82. {
  83. public void changedUpdate(DocumentEvent e)
  84. {
  85.  
  86. }
  87. public void insertUpdate(DocumentEvent e)
  88. {
  89. String heightAns = height_TextField.getText().trim();
  90. if(isNumeric(heightAns))
  91. {
  92. height = Double.parseDouble(heightAns);
  93.  
  94. cal(display);
  95. }
  96.  
  97.  
  98. display.setText("BMI = " + df.format(sum) + "(" + note + ")");
  99.  
  100. }
  101. public void removeUpdate(DocumentEvent e)
  102. {
  103. String heightAns = height_TextField.getText().trim();
  104. if(isNumeric(heightAns))
  105. {
  106. height = Double.parseDouble(heightAns);
  107.  
  108. cal(display);
  109. }
  110.  
  111.  
  112. display.setText("BMI = " + df.format(sum) + "(" + note + ")");
  113.  
  114.  
  115. }
  116. });
  117.  
  118. weight_TextField.getDocument().addDocumentListener(new DocumentListener()
  119. {
  120. public void changedUpdate(DocumentEvent e)
  121. {
  122.  
  123. }
  124. public void insertUpdate(DocumentEvent e)
  125. {
  126. String weightAns = weight_TextField.getText().trim();
  127. if(isNumeric(weightAns))
  128. {
  129. weight = Double.parseDouble(weightAns);
  130.  
  131. cal(display);
  132. }
  133.  
  134.  
  135. display.setText("BMI = " + df.format(sum) + "(" + note + ")");
  136.  
  137.  
  138. }
  139. public void removeUpdate(DocumentEvent e)
  140. {
  141. String weightAns = weight_TextField.getText().trim();
  142. if(isNumeric(weightAns))
  143. {
  144. weight = Double.parseDouble(weightAns);
  145.  
  146. cal(display);
  147. }
  148.  
  149.  
  150. display.setText("BMI = " + df.format(sum) + "(" + note + ")");
  151.  
  152.  
  153. }
  154. });
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. }
  166. public static void cal(JLabel display)
  167. {
  168. sum = weight / Math.pow(height/100, 2);
  169.  
  170. if(sum < 18.5)
  171. {
  172. note = "Underweight";
  173. display.setForeground(Color.RED);
  174. }
  175. else if (sum >= 18.5 && sum < 24)
  176. {
  177. note = "Normal";
  178. display.setForeground(Color.BLUE);
  179. }
  180. else if (sum >= 24 && sum < 27)
  181. {
  182. note = "Overweight";
  183. display.setForeground(Color.RED);
  184. }
  185. else if (sum >= 27 && sum < 30)
  186. {
  187. note = "Mild Obesity";
  188. display.setForeground(Color.RED);
  189. }
  190. else if (sum >= 30 && sum < 35)
  191. {
  192. note = "Moderate Obesity";
  193. display.setForeground(Color.RED);
  194. }
  195. else
  196. {
  197. note = "Severe Obesity";
  198. display.setForeground(Color.RED);
  199. }
  200. }
  201.  
  202. public static boolean isNumeric(String strNum)
  203. {
  204. if (strNum == null)
  205. {
  206. return false;
  207. }
  208. try
  209. {
  210. double d = Double.parseDouble(strNum);
  211. }
  212. catch (NumberFormatException nfe)
  213. {
  214. return false;
  215. }
  216. return true;
  217. }
  218.  
  219.  
  220. //close window
  221. static class ActLis extends WindowAdapter implements ActionListener
  222. {
  223. public void actionPerformed(ActionEvent e){
  224. JOptionPane.showMessageDialog(null,"close","Closing?",JOptionPane.WARNING_MESSAGE);
  225. }
  226. public void windowClosing(WindowEvent e) {
  227. System.out.print("window closing!");
  228. frm.dispose();
  229. System.exit(0);
  230. }
  231.  
  232. }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement