vb3 oop prac
TRANSCRIPT
-
7/25/2019 Vb3 Oop Prac
1/7
3/6/20
Object Oriented ProgrammingUsing Visual Basic
Lab Guide
The Practicals
1-1: Creating a Student Class
1-2: Adding a parameterised constructor
1-3: Enumerated Types
1-4: BankTeller Application
1-5: Manually testing integer input
OOP Lab
2
1-1: Creating a Student Class
1-2: Adding a parameterised constructor
Lab3
OOP Lab
Lab 1-1: Creating a Student class
You will create a two-tier application that usesa form to passinputs by the user to the Student class.
The presentationtier:
the formsclass
The middle tier:
theStudentclass
The formwill have controlsallowing the user to input a StudentID, last name, and test average.
When the user clicks a button, your code will assign the inputvaluesto Student classproperties.
The formwill also have a label to redisplay the Student object.
OOP Lab
4
-
7/25/2019 Vb3 Oop Prac
2/7
3/6/20
Lab 1-1
The formafter the user clicks theSave button.
The same form af ter the user clicks
the View button.
OOP Lab
5
Lab 1-1 Steps
Step1:
Create a new Windows application named Student ClassExample.
Step2:
Next, add a classnamed Studentto the project.
Right-clickon theProjectname,selectAdd, and selectClass.
In the Add New Itemdialog window, select Code, selectClass, and enter the classname asStudent.vb.
Step3:
Open the Student.vb file and replace its contents with the
classdefinition in the next slide: OOP Lab
6
Lab 1-1 Steps
Public Class Student
Public Property IdNumber As String auto-implemented property
Public Property LastName As String auto-implemented property
Private mTestAverage As Double
The TestAverage property requires range checking,
so it is implemented with explicitGet and Set sections.
Public Property TestAverage As Double
Get
Return mTestAverage
End Get
Set(ByVal value As Double)
If value >= 0.0 And value
-
7/25/2019 Vb3 Oop Prac
3/7
3/6/20
Lab 1-1 Steps
Next, you will write code in the startup form that copies theusers inputs to Student properties.
Step5:
Declare a Student variable at the classlevel (inForm1):
Private objStudent As New Student
Step6:
Create theClickhandleronthenext slide for the Savebutton.
Youcanomit the parametersfrom the btnSave_Click procedure becausethey are optional
NB: Relaxed delegates is a VB feature that allows you to omit
parameters in event handlers if the parameters are not being used
insidethe body of thehandler.
OOP Lab
9
Lab 1-1 Steps
Public Class Form1
Private objStudent As New Student
Private Sub btnSave_Click() Handles btnSave.Click
objStudent.IdNumber = txtIdNumber.Text
objStudent.LastName = txtLastName.Text
objStudent. TestAverage= CDbl(txtTes tAverage.Text)
lblStudent.Text = "(student information saved)"
End Sub
End Class
Copies values from the TextBox controls into the propert ies of the objStudentobject.
lblStudentis addedto providea hint to the user.
OOP Lab
10
Lab 1-1 Steps
Step7:
Create a Clickhandler for the View button that usesthe Student.ToStringmethod to display the Studentobject:
Private Sub btnView_Click() Handles btnView.Click
lblStudent.Text = objStudent.ToString()
End Sub
OOP Lab
11
Lab 1-1 Test
OOP Lab
12
Step 8:Save the project, and run the application with thefollowing test:
Input Expectedoutput
Enter an ID number such as001234, a students last name,and a 0 for average and click theSavebutton.
Thenclick theViewbutton.
You should see the same IDnumber and name that youentered.The test average will display as
value 0.
-
7/25/2019 Vb3 Oop Prac
4/7
3/6/20
Lab 1-2
Adding a parameterised constructor to the Studentclass
Threeparameters:
1. ID number,
2. last name,
3. test average
The application will ask the user to input values,whichare thenpassed to theStudent constructor.
Then the application will display the values storedinside theStudent object.
OOP Lab
13
Lab 1-2: Adding a ParameterisedConstructor to the Student Class
Step1:
In Windows Explorer, make a copy of thefolder containing the Student Class Exampleproject you wrote for Lab1-1.
Openthe new project.
Step2:
Change the caption in the forms title bar toStudent Classwith Constructors.
OOP Lab
14
Lab 1-2: Adding a ParameterisedConstructor to the Student Class
Step3:
Add thefollowing constructor to theStudent class:
Public S ub Ne w(By Val pIdN umber As Str ing,
Optional ByVal pLastName As String = "", Optional
ByVal pTestAverage As Double = 0.0)
'the secon d a nd t hird pa ram ete rs are
optional
IdNumber = pIdNumber
LastName = pLastName
TestAverage = pTestAverageEnd Sub
OOP Lab
15
Lab 1-2: Adding a ParameterisedConstructor to the Student Class
Step4:
Edit the forms source code. First, change thedeclarationof objStudent to the following:
Private objStudent As Student
Thisstatement declares a Student variable, but it doesnot create a studentObject.
OOP Lab
16
-
7/25/2019 Vb3 Oop Prac
5/7
3/6/20
Lab 1-2: Adding a ParameterisedConstructor to the Student Class
Step5:
Modify the btnSave_Click event handler so it contains thefollowing code:
Private Sub btnSave_Click() Handles btnSave.Click
Dim testAverage As Double
If Double.TryParse(txtTestAverage.Text, testAverage) Then
objStudent = New Student(txtIdNumber.Text,
txtLastName.Text, testAverage)
calls the Student constructor, assigning values to the three class
variables.
lblStudent.Text = "(student information saved)"
Else
lblStudent.Text = "Test average is not a valid number"
End If
End Sub OOP Lab
17
Lab 1-2: Test the app
OOP Lab
18
Input Expectedoutput
Enter 200032, Odhis, 92.3 in the3 textboxes and click the Savebutton.Thenclick theView button.
You should see the same IDnumber, name and average thatyouentered.
Enter 100011, Ali, XX in the 3textboxes and click the Savebutton.
You should see the same IDnumber and name you entered,and an error message in thelabel.
1-3: Enumerated Types
1-4: Bank Teller Application
Lab19
OOP Lab
Lab 1-3
Enumerated Account type
When the user selects an account type f rom a list box,the selected index is converted into an AccountTypeobject.
OOP Lab
20
-
7/25/2019 Vb3 Oop Prac
6/7
3/6/20
1.4Bank Teller Application
Two-tier application that simulates an electronicbankteller
user looks up account, deposits & withdraws funds,viewsthebalance
OOP Lab
21
Requirements
1. User looks up existing account information byentering an Account number (ID).
2. If the account exists, the account name and balanceare displayed.
3. User can enter an amount to deposit into theaccount.Thisdeposit is added to the balance.
4. User can enter an amount to withdraw from theaccount. If the amount is
-
7/25/2019 Vb3 Oop Prac
7/7
3/6/20
1-5: Manually testing integer input
Lab25
OOP Lab
Requirements Specification
The application prompts the user with a range of acceptableinteger values.
The user inputs an integer N.
If N is a noninteger value, the application displays an errormessage.
If N is outside the range of acceptable values, the applicationdisplaysan error message.
If N is within the range of acceptable values, the applicationdisplaysthe name of a color that matchesN fromthe followinglist: 0 = white, 1 = yellow, 2 = green, 3 = red, 4 = blue, 5 =orange.
OOP Lab
26
Lab 1-5
Manually testing integer input
Displays a string by using the input value as asubscript intoan array of strings
OOP Lab
27
Testing Plan
OOP Lab
28