FIT
100
Lab 10:
Procedures Autumn 2001 Recommended ·
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:
TO DO:
· 4 Labels · 2 Command Buttons ·
2 Text Boxes Name the form frmBMI. Make the caption “Body Mass Index
Calculator”.
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
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. 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 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.
Dim
userWeight As Integer Dim
userBMI As Double End
Sub
Writing
your findBMI Procedure:
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: 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.
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.”
· 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?
·
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.
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.”
Are
we having fun yet???!?!?!?!?!?!? J |