Vous êtes sur la page 1sur 7

3/6/2016

The Practicals
2

1-1: Creating a Student Class


1-2: Adding a parameterised constructor
1-3: Enumerated Types
1-4: Bank Teller Application
1-5: Manually testing integer input

Object Oriented Programming


Using Visual Basic
Lab Guide

OOP Lab

Lab 1-1: Creating a Student class


4

You will create a two-tier application that uses a form to pass


inputs by the user to the Student class.
The presentation tier:

The middle tier:

The form will have controls allowing the user to input a Student
ID, last name, and test average.
When the user clicks a button, your code will assign the input
values to Student class properties.
The form will also have a label to redisplay the Student object.

Lab
1-1: Creating a Student Class
1-2: Adding a parameterised constructor

OOP Lab

the forms class


the Student class

OOP Lab

3/6/2016

Lab 1-1

Lab 1-1 Steps

The form after the user clicks the


Save button.

The same form after the user clicks


the View button.

Step 1:

Create a new Windows application named Student Class


Example.

Step 2:
Next, add a class named Student to the project.
Right-click on the Project name, select Add, and select Class.
In the Add New Item dialog window, select Code, select
Class, and enter the class name as Student.vb.

Step 3:

Open the Student.vb file and replace its contents with the
class definition in the next slide:

OOP Lab

OOP Lab

Lab 1-1 Steps

Lab 1-1 Steps

Public Class Student


Public Property IdNumber As String auto-implemented property
Public Property LastName As String auto-implemented property

Step 4:

Private mTestAverage As Double


The TestAverage property requires range checking,

Open the startup form in design mode and add the labels and the
named controls as shown below

so it is implemented with explicit Get and Set sections.


Public Property TestAverage As Double
Get
Return mTestAverage
End Get
Set(ByVal value As Double)
If value >= 0.0 And value <= 100.0 Then
mTestAverage = value
Else
MessageBox.Show("Invalid test average.", "Error")
End If
End Set
End Property
Public Overrides Function ToString() As String
Return IdNumber & ", " & LastName _
& ", Test average = " & TestAverage
End Function
End Class

OOP Lab

OOP Lab

3/6/2016

Lab 1-1 Steps

Lab 1-1 Steps

10

Next, you will write code in the startup form that copies the
users inputs to Student properties.
Step 5:

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(txtTestAverage.Text)
lblStudent.Text = "(student information saved)"
End Sub

Declare a Student variable at the class level (in Form1):

Private objStudent As New Student

Step 6:

Create the Click handler on the next slide for the Save button.

You can omit the parameters from the btnSave_Click procedure because
they 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
inside the body of the handler.

End Class

Copies values from the TextBox controls into the properties of the objStudent
object.

lblStudent is added to provide a hint to the user.

OOP Lab
OOP Lab

Lab 1-1 Steps

Lab 1-1 Test

11

12

Step 7:

Create a Click handler for the View button that uses


the Student.ToString method to display the Student
object:

Private Sub btnView_Click() Handles btnView.Click


lblStudent.Text = objStudent.ToString()
End Sub

OOP Lab

Step 8:
Save the project, and run the application with the
following test:
Input

Expected output

Enter an ID number such as


001234, a students last name,
and a 0 for average and click the
Save button.
Then click the View button.

You should see the same ID


number and name that you
entered.
The test average will display as
value 0.

OOP Lab

3/6/2016

Lab 1-2
13

14

Adding a parameterised constructor to the Student


class

2.
3.

Step 1:
In

Windows Explorer, make a copy of the


folder containing the Student Class Example
project you wrote for Lab1-1.
Open the new project.

Three parameters:
1.

Lab 1-2: Adding a Parameterised


Constructor to the Student Class

ID number,
last name,
test average

The application will ask the user to input values,


which are then passed to the Student constructor.
Then the application will display the values stored
inside the Student object.

Step 2:
Change

the caption in the forms title bar to


Student Class with Constructors.

OOP Lab

15

Lab 1-2: Adding a Parameterised


Constructor to the Student Class

Step 3:

OOP Lab

16

Lab 1-2: Adding a Parameterised


Constructor to the Student Class

Add the following constructor to the Student class:

Public
Sub
New(ByVal
pIdNumber
As
String,
Optional ByVal pLastName As String = "", Optional
ByVal pTestAverage As Double = 0.0)
'the
second
and
third
parameters
are
optional
IdNumber = pIdNumber
LastName = pLastName
TestAverage = pTestAverage
End Sub

OOP Lab

Step 4:

Edit the forms source code. First, change the


declaration of objStudent to the following:

Private objStudent As Student

This statement declares a Student variable, but it does


not create a student Object.

OOP Lab

3/6/2016

Lab 1-2: Adding a Parameterised


Constructor to the Student Class

17

Lab 1-2: Test the app


18

Step 5:

Modify the btnSave_Click event handler so it contains the


following code:

Private Sub btnSave_Click() Handles btnSave.Click


Dim testAverage As Double
If Double.TryParse(txtTestAverage.Text, testAverage) Then
objStudent = New Student(txtIdNumber.Text,

Input

Expected output

Enter 200032, Odhis, 92.3 in the You should see the same ID
3 textboxes and click the Save number, name and average that
button.
you entered.
Then click the View button.

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"

Enter 100011, Ali, XX in the 3 You should see the same ID


textboxes and click the Save number and name you entered,
button.
and an error message in the
label.

End If
OOP Lab

End Sub

OOP Lab

Lab 1-3
20

19

Lab

Enumerated Account type

1-3: Enumerated Types


1-4: Bank Teller Application

OOP Lab

When the user selects an account type from a list box,


the selected index is converted into an AccountType
object.

OOP Lab

3/6/2016

1.4 Bank Teller Application


21

Requirements
22

Two-tier application that simulates an electronic


bank teller

1.

user looks up account, deposits & withdraws funds,


views the balance

2.

3.

4.

5.
OOP Lab

User looks up existing account information by


entering an Account number (ID).
If the account exists, the account name and balance
are displayed.
User can enter an amount to deposit into the
account. This deposit is added to the balance.
User can enter an amount to withdraw from the
account. If the amount is <= balance, it is subtracted
from the balance
The updated balance is displayed after (3) and (4).
OOP Lab

Depositing Funds

Withdrawing Funds

23

24

Adds Amount, displays the updated Balance

OOP Lab

Verifies the amount of available funds, subtracts the


amount from the balance, redisplays the balance

OOP Lab

3/6/2016

Requirements Specification
26

25

Lab

1-5: Manually testing integer input

The application prompts the user with a range of acceptable


integer values.
The user inputs an integer N.
If N is a noninteger value, the application displays an error
message.
If N is outside the range of acceptable values, the application
displays an error message.
If N is within the range of acceptable values, the application
displays the name of a color that matches N from the following
list: 0 = white, 1 = yellow, 2 = green, 3 = red, 4 = blue, 5 =
orange.

OOP Lab

OOP Lab

Lab 1-5

Testing Plan

27

28

Manually testing integer input


Displays a string by using the input value as a
subscript into an array of strings

OOP Lab

OOP Lab

Vous aimerez peut-être aussi