Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.text.DecimalFormat;
- public class S0754012
- {
- static StringBuffer total = new StringBuffer();
- static JFrame frm = new JFrame("BMI calculator");
- static double height=0;
- static double weight=0;
- static double sum = 0;
- static String note = "";
- //static String heightAns;
- //static String weightAns;
- public static void main(String args[])
- {
- DecimalFormat df = new DecimalFormat("#.00");
- Container contentPane = frm.getContentPane();
- //declare
- JLabel title = new JLabel("BMI Calculator",JLabel.CENTER);
- JLabel height_Label = new JLabel("Height(cm) ",JLabel.CENTER);
- JLabel weight_Label = new JLabel("Weight(kg) ",JLabel.CENTER);
- JLabel result = new JLabel("Result ",JLabel.CENTER);
- JLabel display = new JLabel("",JLabel.CENTER);
- JTextField height_TextField = new JTextField(Double.toString(height),JTextField.CENTER);
- JTextField weight_TextField = new JTextField(Double.toString(weight),JTextField.CENTER);
- //adding
- contentPane.add(title);
- contentPane.add(height_Label);
- contentPane.add(weight_Label);
- contentPane.add(result);
- contentPane.add(display);
- contentPane.add(height_TextField);
- contentPane.add(weight_TextField);
- //setting attribute
- frm.setLayout(null);
- title.setBounds(70,0,200,50);
- title.setFont(new java.awt.Font("Arial",1,20));
- height_Label.setBounds(-20,50,170,50);
- height_Label.setFont(new java.awt.Font("Arial",1,20));
- weight_Label.setBounds(-20,100,170,50);
- weight_Label.setFont(new java.awt.Font("Arial",1,20));
- result.setBounds(-30,170,170,50);
- result.setFont(new java.awt.Font("Arial",1,20));
- display.setBounds(100,170,300,50);
- display.setFont(new java.awt.Font("Arial",1,20));
- height_TextField.setBounds(130,60,100,30);
- weight_TextField.setBounds(130,110,100,30);
- frm.setSize(450,300);
- frm.setVisible(true);
- //setting behavior
- height_TextField.getDocument().addDocumentListener(new DocumentListener()
- {
- public void changedUpdate(DocumentEvent e)
- {
- }
- public void insertUpdate(DocumentEvent e)
- {
- String heightAns = height_TextField.getText().trim();
- if(isNumeric(heightAns))
- {
- height = Double.parseDouble(heightAns);
- cal(display);
- }
- display.setText("BMI = " + df.format(sum) + "(" + note + ")");
- }
- public void removeUpdate(DocumentEvent e)
- {
- String heightAns = height_TextField.getText().trim();
- if(isNumeric(heightAns))
- {
- height = Double.parseDouble(heightAns);
- cal(display);
- }
- display.setText("BMI = " + df.format(sum) + "(" + note + ")");
- }
- });
- weight_TextField.getDocument().addDocumentListener(new DocumentListener()
- {
- public void changedUpdate(DocumentEvent e)
- {
- }
- public void insertUpdate(DocumentEvent e)
- {
- String weightAns = weight_TextField.getText().trim();
- if(isNumeric(weightAns))
- {
- weight = Double.parseDouble(weightAns);
- cal(display);
- }
- display.setText("BMI = " + df.format(sum) + "(" + note + ")");
- }
- public void removeUpdate(DocumentEvent e)
- {
- String weightAns = weight_TextField.getText().trim();
- if(isNumeric(weightAns))
- {
- weight = Double.parseDouble(weightAns);
- cal(display);
- }
- display.setText("BMI = " + df.format(sum) + "(" + note + ")");
- }
- });
- }
- public static void cal(JLabel display)
- {
- sum = weight / Math.pow(height/100, 2);
- if(sum < 18.5)
- {
- note = "Underweight";
- display.setForeground(Color.RED);
- }
- else if (sum >= 18.5 && sum < 24)
- {
- note = "Normal";
- display.setForeground(Color.BLUE);
- }
- else if (sum >= 24 && sum < 27)
- {
- note = "Overweight";
- display.setForeground(Color.RED);
- }
- else if (sum >= 27 && sum < 30)
- {
- note = "Mild Obesity";
- display.setForeground(Color.RED);
- }
- else if (sum >= 30 && sum < 35)
- {
- note = "Moderate Obesity";
- display.setForeground(Color.RED);
- }
- else
- {
- note = "Severe Obesity";
- display.setForeground(Color.RED);
- }
- }
- public static boolean isNumeric(String strNum)
- {
- if (strNum == null)
- {
- return false;
- }
- try
- {
- double d = Double.parseDouble(strNum);
- }
- catch (NumberFormatException nfe)
- {
- return false;
- }
- return true;
- }
- //close window
- static class ActLis extends WindowAdapter implements ActionListener
- {
- public void actionPerformed(ActionEvent e){
- JOptionPane.showMessageDialog(null,"close","Closing?",JOptionPane.WARNING_MESSAGE);
- }
- public void windowClosing(WindowEvent e) {
- System.out.print("window closing!");
- frm.dispose();
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement