Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM148X1 Interactive Programming Lecture 8. Topics Today Review.

Similar presentations


Presentation on theme: "COM148X1 Interactive Programming Lecture 8. Topics Today Review."— Presentation transcript:

1 COM148X1 Interactive Programming Lecture 8

2 Topics Today Review

3

4 Let’s Recall What Have Learnt Variable Declaration Basic Operators Branching and Looping Function and Procedure Flowchart Array and Collection Random Number Generation Timer PictureBox and ImageList Control Graphics and Animation HCI Event Handling

5 Variable Declaration Use keyword Dim in Visual Basic to declare variable Dim variable_name_list As data_type variable_name_list is the list of variable names separated by comma data_type is can be one of the data types mentioned in previous page or user defined data types Example Dim a As Integer Dim b, c As Double

6 Arithmetic Operators OperationVB OperatorVB Expression Addition+x + y Subtraction-x - y Multiplication*x * y Division/x / y Integer Division (quotient)\x \ y Exponentiation^x ^ y Modulus (remainder)Modx Mod y String Concatenation&x & y

7 Comparison Operators OperationVB OperatorVB Expression ==x = y  <>x <> y >>x > y <<x < y  >=x >= y  <=x <= y

8 And Operator (condition 1) And (condition 2) The expression is TRUE if and only if both condition 1 and condition 2 are TRUE Example (x > 10) And (x < 10000) ‘True if 10 < x < 10000

9 Or Operator (Condition 1) Or (Condition 2) The expression is FALSE if and only if both Condition 1 and Condition 2 are FALSE Example (y 10000) ‘True if y greater than 10 ‘or y smaller than 10000

10 Xor Operator (condition 1) Xor (condition 2) The expression is TRUE if and only if condition 1 and condition 2 give different results, that is condition 1 is TRUE while condition 2 is FALSE or vice versa Example (x > 1000) Xor (y > 1000) ‘True if x > 1000 but y ≤ 1000 ‘or x ≤ 1000 but y > 1000

11 Not Operator Not (condition) The expression is TRUE if and only if the given condition is FALSE Example Not (student > 100) ‘True if student smaller than 100

12 Branching Branching is a program control structure which allows computer executes different parts of program according to user inputs or computation results In Visual Basic, branching can be done by If Else statement or Select Case statement

13 Combination of Conditions Sometimes the condition is based on combination of many conditions, for example, checking the value of variable x within a range (a, b) Use operators And, Or, Xor, Not to combine different conditions

14 Looping Looping is a program control structure which allows computer executes part of program multiple times In Visual Basic, looping can be done by For Next loop or Do While loop

15 Procedure Procedure is a segment of code which responsible for a sub-task of the main task Advantages of using procedure Make the program easy to design and implement Make the code easy to reuse Make the code easy to read Make the code easy to modify

16 Define Procedure Sub procedure_name( ByVal pname1 As ptype1, … ) procedure End Sub Example Sub AddStock(ByVal stock_count As Integer) total_cost = total_cost + stock_count * 5 End Sub

17 Function Function is similar to procedure but it will return a value back to the caller

18 Define Function Function function_name( ByVal pname As ptype, … ) As output_type function calculation function_name = value End Function Example Function CheckCode(ByVal code As Integer) As Boolean If code 100 Then CheckCode = False Else CheckCode = True End If End Function

19 Pass By Value If parameters set as ByVal, then it is “Pass by Value”, all modifications to this parameter during the execution of function/ procedure will not retain (i.e. the value of the parameter will be restored back to the value before the function/ procedure execute)

20 Pass by Reference If parameters set as ByRef, then it is “Pass by Reference”, all modifications to this parameter during the execution of function/ procedure will retained

21 Terminal used to represent the start/ end of the program Input/ Output Process Decision Connector used to connect different parts of flowchart when the direct connection among these parts are messy Flowchart Symbols

22 What is Array Array is a collection of data of same type Size of array is fixed unless it is being asked to change Array is random access Index of array start from 0

