Introduction

The BMI calculator is a computer programme designed to calculate your BMI returning the results under, ok, over and obsess. The calculation is your weight divided by your height squared.

The calculator has two input boxes, one for height in cm and one for weight in kg and one results box.

Other controls include a clear button which clears the form ready for inputs and a close button which closes the BMI Calculator


 

Test Number

Task and Test Data

Expected Result

Actual Result and Comments

  1.  

Compile the project

The project compiles without error and displays the GUI for user interface

The project compiled and ran without error

  1.  

Enter alpha characters abc into the height entry box and click the Calculate BMI button

An error window appears saying that the height should be a numeric value in cm

  1.  

Enter the numeric value 98 into the height entry box and click the Calculate BMI button

An error window appears saying that the height should be in the range 100cm to 300cm

  1.  

Enter the numeric value 302 into the height entry box and click the Calculate BMI button

An error window appears saying that the height should be in the range 100cm to 300cm

  1.  

Enter alpha characters qaz into the weight entry box and click the Calculate BMI button

An error window appears saying that the height should be a numeric value in cm

  1.  

Enter the numeric value 18 into the weight entry box and click the Calculate BMI button

An error window appears saying that the height should be a numeric value in cm

  1.  

Enter the numeric value 185 into the height entry box and enter alpha characters qaz into the weight entry box and click the Calculate BMI button

An error window appears saying that the weight should be a numeric value in kg

  1.  

Enter the numeric value 185 into the height entry box and enter the numeric value 28 into the weight entry box and click the Calculate BMI button

An error window appears saying that the weight should be in the range 30kg to 160kg

  1.  

Enter the numeric value 185 into the height entry box and enter the numeric value 175 into the weight entry box and click the Calculate BMI button

An error window appears saying that the weight should be in the range 30kg to 160kg

  1.  

Enter the numeric value 185 into the height box and the numeric value 50 into the weight box and click the Calculate BMI button

The calculator generates the result Under, and sets the background of the results box to yellow.

 

The actual BMI should be 14.609203

  1.  

Enter the numeric value 185 into the height box and the numeric value 65 into the weight box and click the Calculate BMI button

The calculator generates the result Ok, and sets the background of the results box to green.

 

The actual BMI should be 18.991964

  1.  

Enter the numeric value 185 into the height box and the numeric value 88 into the weight box and click the Calculate BMI button

The calculator generates the result Over, and sets the background of the results box to orange.

 

The actual BMI should be 25.712198

  1.  

Enter the numeric value 185 into the height box and the numeric value 100 into the weight box and click the Calculate BMI button

The calculator generates the result Obese, and sets the background of the results box to red.

 

The actual BMI should be 29.218407

  1.  

Click the clear button

All of the text fields are cleared and the focus is set to the height box

  1.  

Click the Close BMI Calculator button

The BMI Calculator closes

The BMI Calculator closes

  1.  

Click the close button on the title bar

The BMI Calculator closes

The BMI Calculator closes

  1.  

Click the maximise button on the title bar

The window is maximised

The window is maximised

  1.  

Click the minimise button on the title bar

The window is minimised

The window is minimised

 


Appendix 1

The source Code

/*

 * BMICalculator.java

 *

 * Created on 06 November 2008, 14:23

 */

 

package bmicalculator;

//import the java classes awt and swing

import java.awt.*;                          //import the java class awt

import javax.swing.*;                       //import the java class swing

 

/**

 *

 * @author  Andrew Jones

 */

public class BMICalculator extends javax.swing.JFrame {

 

 /** create a list of variable for gathering data, error checking and calculation use in the BMI Calculator

 *

 */

    private float height=0;                 //create the private variable height

    private float weight=0;                 //create the private variable weight

    private float result=0;                 //create the private variable result, used in calaculation of bmi

    private boolean errorHEIGHT;            //create the private variable errorHEIGHT, used as error flag

    private boolean errorWEIGHT;            //create the private variable errorWEIGHT, used as error flag

 

          

 

    public BMICalculator()                  //Creates new form BMICalculator

    {                                       //open bracket for gui initialisation

/** position and label the window and set the focus to height text box

 *

 */       

   super("BMI Calculator");                 //give the gui a title of BMI Calculator

        this.setLocation(100, 100);         //set the location of the gui x 100 y 100 from top left

        initComponents();                   //initialise the gui components

        txtHEIGHT.requestFocus(true);       //request the focus to be the height text box

        txtHEIGHT.setFocusable(true);       //set the focus to the text box height

    }                                       //close bracket for gui initialisation

 

