Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Visual Basic: Create a Class file named Generic LinearSearch that contains a gen

ID: 3577088 • Letter: V

Question

Visual Basic:

Create a Class file named Generic LinearSearch that contains a generic method named Search which implements the linear search algorithm. Method Search should compare the search key with each element in the array until the search key is found or until the end or the array is reached. If the search key is found, return its location in the array; otherwise return -1.

The type parameter for method Search should be constrained with IComparable(Of T) so that you can use the method CompareTo to compare the search key to the elements in the array. This class will later be used by the GUI in Figure 1 (below).

The interface will provide buttons that the user can click to create arrays of random integer or double values. The values generated for the arrays should be displayed in the ListBox so that the user knows what values can be searched for. The user will enter an integer or double in the search key TextBox and click the Search button to begin the search. The search will utilize the generic Search method. The Label at the bottom of the form will be used to report the results of the search. Figure 2 (below) shows an example of an integer search.

Generic linear Search E- O x E Generic Linear Search O O FX Create Create Create Create doubles integers integers doubles Index Value 895 783 943 409 Enter Search Key: 409 Enter Search Key: Search Search Found value in index 3 Figure 2- Integer Search Result Figure 1 Test Application Interface

Explanation / Answer

SearchInterface.Designer.vb


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class SearchInterface
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.createIntegersButton = New System.Windows.Forms.Button()
        Me.createDoublesButton = New System.Windows.Forms.Button()
        Me.searchButton = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.searchKeyTextbox = New System.Windows.Forms.TextBox()
        Me.resultLabel = New System.Windows.Forms.Label()
        Me.dataListBox = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        '
        'createIntegersButton
        '
        Me.createIntegersButton.Location = New System.Drawing.Point(12, 12)
        Me.createIntegersButton.Name = "createIntegersButton"
        Me.createIntegersButton.Size = New System.Drawing.Size(83, 59)
        Me.createIntegersButton.TabIndex = 0
        Me.createIntegersButton.Text = "Create integers"
        Me.createIntegersButton.UseVisualStyleBackColor = True
        '
        'createDoublesButton
        '
        Me.createDoublesButton.Location = New System.Drawing.Point(194, 12)
        Me.createDoublesButton.Name = "createDoublesButton"
        Me.createDoublesButton.Size = New System.Drawing.Size(83, 59)
        Me.createDoublesButton.TabIndex = 1
        Me.createDoublesButton.Text = "Create doubles"
        Me.createDoublesButton.UseVisualStyleBackColor = True
        '
        'searchButton
        '
        Me.searchButton.Location = New System.Drawing.Point(12, 286)
        Me.searchButton.Name = "searchButton"
        Me.searchButton.Size = New System.Drawing.Size(265, 23)
        Me.searchButton.TabIndex = 2
        Me.searchButton.Text = "Search"
        Me.searchButton.UseVisualStyleBackColor = True
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(12, 249)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(93, 13)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Enter Search Key:"
        '
        'searchKeyTextbox
        '
        Me.searchKeyTextbox.Location = New System.Drawing.Point(111, 246)
        Me.searchKeyTextbox.Name = "searchKeyTextbox"
        Me.searchKeyTextbox.Size = New System.Drawing.Size(166, 20)
        Me.searchKeyTextbox.TabIndex = 4
        '
        'resultLabel
        '
        Me.resultLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.resultLabel.Location = New System.Drawing.Point(12, 324)
        Me.resultLabel.Name = "resultLabel"
        Me.resultLabel.Size = New System.Drawing.Size(265, 23)
        Me.resultLabel.TabIndex = 5
        '
        'dataListBox
        '
        Me.dataListBox.FormattingEnabled = True
        Me.dataListBox.Location = New System.Drawing.Point(12, 77)
        Me.dataListBox.Name = "dataListBox"
        Me.dataListBox.Size = New System.Drawing.Size(265, 160)
        Me.dataListBox.TabIndex = 6
        '
        'SearchInterface
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(288, 362)
        Me.Controls.Add(Me.dataListBox)
        Me.Controls.Add(Me.resultLabel)
        Me.Controls.Add(Me.searchKeyTextbox)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.searchButton)
        Me.Controls.Add(Me.createDoublesButton)
        Me.Controls.Add(Me.createIntegersButton)
        Me.Name = "SearchInterface"
        Me.Text = "Generic Linear Search"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents createIntegersButton As System.Windows.Forms.Button
    Friend WithEvents createDoublesButton As System.Windows.Forms.Button
    Friend WithEvents searchButton As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents searchKeyTextbox As System.Windows.Forms.TextBox
    Friend WithEvents resultLabel As System.Windows.Forms.Label
    Friend WithEvents dataListBox As System.Windows.Forms.ListBox

End Class


GenericLinearSearch.vb

Public Class GenericLinearSearch

    Public Function Search(Of T As IComparable(Of T))(x As T, y As T())

        For i As Integer = 0 To y.Length - 1 Step 1
            If x.CompareTo(y(i)) = 0 Then
                Return i
            End If
        Next
        Return -1
    End Function

End Class


SearchInterface.vb

Public Class SearchInterface

    Dim rand As New Random
    Dim searchClass As New GenericLinearSearch()
    Dim integerArray(10) As Integer
    Dim doubleArray(10) As Double

    Private Sub createIntegersButton_Click(sender As Object, e As EventArgs) Handles createIntegersButton.Click
        dataListBox.Items.Clear()
        resultLabel.Text = ""
        Array.Clear(doubleArray, 0, doubleArray.Count)
        dataListBox.Items.Add("Index" & vbTab & "Value")

        For i As Integer = 0 To 10 Step 1
            integerArray(i) = rand.Next(100, 1000)
            dataListBox.Items.Add(i & vbTab & integerArray(i))
        Next

    End Sub

    Private Sub createDoublesButton_Click(sender As Object, e As EventArgs) Handles createDoublesButton.Click
        dataListBox.Items.Clear()
        resultLabel.Text = ""
        Array.Clear(integerArray, 0, integerArray.Count)
        dataListBox.Items.Add("Index" & vbTab & "Value")

        For i As Integer = 0 To 10 Step 1
            doubleArray(i) = Math.Round(rand.NextDouble() * 100, 2, MidpointRounding.AwayFromZero)
            dataListBox.Items.Add(i & vbTab & doubleArray(i))
        Next

    End Sub

    Private Sub searchButton_Click(sender As Object, e As EventArgs) Handles searchButton.Click

        Dim resultValue As Integer

        If (searchKeyTextbox.Text.Contains(".")) Then
            resultValue = searchClass.Search(searchKeyTextbox.Text, doubleArray)
        Else
            resultValue = searchClass.Search(searchKeyTextbox.Text, integerArray)
        End If

        If resultValue >= 0 Then
            resultLabel.Text = "Found value in index " & resultValue
        Else
            resultLabel.Text = "Value not found"
        End If
    End Sub
End Class