Sei sulla pagina 1di 3

I0060

06 Laboratory Exercise 1
Operators and Array
Objectives:

At the end of the exercise, the students should be able to:

 use operators
 apply array

Materials:

 Computer with Visual basic 2008 Express Edition installed


 Flash drive

Basic Principles:

Operators and arrays are useful when it comes to programming. It helps the program to make
decision within condition. With this activity, the student will understand the use of operators and
arrays on developing programs.

Procedures:

Activtity #1: Making a simple calculator

1. Open a new project and name it as OpAndArray. This will also serve as Challenged Exercise.
2. Add 17 Button Control and 1 Textbox Control to design a simple calculator.
3. Change the Name Property of Button Controls as follows:
• Button1 – btnClear • Button10 – btn4
• Button2 – btnDivide • Button11 – btn5
• Button3 – btnMultiply • Button12 – btn6
• Button4 – btnSubtract • Button13 – btn7
• Button5 - btnAdd • Button14 – btn8
• Button6 – btnEqual • Button15 – btn9
• Button7 - btn1 • Button16 - btn0
• Button8 - btn2 • Button 17 - btnDecimal
• Button9 – btn3

4. Change the Text Property of the Textbox and Buttons as follows:


• Textbox1 – txtDisplay • btn3 - 3
• btnClear - C • btn4 - 4
• btnDivide - / • btn5 - 5
• btnMultiply - * • btn6 - 6
• btnSubtract - - • btn7 -7
• btnAdd - + • btn8 - 8
• btnEqual - = • btn9 - 9
• btn1 - 1 • btn0 - 0
• btn2 - 2
5. Insert the following code below the Public Class Form1:

Dim firstNum, secondNum, ans as Single


Dim operation as String

06 Laboratory 1 *Property of STI


Page 1 of 3
I0060

NOTE: firstNum and secondNum are variables declared to hold the two (2) input values
to be computed, ans is the variable for the output and operation is used for declaring
the operation to use. Single is the data type used for the first three variable because it
could contain floating-point numbers that do not need the full data width.
6. Type the code below for:
• Button 1:
txtDisplay.Text = 1
• Button 2:
txtDisplay.Text = 2
• Button 3:
txtDisplay.Text = 3

7. Run the program. Click the 1, 2, and 3 button and observe what happened.
NOTE: When you click on one of the three buttons its corresponding value will appear in the
Textbox, but it will be replaced when another button is clicked.
8. Now, challenge yourself to come up with the appropriate code that will keep the value in
Textbox even when another button is clicked before executing the operation.
9. Copy the code below for plus and minus button.

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnAdd.Click
firstNum = Val(txtDisplay.Text)
txtDisplay.Text = ""
operation = "add"
End Sub

Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles btnSubtract.Click
firstNum = Val(txtDisplay.Text)
txtDisplay.Text = ""
operation = "subtract"
End Sub

NOTE: The operation to be executed depends on the value of the variable operation of
each button which is connected to the equal button.
10. Copy the code below for equal button.

Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles btnEqual.Click
secondNum = Val(txtDisplay.Text)

If operation = "add" Then


ans = firstNum + secondNum
txtDisplay.text = ans
ElseIf operation = "subtract" Then
ans = firstNum – secondNum
txtDisplay.Text = ans

NOTE: Conditional statement is used to make decision on which operation will be executed
based from the button clicked. The variable operation contains conditions that have
corresponding actions to be executed when a certain condition is met.
11. Run the program. Click the buttons 1, 2, 3, +, -, and = to perform calculation.

12. Finish the activity by completing the codes of the simple calculator. Save your work.

06 Laboratory 1 *Property of STI


Page 2 of 3
I0060

Figure 6.1 Form1 - Simple Calculator Interface

Activity #2: Array


1. On the same project, add new Windows Form.
2. Add one Button and one ListBox. Change the Button’s Text Property to OK.
3. Copy the code below for the OK button.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim num(4) As Integer
num(0) = 5
num(1) = 4
num(2) = 3
num(3) = 2
num(4) = 1

MsgBox("Let's countdown from 5 to 1!")


MsgBox(num(0))

MsgBox(num(1))
MsgBox(num(2))
MsgBox(num(3))
MsgBox(num(4))

End Sub

4. Run the program. Observe what happened.


5. Stop the program.
6. Challenge yourself using loop, to let the numbers five (5) to one (1) display in the ListBox instead
on the Message box after clicking the first Message box.

Figure 6.2 Form2 – Messagebox Figure 6.3 Form2 - Output


7. When done, save the laboratory exercise.

06 Laboratory 1 *Property of STI


Page 3 of 3

Potrebbero piacerti anche