    /** This method is called from within the constructor to

     * initialize the form.

     * WARNING: Do NOT modify this code. The content of this method is

     * always regenerated by the Form Editor.

     */

    @SuppressWarnings("unchecked")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         

    private void initComponents() {

 

        labelHEIGHT = new javax.swing.JLabel();

        labelWEIGHT = new javax.swing.JLabel();

        txtHEIGHT = new javax.swing.JTextField();

        txtWEIGHT = new javax.swing.JTextField();

        calculateBMI = new javax.swing.JButton();

        txtBMIresult = new javax.swing.JTextField();

        clearButton = new javax.swing.JButton();

        closeButton = new javax.swing.JToggleButton();

 

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

 

        labelHEIGHT.setText("Input your HEIGHT in centimeters (cm)");

 

        labelWEIGHT.setText("Input your WEIGHT in kilogrammes (kg)");

 

        txtHEIGHT.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        txtHEIGHT.setToolTipText("Enter your height in cm. Valid range 100cm to 300cm");

        txtHEIGHT.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                txtHEIGHTActionPerformed(evt);

            }

        });

 

        txtWEIGHT.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        txtWEIGHT.setToolTipText("Enter your weight in kg. Valid range 30kg to 160kg  ");

        txtWEIGHT.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                txtWEIGHTActionPerformed(evt);

            }

        });

 

        calculateBMI.setText("Calculate BMI");

        calculateBMI.setToolTipText("Click to calculate BMI");

        calculateBMI.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                calculateBMIActionPerformed(evt);

            }

        });

 

        txtBMIresult.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

        txtBMIresult.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        txtBMIresult.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                txtBMIresultActionPerformed(evt);

            }

        });

 

        clearButton.setText("Clear");

        clearButton.setToolTipText("Click to clear height and weight boxes");

        clearButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                clearButtonActionPerformed(evt);

            }

        });

 

        closeButton.setText("Close BMI Calculator");

        closeButton.setToolTipText("Click to close the BMI Calculator");

        closeButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                closeButtonActionPerformed(evt);

            }

        });

 

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addContainerGap()

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                    .addComponent(labelWEIGHT, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)

                    .addComponent(labelHEIGHT, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)

                    .addGroup(layout.createSequentialGroup()

                        .addComponent(txtHEIGHT, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addContainerGap())

                    .addGroup(layout.createSequentialGroup()

                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)

                            .addComponent(txtBMIresult, javax.swing.GroupLayout.Alignment.LEADING)

                            .addComponent(closeButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                            .addComponent(clearButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                            .addComponent(calculateBMI, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                            .addComponent(txtWEIGHT, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))

                        .addContainerGap())))

        );

        layout.setVerticalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addContainerGap()

                .addComponent(labelHEIGHT)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(txtHEIGHT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(labelWEIGHT)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(txtWEIGHT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addGap(18, 18, 18)

                .addComponent(calculateBMI)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(txtBMIresult, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addGap(18, 18, 18)

                .addComponent(clearButton)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)

                .addComponent(closeButton)

                .addContainerGap())

        );

 

        pack();

    }// </editor-fold>                       

 

private void txtHEIGHTActionPerformed(java.awt.event.ActionEvent evt) {                                         

// TODO add your handling code here:

   

}                                        

 

private void txtWEIGHTActionPerformed(java.awt.event.ActionEvent evt) {                                         

// TODO add your handling code here:

}                                        

 

