0% found this document useful (0 votes)
87 views

Questions and Solutions To Semester 2 Questions

The document contains 10 questions and solutions related to VB.NET programming. The questions cover topics like finding the maximum value in an array, calculating employee pay based on hours worked, checking if a word is a palindrome, calculating factorials with functions, building a basic calculator application, calculating student averages from subject marks, using database connectors for login validation, displaying array values greater than a number, using Try/Catch for error handling, and finding the greatest value in an array using a function.

Uploaded by

Mathy Mtenje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Questions and Solutions To Semester 2 Questions

The document contains 10 questions and solutions related to VB.NET programming. The questions cover topics like finding the maximum value in an array, calculating employee pay based on hours worked, checking if a word is a palindrome, calculating factorials with functions, building a basic calculator application, calculating student averages from subject marks, using database connectors for login validation, displaying array values greater than a number, using Try/Catch for error handling, and finding the greatest value in an array using a function.

Uploaded by

Mathy Mtenje
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

VB.

NET QUESTINS AND SOLUTIONS

Question 1

Write a program that finds the maximum in a an array of 5 integers

PublicClass Form1

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim arr(4) AsInteger
Dim i AsInteger
Dim temp AsInteger
arr(0) = TextBox1.Text
arr(1) = TextBox2.Text
arr(2) = TextBox3.Text
arr(3) = TextBox4.Text
arr(4) = TextBox5.Text
temp = arr(0)
For i = 0 To 4
If arr(i) > temp Then
temp = arr(i)
EndIf
Next
MsgBox(" The highest is : "& temp)
EndSub
EndClass

Question 2

write a program that calculates the daily remune of an employee according to


the following scheme of payment

• 0 to 8 - $2 per hour

• 9-12 - $2,50 per hour

• 12-15 - $3,00 per hour using a function, catch any errors that may
present

solution
PublicClass Form1

Function remune(ByVal x AsDouble) AsDouble


Try
SelectCase x
Case 1 To 8
Return (2 * x)
Case 9 To 12
Return ((2 * 8) + 2.5 * (x - 8))
Case 13 To 15
Return ((2 * 8) + (2.5 * 4) + (3 * (x - 12)))
EndSelect
Catch ex As Exception
EndTry
EndFunction
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim hrs AsInteger
hrs = TextBox1.Text
TextBox2.Text = remune(hrs)
EndSub
EndClass

Question 3

Write a program that tests if a given word is a palindrome or not


PublicClass Form1

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim word AsString
word = TextBox1.Text
If word = StrReverse(word) Then
MsgBox((word) &" is a palindrome")
Else
MsgBox((word) &" is not a palindrome")
EndIf
EndSub
EndClass

Question 4

Write a program that calculates the factorial of a number using a function


PublicClass Form1
Function fact(ByVal x AsInteger) AsInteger
If x = 0 Then
Return 1
Else
'For i = x To 1 Step -1 (comments)
Return x * fact(x - 1)(nested if loop)
'Next I (comments)
EndIf
EndFunction

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim m AsInteger
m = TextBox1.Text
MsgBox(" factorial is : "& fact(m))
EndSub
EndClass
Question 5

Write a programme that can be used as coding for a calculator


PublicClass Form1
Dim bocac As Boolean
Function fact(ByVal x AsInteger) AsInteger
If x = 0 Then
Return 1
Else
'For i = x To 1 Step -1
Return x * fact(x - 1)
'Next i
EndIf
EndFunction

PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) HandlesMyBase.Load
lblnum.Text = "p"
lblsign.Text = "p"
TextBox1.Text = "0"
EndSub

PrivateSub Butt1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Butt1.Click, Butt0.Click, Butt2.Click,
Butt3.Click, Butt4.Click, Butt5.Click, Butt6.Click, Butt7.Click,
Butt8.Click, Butt9.Click
If bocac Then
TextBox1.Text = ""
bocac = False
EndIf
If TextBox1.Text = "0"Then
TextBox1.Text = sender.text
Else
TextBox1.Text = TextBox1.Text & sender.text
EndIf
EndSub

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles a.Click, d.Click, s.Click, m.Click
bocac = False
SelectCase Trim(lblsign.Text)
Case"-"
TextBox1.Text = Val(lblnum.Text) - Val(TextBox1.Text)
Case"+"
TextBox1.Text = Val(lblnum.Text) + Val(TextBox1.Text)
Case"/"
If TextBox1.Text = "0"Then
TextBox1.Text = "ERROR 1"
Else
TextBox1.Text = Val(lblnum.Text) / Val(TextBox1.Text)
EndIf
Case"*"
TextBox1.Text = Val(lblnum.Text) * Val(TextBox1.Text)
EndSelect
lblnum.Text = TextBox1.Text
lblsign.Text = sender.text
bocac = True
EndSub
PrivateSub comma_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles comma.Click
If bocac Then
TextBox1.Text = ""
bocac = False
EndIf
If TextBox1.Text = "0"Then
TextBox1.Text = "0."
ElseIf InStr(TextBox1.Text, ".") = 0 Then
TextBox1.Text = TextBox1.Text &"."
EndIf
EndSub

