Question: Ques1

Ads

 
 
 

Share on Google+Share on Google+

hashihi raja
Ques1
1 Answer(s)      6 years and 3 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);
        }
        }

Ads









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
Tutorials   
Java Spring Hibernate Struts Training Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include?

Ads

 
Advertisement null

Ads