meljun cortes itc 22 midterm exercises
TRANSCRIPT
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
1/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
MIDTERM PERIOD
SAMPLE EXERCISE #1Building a Visual Basic Application
Launch Microsoft Visual Basic.
Create the Source Code Window, producing the output below:
Figure 2.1 Source Code Window
Visualize the List of Objects:
Figure 2.2: List of Objects
Page 1 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
2/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
Visualize the List of Objects:
Figure 2.3: List of Procedures
Key in the lines in between the above two statements.
Private Sub Form_Load ( )Form1.showPrint Welcome to Visual Basic tutorial
End Sub
Run the program, press F5 or click on the run button to run the program.
SAMPLE EXERCISE #2
Simple Arithmetic Calculations
Perform simple arithmetic calculations as shown in example 2.1.2. VB uses * to denote the
multiplication operator and / to denote the division operator.
The output is shown in figure 2.3, where the results are arranged vertically.
Page 2 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
3/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
Use the ff. Source Code:
Private Sub Form_Activate ( )Print 20 + 10Print 20 - 10Print 20 * 10Print 20 / 10
End Sub
Use the ff. Source Code for another set:
Private Sub Form_Activate ( )x = 20y = 10Print x + yPrint x - yPrint x * yPrint x / y
End Sub
SAMPLE EXERCISE #3
Besides, you can also use the + or the & operator to join two or more texts (string) together like
in example 2.1.4 (a) and (b)
Private Sub Form_Activate( )A = "Tom"B = likes"C = to"D = eat"E = burger"Print A + + B + + C + + D + + E
End Sub
Sample Exercise #4Private Sub cmdnext_Click()
Form2.Show
Form1.HideEnd Sub
Private Sub Form_Activate()Print "Welcome to VB"
End Sub
Private Sub cmdexit_Click()End
Page 3 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
4/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
End Sub
Private Sub Form_Activate()
Print "VB is fun and easy"End Sub
SAMPLE EXERCISE #5
Introduction to Programming Concept (OOP)
The procedure to calculate and to display the output on the label is shown below. The output is
shown in Figure 3.1.
Objects/Controls Name Text Event /Procedure
Form1 Form1 None Load
Label1 lblNum1 Number 1 NoneLabel2 lblNum2 Number 2 None
Label3 lblAnswer Answer None
TextBox1 txtNum1 None None
TextBox2 txtNum2 None None
TextBox3 txtNum3 None NoneCommandButton1 CmdSum Addition Click
CommandButton2 CmdClear Clear Click
Code:
Dim num1,num2,result As IntegerPrivate Sub cmdSum_Click()num1 = Val(txtNum1.Text)num2 = Val(txtNum2.Text)result = numb1 + num2txtNum3.Text = result
End Sub
Private Sub cmdclear_Click()txtNum1.Text = ""txtNum2.Text = ""
txtNum3.Text = ""End Sub
GRADED EXERCISE #1 LASTNAMEEXER1
Introduction to Programming Concept (OOP)
1. Modify the calculator program (EXERCISE#1) by allowing the user to select from any of the
four fundamental operations and display the correct answer.
Page 4 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
5/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
Dim a,b,c As IntegerPrivate Sub cmdSum_Click()
a = Val(txtNum1.Text)b = Val(txtNum2.Text)c = a + btxtNum3.Text = c
End Sub
Private Sub cmdclear_Click()txt1.Text = ""txt2.Text = ""txt3.Text = ""
End Sub
Private Sub cmddiv_Click()a = Val(txt1.Text)b = Val(txt2.Text)c = a / btxt3.Text = c
End Sub
Private Sub cmdexit_Click()End
End Sub
Private Sub cmdmul_Click()a = Val(txt1.Text)b = Val(txt2.Text)c = a * btxt3.Text = c
End Sub
Private Sub cmdsub_Click()a = Val(txt1.Text)b = Val(txt2.Text)c = a - btxt3.Text = c
End Sub
GRADED EXERCISE 2 LASTNAMEEXER2
Page 5 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
6/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
2. Write a program that will input three different integers and print the sum, the average, and the
product of these numbers. Provide an Exit button to terminate program execution.
Dim num1, num2, num3, sum, prod As Integer
Dim ave As Double
Private Sub cmdAve_Click()
num1 = Val(txt1.Text)
num2 = Val(txt2.Text)
num3 = Val(txt3.Text)ave = (num1 + num2 + num3) / 3
txt4.Text = ave
End Sub
Private Sub cmdclear_Click()
txt1.Text = ""txt2.Text = ""
txt3.Text = ""
txt4.Text = ""End Sub
Private Sub cmdexit_Click()
EndEnd Sub
Private Sub cmdprod_Click()num1 = Val(txt1.Text)
num2 = Val(txt2.Text)
num3 = Val(txt3.Text)prod = num1 * num2 * num3
txt4.Text = prod
End Sub
Private Sub cmdsum_Click()
num1 = Val(txt1.Text)
num2 = Val(txt2.Text)
num3 = Val(txt3.Text)sum = (num1 + num2 + num3)
txt4.Text = sumEnd Sub
GRADED EXERCISE 3 LASTNAMEEXER3
3. Write a temperature conversion program that converts a Fahrenheit temperature to a Celsius
temperature. Provide a Textbox for user input and another Textbox for displaying the converted
Page 6 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
7/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
temperature. Also provide the user with an Exit button to end program execution. Use the
following formula: Celsius 5 / 9 * (Fahrenheit 32).
GRADED EXERCISE 4 LASTNAMEEXER4
4. Write a program that reads in the radius of a circle as an integer and prints the circles diameter,circumference and area. Provide textboxes for each of these calculations. Use the following
formulas:
diameter = 2 * radius
circumference = 2 * 3.1415 * radiusarea = 3.1415 * radius * radius
PRACTICE EXERCISE #2
Control Structures in VB 6.0
Data-Entry Validation Problem Using Multiple If/Then/Else Structure
1. Create an application that will validate a value entered. If the value is between 100 and 200 then the value
is accepted. Otherwise rejected.
Object Property Settings
Form Name frmValidate
Caption Validation
Textbox Name txtEntry
Text None
TabIndex 0
Button Name cmdAcceptCaption Accept
Enabled False
Code:
Private Sub cmdAccept_Click( )
Dim Valuentry As Integer
ValueEntry = val(txtEntry.Text)
If (ValueEntry >= 100) Then
msgbox Entry Accepted
ElseIf (ValueEntry > 200) Then
msgbox Invalid
EndIf
EndIf
End Sub
The preceding code seems to be the right code for the problem. In fact, there is no error when you
compile the program. Unfortunately, the code always revert to true whenever the input is greater than
100 rendering the code after the else statement useless. Lets modify the code:
Page 7 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
8/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
Private Sub cmdAccept_Click( )
Dim Valuentry As IntegerValueEntry = val(txtEntry.Text)
If (ValueEntry >= 100) And (ValueEntry < 200) Then
msgbox Entry AcceptedElse
msgbox Invalid
EndIf
End Sub
Private Sub txtEntry_change( )
cmdAccept.Enabled = True
End Sub
2. Compute for the Gross Pay wherein the formula is HoursWorked multiplied to Rate per hour only if the
number of hours worked is less than or equal to 40 hours. Otherwise, an hours in excess of 40 is 1.5 therate per hour.
Object Property Settings
Form Name FrmPayroll
Caption Simple Payroll
Textbox Name txtHoursWorked
Text
Textbox Name txtRate
Text
Textbox Name txtOTHours
Text
Textbox Name txtOTpay
Text
Textbox Name txtBasicPay
Text
Textbox Name txtGrossPay
Text
Label Name lblHours
Caption Hours Worked
Label Name lblRate
Caption Rate
Label Name lblOTHours
Caption OT HoursLabel Name lblOTPay
Caption OT Pay
Label Name lblBasicPay
Caption Basic Pay
Label Name lblGrossPay
Caption Gross Pay
Button Name cmdCompute
Code:
Page 8 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
9/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
Private Sub cmdCompute_Click()
Dim Rate, HoursWorked, OTHours, OTPay, Basic, Gross As Integer
Rate = Val(txtRate.Text)HoursWorked = Val(txtHoursWorked.Text)
OTHours = Val(txtOTHours.Text)OTPay = Val(txtOTPay.Text)
Basic = Val(txtBasicPay.Text)
Gross = Val(txtGrossPay.Text)
If (HoursWorked 40) ThenOTHours = HoursWorked 40
OTPay = Rate * OTHours * 1.5
Basic = Rate * 40
Gross = Basic + OTPay
txtOTHours.Text = OTHours
txtOTPay.Text = OTPay
txtBasicPay.Text = BasictxtGrossPay.Text = Gross
End If
End If
End Sub
PRACTICE EXERCISE #3
Using ListBox and ComboBox Controls In Visual Basic 6
1. Open a new Standard EXE project is opened an named the Form as Listbox.frm and save the
project as Listbox.vbp2. Design the application as shown below.
Object Property Settings
Form Caption
Name
ListBox
frmListBox
TextBoxText
Name
(empty)
TxtName
Label
Caption Enter a name
Page 9 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
10/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
Name lblName
ListBox Name FirstName
LabelCaption
Name
Amount Entered
lblAmount
Label
Caption
Name
Border Style
(empty)
lblDisplay
1 Fixed Single
CommandButton Caption
Name
Add
cmdAdd
CommandButtonCaption
Name
Remove
cmdRemove
CommandButtonCaption
Name
Clear
cmdClear
CommandButtonCaption
Name
Exit
cmdExit
3. The following event procedures are entered for the TextBox and CommandButton controls.
Private Sub txtName_Change()If (Len(txtName.Text) > 0) Then 'Enabling the Add button'if atleast one character
Page 10 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
11/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
'is enteredcmdAdd.Enabled = TrueEnd If
End Sub
Private Sub cmdAdd_Click()lstName.AddItem txtName.Text 'Add the entered the characters to the list boxtxtName.Text = "" 'Clearing the text box
txtName.SetFocus 'Get the focus back to the'text box
lblDisplay.Caption = lstName.ListCount 'Display the number of items in the list box
cmdAdd.Enabled = False ' Disabling the Add button
End Sub
4. The click event of the Add button adds the text to the list box that was typed in the Text box.Then the text box is cleared and the focus is got to the text box. The number of entered values
will is increased according to the number of items added to the listbox.
Private Sub cmdClear_Click()lstName.ClearlblDisplay.Caption = lstName.ListCountEnd Sub
Private Sub cmdExit_Click()Unload MeEnd Sub
Private Sub cmdRemove_Click()Dim remove As Integer
remove = lstName.ListIndex 'Getting the index
If remove >= 0 Then 'make sure an item is selected'in the list box
lstName.RemoveItem remove 'Remove item from the list box
lblDisplay.Caption = lstName.ListCount 'Display the number of items'in the listbox
End If
End Sub
Page 11 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
12/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
PRACTICE EXERCISE #3
Using ListBox and ComboBox Controls In Visual Basic 6
1. The following example illustrates the use of CheckBox control
2. Open a new Project and save the Form as CheckBox.frm and save the Project asCheckBox.vbp
3. DDesign the Form as shown below
Object Property Setting
FormCaption
Name
CheckBox
frmCheckBox
CheckBoxCaption
Name
Bold
ChkBold
CheckBoxCaption
Name
Italic
ChkItalic
CheckBoxCaption
Name
Underline
ChkUnderline
OptionButtonCaption
Name
Red
OptRed
OptionButtonCaption
Name
Blue
OptBlue
OptionButtonCaption
Name
Green
OptGreen
TextBoxName
Text
TxtDisplay
(empty)
CommandButtonCaption
Name
Exit
CmdExit
Page 12 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
13/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
4. Following code is typed in the Click() events of the CheckBoxes
Private Sub chkBold_Click()If chkBold.Value = 1 ThentxtDisplay.FontBold = True
ElsetxtDisplay.FontBold = False
End IfEnd Sub
Private Sub chkItalic_Click()If chkItalic.Value = 1 ThentxtDisplay.FontItalic = True
Else
txtDisplay.FontItalic = FalseEnd IfEnd Sub
Private Sub chkUnderline_Click()
If chkUnderline.Value = 1 ThentxtDisplay.FontUnderline = True
ElsetxtDisplay.FontUnderline = False
End If
End Sub
Following code is typed in the Click() events of the OptionButtons
Private Sub optBlue_Click()txtDisplay.ForeColor = vbBlue
End Sub
Private Sub optRed_Click()
txtDisplay.ForeColor = vbRed
End Sub
Page 13 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
14/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
Private Sub optGreen_Click()
txtDisplay.ForeColor = vbGreenEnd Sub
To terminate the program following code is typed in the Click() event of the Exit button
Private Sub cmdExit_Click()End
End Sub
5. Run the program by pressing F5. Check the program by clicking on OptionButtons and
CheckBoxes.
PRACTICE EXERCISE #4
Using ListBox and ComboBox Controls In Visual Basic 6
1. Open a new Standard EXE project and the save the Form as Option.frm and save the project as
Option.vbp.
Design the Form as per the following specifications table.
Object Property Settings
LabelCaption
Name
Enter a Number
Label1
TextBoxText
Name
(empty)
Text1
CommandButtonCaption
Name
&Close
Command1
OptionButtonCaption
Name
&Octal
OptOct
OptionButtonCaption
Name
&Hexadecimal
OptHex
OptionButtonCaption
Name
&Decimal
OptDec
The application responds to the following events.
Page 14 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
15/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
The change event of the TextBox reads the value and stores it in a form-level numeric variable.
The click event of optOct button returns curretval in octal.
The click event of the optHex button curerntval in hexadecimal The click event of the optDec button returns the decimal equivalent of the value held currentval.
The following code is entered in the general declarations section of the Form.
Dim currentval as variant
The variable is initialized to 0 by default. The change event procedure checks to ascertain the
number system (Octal, Hexadecimal) that is in effect and then reads in the number.
Private Sub Text1_Change()
If optOct.Value = True Thencurrentval = Val ("&O" & LTrim (Text1.Text) & "&")Elseif optDec.value = True Thencurrentval = Val (LTrim (Text1.Text) & "&")Elsecurrentval = Val ("&H" & LTrim (Text1.Text) & "&")End ifEnd Sub
The Val function is used to translate string to a number and can recognize Octal and Hexadecimal
strings. The LTrim function trims the leading blanks in the text. The following code is entered in theclick events of the OptionButton controls.
Private Sub optOct_Click()Text1.Text = Oct(currentval)End Sub
Private Sub optHex_Click()Text1.Text = Hex(currentval)End Sub
Private Sub optDec_Click()Text1.Text = Format(currentval)End Sub
The following code is entered in the click event of the Close button.
Private Sub cmdClose_Click()
Unlod MeEnd Sub
PRACTICE EXERCISE #4
Using ListBox and ComboBox Controls In Visual Basic 6
Page 15 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
16/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
1. Open a new Project and save the Form as messageboxdemo.frm and save the Project as
messageboxdemo.vbp
2. Design the application as shown below.Object Property Setting
Form
Caption
Name
MessageBoxDemo
frmMessageBoxDemo
Label
Caption
Name
lblName
Name
TextBox Name
Text
txtName
( empty )
ListBox Name LstName
CommandButton
Caption
Name
Add
CmdAdd
CommandButton
Caption
Name
Delete
CmdDelete
CommandButton
Caption
Name
Exit
CmdExit
3. Following code is entered in the txtName_Change ( ) event.
Private Sub txtName_Change()If Len(txtName.Text) > 0 Then
Page 16 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
17/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
cmdAdd.Enabled = TrueEnd IfEnd Sub
4. Following code has to be entered in the cmdAdd_Click ( ) event.
Private Sub cmdAdd_Click()answer = MsgBox("Do you want to add this name to the list box?", vbExclamation +vbYesNo,"Add Confirm")If answer = vbYes ThenlstName.AddItem txtName.TexttxtName.Text = ""txtName.SetFocuscmdAdd.Enabled = FalseEnd IfEnd Sub
Following code is entered in the cmdDelete_Click ( ) event
Private Sub cmdDelete_Click()Dim remove As Integerremove = lstName.ListIndexIf remove < 0 ThenMsgBox "No names is selected", vbInformation, "Error"Elseanswer = MsgBox("Are you sure you want to delete " & vbCrLf & "the selected name?",_vbCritical + vbYesNo, "Warning")If answer = vbYes ThenIf remove >= 0 ThenlstName.RemoveItem removetxtName.SetFocusMsgBox "Selected name was deleted", vbInformation, "Delete Confirm"
End IfEnd IfEnd IfEnd Sub
5. Following code is entered in the cmdExit_Click ( ) event
Private Sub cmdExit_Click()answer = MsgBox("Do you want to quit?", vbExclamation + vbYesNo, "Confirm")If answer = vbYes ThenEndElseMsgBox "Action canceled", vbInformation, "Confirm"
Page 17 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
18/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
End IfEnd Sub
6. Save and run the application. You can notice the different type of message box types are used toperform an action.
PRACTICE EXERCISE #5
Using ListBox and ComboBox Controls In Visual Basic 6
1. Open a new project and save the Form as InputBox.frm and save the Project as InputBox.vbp
2. Design the application as shown below.
Object Property Setting
Form
Caption
Name
InputBox test
FrmInputBox
Label
Caption
Name
You entered
lbl1
Label
Caption
Name
BorderStyle
( empty)
lbl2
1-FixedSingle
CommandButton
Caption
Name
OK
cmdOK
Page 18 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
19/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
3. Following code is entered in cmdOK_Click ( ) event
Private Sub cmdok_Click()Dim ans As Stringans = InputBox("Enter something to be displayed in the label", "Testing", 0)If ans = "" Thenlbl2.Caption = "No message"Elselbl2.Caption = ansEnd IfEnd Sub
4. Save and run the application. As soon as you click the OK button you will get the following
InputBox
Page 19 of 20
-
7/27/2019 MELJUN CORTES ITC 22 Midterm Exercises
20/20
JOSE RIZAL UNIVERSITY Computer Science Department
ITC 22 Database Management Systems
LABORATORY EXERCISES
PRACTICE EXERCISE #6
Using Select Case, ListBox and ComboBox Controls In Visual Basic 6
1. Genetic Computer Institute wants to install a security keypad outside a laboratory room. Only
authorized personnel may enter the laboratory using their security codes. The following are valid
and authorized codes:a. 2001 2003 Technicians
b. 2004 2009 Marketing Services
c. 2010 2015 Lecturersd. 2016 2020 Managers
2. Write a security program that accepts only the following codes using Select Case statement.
3. Write a program that displays the names of cities in Metro Manila in a ComboBox. When anitem is selected from the ComboBox remove it.
4. Modify your solution in #1. When the user selects an item from the ComboBox, remove the item
from the ComboBox and add it to the ListBox.
5. Write a program that will allow the user to type the text in a textbox and transfer it to the Listbox
and clear the textbox. In case the inputted text needs correction, the user should be able to selectthe item and return it in the textbox for correction and then back again.
6. Write a program that allows the user to enter a series of values in a Listbox to be translated intoFahrenheit or Celsius temperatures. When the user selects one of the items in the listbox, the
program has button options to translate the item into Celsius or Fahrenheit degree.
Page 20 of 20