23 UBound Function Obtain the largest index of an array Example For j = 0 To UBound(a) a(i) = 20 Next j 20 a 0 1 2 3 4 5

24 What is Collection Collection is a group of data which can be retrieved either by index or string Collection index started from 1 Dim collection_name As New Collection() Example Dim marks As New Collection()

25 Generate Random Number In VB, random numbers can be generated using Rnd() function Rnd() function returns random number in the range [0 … 1) (include 0 but exclude 1) Example x = Rnd() ‘0 ≤ x < 1 x = Rnd() * 5 ‘0 ≤ x < 5 x = Int(Rnd() * 5) + 1 ‘1 ≤ x ≤ 5, integer only

26 Randomize() Function The number sequence generated by Rnd() will be the same when the application start Randomize() function is used to shuffle the sequence again Call Randomize() once when the application start

27 Pseudo Random Number Sometimes, non-repeatable random number is required, for example, shuffle a deck of card (A, 2, 3, …, J, Q, K) Use the technique called pseudo random number to generate non-repeatable random number

28 Pseudo Random Number Algorithm Use array to store all number Use Rnd() function to pick two array position randomly and then SWAP the content Repeat the step above several times and use the array as random result 23456789101112131 21045678931112131 471 681225391011

29 Pseudo Random Number Example Dim cards(13) As Integer Dim j, k, card1, card2 As Integer For j = 1 To 13 cards(j) = j Next j PseudoRandom(cards)

30 Pseudo Random Number Example (cont’) ‘Perform pseudo random from 1 … UBound(a) Sub PseudoRandom(ByRef a() As Integer) For k = 1 to 10000 card1 = Int(Rnd() * UBound(a)) + 1 card2 = Int(Rnd() * UBound(a)) + 1 Swap(a(card1), a(card2)) Next k End Sub

31 Pseudo Random Number Example (cont’) Sub Swap(ByRef x As Integer, ByRef y As Integer) Dim temp As Integer = x x = y y = temp End Sub

32 Timer Timer is an invisible stopwatch to do count down from present time Once timer finished count down, corresponding code set by programmer will be executed

33 Timer Properties Enabled Count down will start if it is True Interval Number of milliseconds for count down, once finished count down, call-back function (written by programmer himself) will be executed

34 Graphics Example Dim g As Graphics g = Panel1.CreateGraphics() Dim brush1 As New SolidBrush(Color.Red) Dim pen as New Pen(brush1, 10) g.DrawLine(pen, p1, p2) Dim brush2 As New SolidBrush(Color.Green) g.FillRectangle(brush, p1.X(), p1.Y(), 100, 150)

35 ImageList Images is set of Images, index start from 0 ImageSize is the size of image, maximum size is 256x256 Number of images in the ImageList can be found in the Count properties of Images Example x = ImageList1.Images.Count

36 PictureBox Location represents the top-left corner of PictureBox Top represents the top most coordinate of PictureBox Left represents the left most coordinate of PictureBox Width represents the width of PictureBox Height represents the height of PictureBox Image represents the picture showing in the PictureBox, set Image to Nothing in order to make the PictureBox shows nothing Example PictureBox1.Image = Nothing

37 Animation Example ‘move image from left to right while playing ‘image sequence inside PictureBox Dim i As Integer = 0 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim x, y as Integer x = PictureBox1.Location.X + 1 y = PictureBox1.Location.Y PictureBox1.Location = new Point(x, y) PictureBox1.Image = ImageList1.Images(i) i = (i + 1) mod ImageList1.Images.Count End Sub

38 HCI HCI stands for Human Computer Interaction, it is a field of study of designing user-friendly computer interface Many guidelines available and Apple’s one mainly used for reference during lecture

39 Event An event is an action which programmer can handle in code An event can be generated by User (clicking a button) System (timer completed count down) Programmer (change the attribute of control) In VB, different control expose different set of event for programmer to handle

40 Event Handler Event handler is the program which handle specific event(s)


Download ppt "COM148X1 Interactive Programming Lecture 8. Topics Today Review."

Similar presentations


Ads by Google