FIT 100

Lab 10:  Procedures

Autumn 2001

Recommended Reading for Lab 10:

·         Chapter 13 of the FIT Course Pack

·         Review Chapter 7 of Learn to Program with Visual Basic 6.0

 

Introduction:

Class lecture introduced the concept of procedures and parameters as a way to encapsulate a process so that it can be used multiple times with multiple inputs.  This lab works out the solution of the Body Mass Index program.

 

Objectives:

  1. To use the idea of procedural abstraction as a way to compartmentalize a program’s computation.
  2. To write statements that use actual parameters in a procedure call.

 

TO DO: 

  1. Create a folder on the C drive called Lab10.

  2. Open up a new project in Visual Basic. 

 

  1. Rename the project BodyMassIndex. 

 

  1. Create a form with the following controls on it:

 

·         4 Labels

·         2 Command Buttons

·         2 Text Boxes

Name the form frmBMI.  Make the caption “Body Mass Index Calculator”.

  1. Spend a few minutes changing the names and captions of the controls that have been placed on the form.  Change the colors and fonts to something aesthetically pleasing.  This lab, you won’t be given the names for the objects. Decide on a naming scheme and be able to use the names of the objects appropriately in code. 

    **A good way to keep track of the names would be to print out the figure
    below and put the name you will give each control next to its image**

 

The form may look something like this when completed (use the colors and fonts of your choice):



 

Make these 2 controls invisible at first

 

Command Buttons

 

Text

Boxes

 

Labels

 
 

 

  1. Save your form and your project.

 

The Scenario:

This is the GUI that finds a user’s Body Mass Index.  The reason for finding a person’s BMI is based on a medical assumption that height and weight are directly related to whether or not a person is considered overweight or underweight.  This index is no longer considered a true test of a person’s weight problems, but it is a way to test class understanding of procedure declaration and procedure calls.

An index number over 25 would indicate that someone is overweight, while an index of less than 18.5 would indicate that someone is underweight.

 

A user sits in front of the computer and after entering their weight in pounds in the first text box and their height in inches in the second text box, they will click the “What’s my BMI” command button.

The user body mass index will display in the label to the left of the Try Again button. 

 

The steps below are to demonstrate how to write a procedure that has formal parameters to take in the input values of weight and height and a parameter to output the value of the Body Mass Index. 

 

Instead of declaring global variables at the top of code window under General Declarations, use local variables, declared within the Click event procedure of the “What’s my BMI” command button. 



  1. Declare 3 variables locally:  userWeight, userHeight and userBMI inside the click event procedure of the button named for finding the BMI.  They will hold integer values entered by the user and the result returned from the procedure call:

    Private Sub <name of  command button>_Click()

Dim userWeight As Integer
Dim userHeight As Integer

Dim userBMI As Double

            End Sub

           

  1. Underneath the variable declarations, inside of the command button click event, assign the value that a user enters into the text box for Weight to the variable userWeight. 

    Assign the value that a user enters into the text box for Height to the variable userHeight.

    userBMI will not be used just yet.  Initialize it with a value of 0.0 so you control the initial value.

 

Writing your findBMI Procedure:

  1. The click event procedure for the command button is done for the moment.  Now concentrate on declaring the procedure so that it can be called when needed.

  2. Write a procedure to calculate the Body Mass Index for a user.  Start by identifying the 4 specifications of a procedure.  What are they?

    Name: a way to refer to the procedure.  Name the procedure findBMI.

 

Definition: the instructions or procedure body, in this case the formulae for finding

the Body Mass Index:

 bodyMass = 4.89 * weightLBS / ((heightIN / 12) ^ 2)

           

            Parameters: The input and/or output variables used for this procedure:

                   (weightLBS As Integer, heightIN As Integer, bodyMass as Double)

 

            Declaration:  The entire package:  a procedure’s name, definition and parameters

is placed in the declaration.  The VB syntax (how it is written) is this:

                   Private Sub findBMI (weightLBS As Integer, heightIN As Integer, _

bodyMass as Double)

bodyMass = 4.89 * weightLBS / ((heightIN / 12) ^ 2)

                       

                        End Sub

 

Write the procedure declaration at the bottom of the code window.  When you need

to move to that area of your code, select General from the Objects drop-down menu

and find the name of the procedure in the Procedure drop-down menu.   

 

  1. Before you go further, save your project.

 

  1. There is now a procedure declared that will calculate the Body Mass Index when called. 

    Where is a logical place to call this procedure? 
    What would the actual parameters of the procedure call be?

    The logical place to call it is after the assignment statements in the click event (from step 8).
     
    The actual parameters would be the variables holding the user’s weight and height input values and the variable that will eventually hold the outcome of the Body Mass Index calculation.

 

Call findBMI (userWeight, userHeight, userBMI)

         

          After the call statement, userBMI will have a value in it (a result of the procedure).
 

          Assign the caption of the label that will show the BMI the following value:

“Your Body Mass Index is ” & userBMI &  “.  A BMI over 25 means you” & _

“are overweight.  A BMI under 18.5 means you are underweight.”

 

  1. Run your program. 

·         Can you explain what is happening at each step in the code?

·         Is a Body Mass Index displayed after entering in a weight value, height value and clicking on the command button?

·         Can the user continue entering in values after the first index has been calculated?

 

  1. Do what is needed so that the proper labels appear and disappear at the click of each button:

·         When the program is run, the label that will display the BMI and the Try Again command button should be hidden

·         After entering in values for weight and height and clicking the proper command button, the labels and text boxes for weight and height should disappear and the label for the BMI and the Try again button should appear

·         After clicking the Go Again button, the form should reset.

 

  1. Now, introduce a conditional to the program:

IF the userBMI is greater than 25, the label displays:

 

Your BMI of” & userBMI & “is an indication you may be overweight”

but if the userBMI is less than 18.5, the label displays:

 

Your BMI of” & userBMI & “is an indication you may be underweight”

 

          otherwise, the label displays:

 

                   Your BMI of” & userBMI & “shows your weight is normal.”

 

         

  1. Run the program.  Are all the conditions working?

 

  1. What happens if the user enter letters to the text boxes?  Using Lab 9, create a usable error trap for this program that will alert the user when they have entered the wrong data type.

 

  1. Save the project to disk or FTP it to your Dante account.  Show the working executable to your TA at the beginning of lab 11.

 

Are we having fun yet???!?!?!?!?!?!? J