PrivateSub e_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles e.Click
bocac = False
SelectCase Trim(lblsign.Text)
Case"-"
TextBox1.Text = Val(lblnum.Text) - Val(TextBox1.Text)
Case"+"
TextBox1.Text = Val(lblnum.Text) + Val(TextBox1.Text)
Case"/"
If TextBox1.Text = "0"Then
TextBox1.Text = "ERROR 1"
Else
TextBox1.Text = Val(lblnum.Text) / Val(TextBox1.Text)
EndIf
Case"*"
TextBox1.Text = Val(lblnum.Text) * Val(TextBox1.Text)
EndSelect
lblnum.Text = "p"
lblsign.Text = "p"
bocac = True
EndSub

PrivateSub Button1_Click_1(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
TextBox1.Text = "0"
lblnum.Text = "P"
lblsign.Text = "p"
bocac = False
EndSub

PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Dim f AsDouble
Dim x AsInteger
x = TextBox1.Text
f = fact(x)
TextBox1.Text = f
EndSub

PrivateSub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox1.TextChanged

EndSub
EndClass
Question 6

Write a VB.NET program that calculates an average mark for a student, for 6
subjects, and displays it on the message box. Draw a diagram showing the
interface for capturing the marks. Your program should utilize this interface.
[15]
PublicClass Form1

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim arr(5) AsInteger
Dim sum AsDouble
Dim i AsInteger
sum = arr(0)
arr(0) = TextBox1.Text
arr(1) = TextBox2.Text
arr(2) = TextBox3.Text
arr(3) = TextBox4.Text
arr(4) = TextBox5.Text
arr(5) = TextBox6.Text
For i = 0 To 5
sum = sum + arr(i)
Next
Dim ave AsDouble = sum / 6

MsgBox("The average of the six subjects is "& ave)


EndSub

PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
EndSub

EndClass
Question 7

Write a program that can be used as login screen making reference to


information stored in another database employing the use of Database
connectors
Imports system.data
PublicClass Form1

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim con AsNew OleDb.OleDbConnection
Dim data AsNew OleDb.OleDbDataAdapter

Dim dset AsNew DataSet


con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0 ; Data
Source=C:\Users\user\Documents\passwords.mdb"
con.Open()
Dim sql AsString
sql = "select * from tblpasswords"
data = New OleDb.OleDbDataAdapter(sql, con)
data.Fill(dset, "login")
Dim rows AsInteger
rows = dset.Tables("login").Rows.Count
For i AsInteger = 1 To rows
If TextBox1.Text = dset.Tables("login").Rows(i - 1).Item(0) AndTextBox2.Text
= dset.Tables("login").Rows(i - 1).Item(1) Then
Dim v AsString
v = dset.Tables("login").Rows(i - 1).Item(0)
MsgBox("Access granted Welcome "& v)
ExitSub
EndIf
Next

For i AsInteger = 1 To rows


If TextBox1.Text <> dset.Tables("login").Rows(i - 1).Item(0) And
TextBox2.Text <> dset.Tables("login").Rows(i - 1).Item(1) Then
MsgBox("access denied")
ExitFor

EndIf
Next

con.Close()
EndSub

Errors on this program it does not loop for three times (yet!)
Question 8

Write a VB.net program that takes values from an array of five numbers and
shows the values greater than 10

Public Class Form1

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


System.EventArgs) Handles Button1.Click
Dim Arr(4) As Integer
Arr(0) = TextBox1.Text
Arr(1) = TextBox2.Text
Arr(2) = TextBox3.Text
Arr(3) = TextBox4.Text
Arr(4) = TextBox5.Text
Dim i As Integer
For i = 0 To 4
If Arr(i) > 10 Then
ListView1.Items.Add(Arr(i))(lists the values in a list box)
End If
Next
End Sub

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


System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
ListView1.Items.Clear() (clears a list box)
End Sub
End Class

OR
.
.
.
Label1.Text = Label1.Text & " , " & Arr(i)
.
.
Question 9

Write a VB.net program that calculates the triangular of number, employ the
use of the Try Catch statement

Public Class Form1


Function tri(ByVal x As Integer) As Double
If x <= 0 Then
Return 0
Else
Return x + tri(x - 1)
End If
End Function

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


System.EventArgs) Handles Button1.Click
Try
Dim num As Integer
num = TextBox1.Text
MsgBox("The triangular value of " & num & " is " & tri(num))
Catch ex As Exception
MsgBox("An Invalid entry has been made")
End Try
End Sub
End Class

Question 10

Write a program that calculates the greatest value in an array of 5 using a


function

Public Class Form1


Dim max As Integer = 0
Function greatest(ByVal x As Integer) As Integer
If x > max Then
max = x
Return max
End If
End Function

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


System.EventArgs) Handles Button1.Click
Dim arr(4) As Integer
arr(0) = textbox1.text
arr(1) = textbox2.text
arr(2) = textbox3.text
arr(3) = textbox4.text
arr(4) = TextBox5.Text
For i As Integer = 0 To 4
greatest(arr(i))
Next
MsgBox("The greatest value is " & max)
End Sub
End Class
Question

You might also like