UCF homepage
Lars P. Linden
MIS Dept.
University of Central Florida

demo program

Lab 05

Decision Structures

About This Lab

Create an application that, given inputs, makes decisions about whether a company is "green" or not.

Why do this application?

Use this to start the application: Decision_Structures_starter [zip]


Review of the Interface

The interface has several controls to allow the input of data and one button to trigger the decision process.




Decision #1: Is the organization carbon neutral?

Technique: Use an If statement.

For this application, a company is considered "carbon neutral" if they purchase a value of carbon offsets greater than or equal to the estimated cost of their carbon-dioxide emissions.




Get the Text of the cost of carbon-dioxide emissions and store the value in a variable.

Get the Text of the carbon offsets purchased from a carbon credit exchange and store the value in a variable.

Dim sglCostOfCarbonDioxideEmissions As Single Dim sglCarbonOffestsPurchased As Single sglCostOfCarbonDioxideEmissions = CSng(txtCostOfCarbonDioxideEmissions.Text) sglCarbonOffestsPurchased = CSng(txtCarbonOffestsPurchased.Text)

Note that if you test this application and enter a non-numerical amount in one of these TextBoxes, the system will crash.

We solve this problem with an If statement and the IsNuman eric() function.

Below is the re-written code:

' test to see if what the user entered is a number If IsNumeric(txtCostOfCarbonDioxideEmissions.Text) And IsNumeric(txtCarbonOffestsPurchased.Text) Then ' it is a number sglCostOfCarbonDioxideEmissions = CSng(txtCostOfCarbonDioxideEmissions.Text) sglCarbonOffestsPurchased = CSng(txtCarbonOffestsPurchased.Text) Else ' not a number, so display an error message lblStatusMessage.Text = "Please enter a number" ' start over Return End If

Next, we need to consider the decision.

We want to use our intermediate result later in the overall "Green" assessment decision, so we will save it in a variable.

We want to save a True or a False, so create a Boolean variable.

Dim blnIsCarbonNeutral As Boolean

Now, code an If Then statement, to make the "Carbon Neutral" decision.

Assign True or False to this variable depending upon the result of the above decision. Once set, this variable will be used to during the overall "green" assessment.

' make decision If (sglCarbonOffestsPurchased >= sglCostOfCarbonDioxideEmissions) Then ' display result of this decision lblCarbonNeutralResult.Text = "Yes" ' set flag to be used for overall decision blnIsCarbonNeutral = True Else ' display result of this decision lblCarbonNeutralResult.Text = "No" ' set flag to be used for overall decision blnIsCarbonNeutral = False End If

Display an error message in the "lblStatusMessage" Label.


Decision #2:

Technique: Use several If statements.

The "Green Washing" section of the application looks like this:




Here is how it works!

The organization earns points, so declare a variable to store the value that is being calculated.

Dim intPointsForEffort As Integer = 0

Your programming task is to use If statements for each of these controls to add or subtract the respective points.


For each CheckBox the organization earns 1 point.

Dim intPointsForEffort As Integer = 0 ' add points if organization has a given initiative If chkLivingWage.Checked Then intPointsForEffort += 1 End If If chkGreenProcurement.Checked Then intPointsForEffort += 1 End If If chkRenewableEnergy.Checked Then intPointsForEffort += 1 End If

However, the company loses points for unsubstantiated green advertising (saying that they are green when they really aren't). See the ComboBox for these numbers.


' subtract points if organization is green washing with ads If CInt(cboGreenWashingAdvertising.SelectedIndex) = 0 Then intPointsForEffort += -3 End If If CInt(cboGreenWashingAdvertising.SelectedIndex) = 1 Then intPointsForEffort += -1 End If If CInt(cboGreenWashingAdvertising.SelectedIndex) = 2 Then intPointsForEffort += 0 End If

Finally, we need to display the final "Green Washing" score to the Label.

' display intermediate result lblGreenWashingScore.Text = intPointsForEffort.ToString ' display success message lblStatusMessage.Text = "OK"

Decision #3: The overall "green" assessment


Here are the interface details for the first decision:

Control Purpose
Label for output of a overall "green" assessment
Button this control triggers all decision (same as above)

Technique: Use a compound conditional.

Use the Boolean variable that contain the results from the first decisions and the "Green Washing" score.




If the organization is Carbon Neutral and has a positive "Green Washing" score, then the overall "green" assessment of the company is "Yes". Otherwise, "No".

Assign the results label accordingly.

'*************************************************** ' Decision #3: Overall '*************************************************** If blnIsCarbonNeutral And intPointsForEffort > 0 Then lblDecision.Text = "Yes" Else l End If

All done!





<< back to course home


Fall 2009