LAB 3 – CREATING AND USING VARIABLE OBJECTIVE: The main objective is to introduce the students with variables. Students will be able to know how to create and use the variables defined by them in the program. QUESTIONS: 1. Using the class Account as attached in the appendix, write a program that simulates a user bank account. A text box allows user to enter the amount of money to be deposited into or withdrawn from the account when the Deposit or Withdraw button is pressed. The current balance of the account is continuously displayed on the form. An example is shown on the left.
Create a new VB project named myBankAccount Add the Account class to the project: i. Select Project -> Add Class to open ‘Add New Item’ window. ii. Set the name to Account.vb and click Add button. iii. In the Account class code window, add the definition of the Account class as given in Lecture 4 between the lines ‘Public Class Account’ and ‘End Class’. Study the Account class and identify the public methods it provides. Design the form: i. Add appropriate VB controls on the Form for user input. ii. Add three buttons: Deposit, Withdraw and Exit, each with an appropriate name and style. Study the example program code given in Lecture 4 for bank account transaction and make sure you understand how it works and why. Implement the program: i. Declare an object of Account class named myAccount (before any methods). ii. On loading the form, create and initialize the object myAccount with a given amount of money and display the current balance on the form. iii. When the Deposit or Withdraw button is clicked, the balance of myAccount is updated accordingly and displayed on the form. iv. Clicking on the Exit button closes the program. Select File > Save All to save the project in your folder. Run the program and check if it works as expected. If not, find out why and fix it.
2. Modify the project ballProject that you have built (Ex.2B) such that users are able to
move the ball to left, right, or a given position. (See the example given in appendix below); change the colour of the ball; inflate or deflate the ball, i.e. make it bigger or smaller.
Session: Jul - Dec 2008 Subject Code: FSB23103
Page: 1 of 5
LAB 3 – CREATING AND USING VARIABLE Appendix Public Class Account Private balance As Double Public Sub New(ByVal iniAmount As Double) balance = iniAmount End Sub Public Sub Deposit(ByVal amount As Double) balance += amount End Sub Public Sub Withdraw(ByVal amount As Double) balance -= amount End Sub Public ReadOnly Property currentBalance() As Double Get Return balance End Get End Property End Class
Command button cmdUp, cmdDown, cmdLeft, cmdRight Textbox txbX, txbY
PictureBox picBox
Command button cmdGo Command button cmdDraw, cmdClear, cmdClose
Radio button rbnRed, rbnYellow, rbnBlue
Session: Jul - Dec 2008 Subject Code: FSB23103
Page: 2 of 5
LAB 3 – CREATING AND USING VARIABLE
Event handling methods Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' create and initialize the paper object paper = picBox1.CreateGraphics paper.Clear(paperColour)
Form load
Instantiate the paper object
' create and initialise the ball object ball = New Circle(150, 105, 25, Color.Blue) End Sub
Instantiate the ball object
Private Sub cmdDraw_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdDraw.Click ' display the ball ball.Show(paper) End Sub Private Sub cmdClear_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClear.Click ' paint the ball the same colour as papaColour ball.Clear(paper, paperColour) End Sub Private Sub cmdClose_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClose.Click Close() End Sub
Session: Jul - Dec 2008 Subject Code: FSB23103
Page: 3 of 5
LAB 3 – CREATING AND USING VARIABLE
Private Sub rbnRed_CheckedChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles rbnRed.CheckedChanged ' Wipe the ball ball.Clear(paper, paperColour) ' Change the colour of the ball ball.Colour = Color.Red ' Display the ball ball.Show(paper) End Sub
Private Sub cmdMoveTo_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdMoveTo.Click cmdMoveTo ' get user input for x and y button Dim x As Integer = CInt(txbX.Text) Three steps Dim y As Integer = CInt(txbY.Text) 1 2 3
' move the ball to a new position (x, y) ball.Clear(paper, paperColour) ' wipe the ball ball.ChangeCentreTo(x, y) ' change the ball position ball.Show(paper) ' show the ball at the new position ' clear input boxes txbX.Text = "" txbY.Text = "" End Sub
Session: Jul - Dec 2008 Subject Code: FSB23103
Page: 4 of 5
LAB 3 – CREATING AND USING VARIABLE
•
e.g. to move the ball to a new position (30,40) involved three steps:
Step 1: Clear the ball on paper ball.Clear(paper, paperColour)
Memory address
xx x
Step 2: Change the position of the ball to (30,40) ball.ChangeCentreTo(30, 40)
ball
Circle object xpos
30
ypos
40
radius
25
tint
xxx
xxx
Step 3: Show the ball on paper ball.Show(paper)
Color object xx x
Session: Jul - Dec 2008 Subject Code: FSB23103
Page: 5 of 5
Blue