private void calculateBMIActionPerformed(java.awt.event.ActionEvent evt) {                                            

 

/**check to validate that height is entered as a numeric value

 * using a try catch statement. Set error flags and then if error set the focus

 * to the height text box

 */

try                                                     //try statement

{                                                       //opening bracket for try statement

height = Float.parseFloat(txtHEIGHT.getText());         //asign the variable height from the text box txtHeight value as a float

errorHEIGHT = false;                                    //set the error flag errorHEIGHT to flase, there has been no error

}                                                       //closing bracket for try statement

catch (NumberFormatException n)                         //catch number format exception aplha characters entered instead of numeric value

    {                                                   //openning bracket of the catch statement

    JOptionPane.showMessageDialog(                      //show message dialog box

            this,                                       //using this, the dialog box

            "You must enter a valid number for height in cm",   //set the message text of the dialog box

            "Invalid Input",                            //set the title of the dialog box

            JOptionPane.ERROR_MESSAGE);                 //invoke the dialog box

        txtHEIGHT.requestFocus(true);                   //set the focus to txtHEIGHT text box

        txtHEIGHT.setFocusable(true);                   //grant the focus to the txtHEIGHT text box

        txtHEIGHT.setText("");                          //set the contents of the txtHEIGHT box to nothing

        errorHEIGHT = true;                             //set the error flag errorHEIGHT to true, there has been an error

    }                                                   //closing bracket for the catch statement

 

/** if the error flag is true test to see that height is within the acceptable range 100-300cm

 *  generate a error message if out of range, else convert height in cm to meters and set the error flag

 */

if ( errorHEIGHT == false)                              //if the error flag errorHEIGHT is set to false

{                                                       //opening bracket for if statement evaluateing as true

if ((height <100) || (height>300))                      //if height is less than 100 or greater than 300

    {                                                   //opening bracket for if statement evaluating as true           

   JOptionPane.showMessageDialog(                       //show message dialog box

           this,                                        //using this, the dialog box

           "You must enter a valid height in the range 100cm to 300cm",     //set the message text of the dialog box

           "Invalid Input",                             //set the title of the dialog box

           JOptionPane.ERROR_MESSAGE);                  //invoke the dialog box

        txtHEIGHT.requestFocus(true);                   //set the focus to txtHEIGHT text box

        txtHEIGHT.setFocusable(true);                   //grant the focus to the txtHEIGHT text box

        txtHEIGHT.setText("");                          //set the contents of the txtHEIGHT box to nothing

        errorHEIGHT = true;                             //set the error flag errorHEIGHT to true, there has been an error

    }                                                   //closing bracket for the catch statement

    else                                                //else statement

    {                                                   //opening bracket for else statement

    height = (height/100);                              //convert the height to meters by dividiing the height in cm by 100

    errorHEIGHT =false;                                 //set the error flag errorHEIGHT to flase

    }                                                   //closing bracket for the else statement

}                                                       //clsoing bracket for if statement evaluating as true

/** evaluate error flag for height to ensure no errors

 *  check to validate that weight is entered as a numeric value

 *  using a try catch statement.

 *  Set error flags and then if error set the focus to the weight text box

 */

if (errorHEIGHT ==false)                                //if statement the error flag errorHEIGHT is equal to false

{                                                       //opening bracket for the if statement evaluating as true

try{                                           

weight = Float.parseFloat(txtWEIGHT.getText());         //asign weight to value of the txtWEIGHT box

errorWEIGHT = false;                                    //set the error flag errorWEIGHT to false

    }                                                   //closing bracket for the try statement

catch (NumberFormatException n)                         //catch number exception for alpha characters entered

    {                                                   //opening bracket for the catch statement

    JOptionPane.showMessageDialog(                      //show message dialog box

            this,                                       //using this, the dialog box

            "You must enter a valid number for weight in kg",       //set the message text of the dialog box

            "Invalid Input",                            //set the title of the dialog box

            JOptionPane.ERROR_MESSAGE);                 //invoke the dialog box

        txtWEIGHT.requestFocus(true);                   //set the focus to txtWEIGHT text box

        txtWEIGHT.setFocusable(true);                   //grant the focus to the txtWEIGHT text box

        txtWEIGHT.setText("");                          //set the contents of the txtWEIGHT box to nothing

        errorWEIGHT = true;                             //set the error flag errorWEIGHT to true, there has been an error

    }                                                   //closing bracket for the catch statement

 

/**evaluate error flag then if true

 * test to see that weight is within the acceptable range 30-160kg

 * if height is out of range generate an error message, set the focus

 * to weight text box and set the error flag

 */

if (errorWEIGHT==false)                                 //if error flag errorWEIGHT is equal to false

{                                                       //opening bracket for if statement evaluating as true

    if((weight <30) || (weight>160))                    //if weight is less than 30 or greater than 160

    {                                                   //opening bracket for if statement evaluating as true

        JOptionPane.showMessageDialog(                  //show message dialog box

                this,                                   //using this, the dialog box

                "You must enter a valid weight in the range 30kg to 160kg",           //set the message text of the dialog box

                "Invalid Input",                        //set the title of the dialog box

                JOptionPane.ERROR_MESSAGE);             //invoke the dialog box

        txtWEIGHT.requestFocus(true);                   //set the focus to txtWEIGHT text box

        txtWEIGHT.setFocusable(true);                   //grant the focus to the txtWEIGHT text box

        txtWEIGHT.setText("");                          //set the contents of the txtWEIGHT box to nothing

        errorWEIGHT = true;                             //set the error flag errorWEIGHT to true, there has been an error

    }                                                   //closing bracket for the if statement

    else                                                //else statement

    {                                                   //opening bracket for else statement

      errorWEIGHT = false;                              //set the error flag errorWEIGHT to flase

    }                                                   //closing bracket for else statement

}                                                       //clsoing bracket for if statement evaluating as true

 

/**check to see that there are no errors and calculate the bmi to variable result

 * and print the result to the system console

 */

if((errorHEIGHT == false) && (errorWEIGHT == false))    //if both error flags errorHEIGHT and errorWEIGHT are equal to false

{                                                       //opening bracket for if statement evaluating as true

    result = weight/(height * height);                  //the calaculation result equals weight divided by height squared

    System.out.println(result);                         //print the result to the system console

/** generate the output for bmi calculation under, ok, over or obese

 *  test to check that bmi result is within ranges 18.5 or less,

 *  18.5 - 24.9, 25.0 - 25.9 or greater than 25.9

 *  set the background colour of the results box yellow, green, orange or red

 */

if (result <= 18.5)                                     //if the result is less than or equal to 18.5

{ txtBMIresult.setText("Under");                        //set the text box txtBMIresult to a value of under

  txtBMIresult.setBackground(Color.YELLOW);}            //set the background colour of the text box txtBMIresult to yellow

else if ((result > 18.5) && (result <= 24.9))           //else if result is greater than 18.5 and less than equal 24.9

{ txtBMIresult.setText("Ok");                           //set the text box txtBMIresult to a value of ok

  txtBMIresult.setBackground(Color.GREEN);}             //set the background colour of the text box txtBMIresult to green

else if ((result > 24.9) && (result <= 25.9))           //else if the result is greater than 24.9and less than or equal 25.9

{ txtBMIresult.setText("Over");                         //set the text box txtBMIresult to a value of over

  txtBMIresult.setBackground(Color.ORANGE);}            //set the background colour of the text box txtBMIresult to orange

else if (result >25.9)                                  //else if the result is greater than 25.9

{ txtBMIresult.setText("Obese");                        //set the text box txtBMIresult to a value of obses

  txtBMIresult.setBackground(Color.RED);}               //set the background colour of the text box txtBMIresult to red

}                                                       //clsoing bracket for if statement evaluating as true

 

}                                                       //closing bracket for the if statement evaluating as true

 

}

 

