University Visual Basic Assignment: Code, Testing, and Analysis Report

Verified

Added on  2022/08/18

|11
|949
|20
Practical Assignment
AI Summary
This document presents solutions to a Visual Basic programming assignment, providing code for several exercises. Exercise 4 involves a department code display with input validation. Exercise 11 calculates the total amount due, incorporating quantity, price, discounts, and user input validation. Exercise 12 addresses commission calculations, highlighting the differences between integer and decimal data types and demonstrating the use of TryParse() for data conversion. The solution includes code snippets, testing data, and output screenshots for each exercise, demonstrating the functionality and correctness of the implemented code. The assignment covers fundamental programming concepts such as user input, conditional statements, data type handling, and output formatting within the Visual Basic environment.
Document Page
Running head: VISUAL BASIC ASSIGNMENT
VISUAL BASIC ASSIGNMENT
Name of the Student
Name of the University
Author Note
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
1VISUAL BASIC ASSIGNMENT
Exercise 4
Code Part:
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles
btnExit.Click
Me.Close()
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles
btnDisplay.Click
' displays the department name
Dim strCode As String
If txtCode.Text = "" Then
lblName.Text = "Not available"
MsgBox("Please enter a Code", MsgBoxStyle.Information, "Depaerment
Codes")
Else
' assign user input to a variable
strCode = txtCode.Text
' display department name
Select Case strCode
Case "A"
lblName.Text = "Accounting"
Case "B"
lblName.Text = "Inventory"
End Select
End If
End Sub
' limiting the inputs to A,a,B,b and Backspace only
' ascii values of A = 65, B = 66, a = 97, b = 98, backspace = 8
Private Sub txtCode_KeyPress(sender As Object, e As
System.Windows.Forms.KeyPressEventArgs) Handles txtCode.KeyPress
If (Asc(e.KeyChar) = 65 Or Asc(e.KeyChar) = 66 Or Asc(e.KeyChar) = 97
Or Asc(e.KeyChar) = 98 Or Asc(e.KeyChar) = 8) Then
Else
e.Handled = True
End If
End Sub
End Class
Document Page
2VISUAL BASIC ASSIGNMENT
Testing the data as per guidelines:
Fig: Testing the application without entering any data
Fig: Testing the application with data (Path B)
Document Page
3VISUAL BASIC ASSIGNMENT
Fig: Testing the application with data (Path A)
Exercise 11
Code Part:
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles
btnExit.Click
Me.Close()
End Sub
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles
btnCalc.Click
' calculates and displays the total amount due
If txtPrice.Text = "" Or txtQuantity.Text = "" Then
MsgBox("Please fill the blank text boxes",
MsgBoxStyle.Information, "Total Due Calculator")
Else
If intChecking(txtQuantity.Text) And intChecking(txtPrice.Text)
Then
If Convert.ToInt32(txtPrice.Text) > 0 Then
Const decDISC_RATE As Decimal = 0.1
Dim intQuantity As Integer
Dim decPrice As Decimal
Dim decSubtotal As Decimal
Dim decDiscount As Decimal
Dim decTotal As Decimal
' assign quantity and price to variables
Integer.TryParse(txtQuantity.Text, intQuantity)
Decimal.TryParse(txtPrice.Text, decPrice)
' calculate subtotal, discount, and total due
decSubtotal = intQuantity * decPrice
If chkEmployee.Checked = True Then
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
4VISUAL BASIC ASSIGNMENT
decDiscount = decSubtotal * decDISC_RATE
Else
decDiscount = 0
End If
decTotal = decSubtotal - decDiscount
' display total due
lblTotal.Text = decTotal.ToString("C2")
Else
MessageBox.Show("Price should be a positive number.")
End If
Else
MessageBox.Show("Enter only numbers in the text boxes.")
End If
End If
End Sub
Function intChecking(ByVal value As String) As Boolean
Try
Convert.ToInt32(value)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
Testing the data as per guidelines:
Fig: Testing the application without entering any data
Document Page
5VISUAL BASIC ASSIGNMENT
Fig: Testing the application with alphabetic characters
Fig: Testing the application with negative price
Document Page
6VISUAL BASIC ASSIGNMENT
Fig: Testing the application with price zero and if statement use checking
Fig: Testing the application with negative quantity
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7VISUAL BASIC ASSIGNMENT
Fig: Testing the application with positive quantity
Fig: Testing the application with positive quantity and check box
Document Page
8VISUAL BASIC ASSIGNMENT
Exercise 12
Changed and corrected Code Part:
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles
btnExit.Click
Me.Close()
End Sub
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles
btnCalc.Click
' displays a 10% commission
Const decRATE As Decimal = 0.1
Dim intSales1 As Integer
Dim decSales2 As Decimal
Dim decComm1 As Decimal
Dim decComm2 As Decimal
' assign sales amounts to variables
Decimal.TryParse(txtSales1.Text, intSales1)
Decimal.TryParse(txtSales2.Text, decSales2)
' calculate the commission
decComm1 = intSales1 * decRATE
decComm2 = decSales2 * decRATE
' display the commission
lblComm1.Text = decComm1.ToString("C2")
lblComm2.Text = decComm2.ToString("C2")
End Sub
End Class
Document Page
9VISUAL BASIC ASSIGNMENT
Testing the data as mentioned in the problem statement:
Fig: Output as given in the supplied application file Code
The answers are different in the two labels because for calculating the commission ‘Region 1
Sales’, integer data type have been used whereas for the case of ‘Region 2 Sales’, decimal
data type is used.
Integers only deals with whole numbers whereas Decimal can deal with whole numbers and
decimal numbers. It (Decimal number) also deals with monetary values which an integer does
not.
While using the ‘TryParse()’ method for assigning the sales amount to the variables, the
TryParse() method only lets the data to assign to the mentioned variables only. If the parsing
from String to the mentioned data type is successful or else (if any type of error is found
while cinversion) the assignment to the mentioned variable (in the TryParse() method) is not
done.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
10VISUAL BASIC ASSIGNMENT
The problem can be fixed by changing the conversion type of TryParse() method from
‘Integer’ to ‘Decimal’
Fig: Output after applying the modifications in the application file Code
chevron_up_icon
1 out of 11
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]