hashihi raja
Ques1
1 Answer(s)      5 years and 10 months ago
Posted in : Java Beginners

hi,this is my java source for calculate BMI..

import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class MyPanel20Feb extends JPanel implements ActionListener { JTextField tfWeight,tfHeight,tfBMI; JButton bCalculate,bClear,bExit; JTextArea ta1;

public MyPanel20Feb()
{
    this.setLayout(new BorderLayout());
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    p1.add(new JLabel("Weight(kg)"));
    p1.add(tfWeight=new JTextField(5));
    p1.add(new JLabel("Height(m)"));
    p1.add(tfHeight=new JTextField(5));
    p1.add(new JLabel("BMI"));
    p1.add(tfBMI=new JTextField(15));
    this.add(p1,BorderLayout.NORTH);
    p2.add(ta1=new JTextArea());
    this.add(p2,BorderLayout.CENTER);
    p3.add(bCalculate=new JButton("CALCULATE"));
    p3.add(bClear=new JButton("CLEAR"));
    p3.add(bExit=new JButton("EXIT"));
    bCalculate.addActionListener(this);
    bClear.addActionListener(this);
    bExit.addActionListener(this);
    this.add(p3,BorderLayout.SOUTH);

}
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==bCalculate)
        {


            double berat=Double.parseDouble(tfWeight.getText());
            double tinggi=Double.parseDouble(tfHeight.getText());
            double bmi;

            bmi=berat/(tinggi*tinggi);

            tfBMI.setText(" "+bmi);
                if(bmi>0 && bmi < 18.5)
                {
                    ta1.setText("You are Underweight");
                    ta1.setBackground(Color.YELLOW);
                }
                else if(bmi>=18.5 && bmi <25.0)
                {
                    ta1.setText("You are Normal");
                    ta1.setBackground(Color.GREEN);
                }
                else if(bmi>=25.0)
                {
                    ta1.setText("You are Overweight");
                    ta1.setBackground(Color.RED);
                }

        }
        if(e.getSource()==bClear)
        {
            tfWeight.setText("");
            tfHeight.setText("");
            tfBMI.setText("");
            ta1.setText("");

        }
        else if(e.getSource()==bExit)
        {
            System.exit(0);
        }
    }

}

import javax.swing.*; public class MyFrame20Feb extends JFrame{ MyPanel20Feb p1=new MyPanel20Feb(); public MyFrame20Feb() { this.add(p1);

}

}

public class Test20Feb { public static void main(String[]args) { MyFrame20Feb f1=new MyFrame20Feb(); f1.pack(); f1.setVisible(true); f1.setSize(500,200); } }

what i need 2 do is: Provide proper exception handling to tackle the problem of user entering zero for the height value or entering a non-numerical input. If the height input is zero, the program will throw an exception and the catch() method will invoke the showMessageDialog() method from the JOptionPane class with a message to remind the user that the height cannot be zero, else if the user enter non-numerical input, the message will remind the user to enter numerical values.

anyone can modify this coding by includes the exception handling method in class MyPanel20Feb??

Ads

View Answers

February 22, 2011 at 1:53 PM


Java swing Height Weight Application

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;
import java.util.regex.*;

 class MyPanel20Feb extends JPanel implements ActionListener {
    JTextField tfWeight,tfHeight,tfBMI;
    JButton bCalculate,bClear,bExit;
    JTextArea ta1;

public MyPanel20Feb(){
    this.setLayout(new BorderLayout());
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    p1.add(new JLabel("Weight(kg)"));
    p1.add(tfWeight=new JTextField(5));
    p1.add(new JLabel("Height(m)"));
    p1.add(tfHeight=new JTextField(5));
    p1.add(new JLabel("BMI"));
    p1.add(tfBMI=new JTextField(15));
    this.add(p1,BorderLayout.NORTH);
    p2.add(ta1=new JTextArea());
    this.add(p2,BorderLayout.CENTER);
    p3.add(bCalculate=new JButton("CALCULATE"));
    p3.add(bClear=new JButton("CLEAR"));
    p3.add(bExit=new JButton("EXIT"));
    bCalculate.addActionListener(this);
    bClear.addActionListener(this);
    bExit.addActionListener(this);
    this.add(p3,BorderLayout.SOUTH);
}
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==bCalculate){
        String input1=tfWeight.getText();
        String input2=tfHeight.getText();
        Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
        Matcher m = p.matcher(input1);
        Pattern p1 = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
        Matcher m1 = p1.matcher(input2);
    if(input1.equals("")||input2.equals("")){
    JOptionPane.showMessageDialog(null, "Enter value for first textfield: ");
    }
    else if(input1.equals("0")){
    JOptionPane.showMessageDialog(null, "Not Allowed");
    tfWeight.setText("");
    }
    else if (m.find()) {
    JOptionPane.showMessageDialog(null, "Please enter only numbers");
    tfWeight.setText("");
    }
    else if(input2.equals("")){
    JOptionPane.showMessageDialog(null, "Enter value for second textfield: ");
     }
    else if(input2.equals("0")){
    JOptionPane.showMessageDialog(null, "Not Allowed");
    tfHeight.setText("");
     }  
    else if (m1.find()) {
     JOptionPane.showMessageDialog(null, "Please enter only numbers");
    tfHeight.setText("");
     }
     else{
           double berat=Double.parseDouble(tfWeight.getText());
            double tinggi=Double.parseDouble(tfHeight.getText());
            double bmi;
            bmi=berat/(tinggi*tinggi);
            tfBMI.setText(" "+bmi);
                if(bmi>0 && bmi < 18.5){
                    ta1.setText("You are Underweight");
                    ta1.setBackground(Color.YELLOW);
                }
                else if(bmi>=18.5 && bmi <25.0){
                    ta1.setText("You are Normal");
                    ta1.setBackground(Color.GREEN);
                }
                else if(bmi>=25.0){
                    ta1.setText("You are Overweight");
                    ta1.setBackground(Color.RED);
                }
     }
        }
                if(e.getSource()==bClear){
                tfWeight.setText("");
                tfHeight.setText("");
                tfBMI.setText("");
                ta1.setText("");
        }
        else if(e.getSource()==bExit) {
            System.exit(0);
        }
    }
}
 class MyFrame20Feb extends JFrame{
    MyPanel20Feb p1=new MyPanel20Feb();
    public MyFrame20Feb() {
        this.add(p1);
}
}
public class Test20Feb{ 
    public static void main(String[]args) {
        MyFrame20Feb f1=new MyFrame20Feb();
        f1.pack();
        f1.setVisible(true); 
        f1.setSize(500,200);
        }
        }









Related Tutorials/Questions & Answers:
Ques1
Ques1  hi,this is my java source for calculate BMI.. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyPanel20Feb extends JPanel implements ActionListener { JTextField tfWeight,tfHeight,tfBMI