private void txtBMIresultActionPerformed(java.awt.event.ActionEvent evt) {                                            

// TODO add your handling code here:

}                                           

 

private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

/** clear the height, weight and results text boxes

 *  reset the variables to zero

 *  set the focus to the height text box

 *  set the background of the result box to white

 */

    txtHEIGHT.setText("");                              //set the value of txtHEIGHT to nothing

    txtWEIGHT.setText("");                              //set the value of txtWEIGHT to nothing

    txtBMIresult.setText("");                           //set the value of txtBMIresult to nothing

    height = 0;                                         //set the value of height to zero

    weight = 0;                                         //set the value of weight to zero

    result = 0;                                         //set the value of result to zero

    txtHEIGHT.requestFocus(true);                       //set the focus to txtHEIGHT text box

    txtHEIGHT.setFocusable(true);                       //grant the focus to the txtHEIGHT text box

    txtBMIresult.setBackground(Color.WHITE);            //set the background colour of txtBMIresult text box to white

}                                          

 

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

/** close the BMI calculator on click

 *

 */

System.exit(0);                                         //clsoe the BMI Calculator using system exit true

}                                          

 

    /**

    * @param args the command line arguments

    */

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new BMICalculator().setVisible(true);

            }

        });

    }

 

    // Variables declaration - do not modify                    

    private javax.swing.JButton calculateBMI;

    private javax.swing.JButton clearButton;

    private javax.swing.JToggleButton closeButton;

    private javax.swing.JLabel labelHEIGHT;

    private javax.swing.JLabel labelWEIGHT;

    private javax.swing.JTextField txtBMIresult;

    private javax.swing.JTextField txtHEIGHT;

    private javax.swing.JTextField txtWEIGHT;

    // End of variables declaration                  

 

}