15 Structured Programming Mark Dixon 1 Admin Test

  • Slides: 19
Download presentation
15 – Structured Programming Mark Dixon 1

15 – Structured Programming Mark Dixon 1

Admin: Test 2 • In class test – 11 Feb 2014 – 4 Feb

Admin: Test 2 • In class test – 11 Feb 2014 – 4 Feb 2014: revision (technique) session • 50 mins • short answer (1 - 15 lines) • 10% of module mark Mark Dixon 2

Questions: Functions • Consider the following code: Function Smallest(num 1, num 2) If num

Questions: Functions • Consider the following code: Function Smallest(num 1, num 2) If num 1 < num 2 Then Smallest = num 1 Else Smallest = num 2 End If End Function • name a function. Smallest • what is left in small after the following is executed? Dim small = Smallest(23, 15) Mark Dixon 15 3

Session Aims & Objectives • Aims – To highlight the fundamental ideas of the

Session Aims & Objectives • Aims – To highlight the fundamental ideas of the structured programming paradigm • Objectives, by end of this week’s sessions, you should be able to: – create an abstract data type, which includes • data, and • routines – use this to reduce code length Mark Dixon 4

Example: Ball Bounce v 1 <html> <head><title>Ball Bounce</title></head> <body style="margin: 0; "> <img id="img.

Example: Ball Bounce v 1 <html> <head><title>Ball Bounce</title></head> <body style="margin: 0; "> <img id="img. Ball" src="Ball. gif" style="position: absolute; " /> </body> </html> <script language="vbscript"> Option Explicit Dim x Dim y Dim x. Inc Dim y. Inc Sub window_on. Load() window. setinterval "Main()", 20 x. Inc = 5 y. Inc = 3 End Sub Mark Dixon Sub Main() x = img. Ball. style. pos. Left + x. Inc If x <= 0 Or x >= document. body. client. Width - img. Ball. width x. Inc = -x. Inc Else img. Ball. style. pos. Left = x End If y = img. Ball. style. pos. Top + y. Inc If y <= 0 Or y >= document. body. client. Height - img. Ball. heigh y. Inc = -y. Inc Else img. Ball. style. pos. Top = y End If End Sub </script> 5

Structured Paradigm • Program made up of – data structures (variables & arrays), and

Structured Paradigm • Program made up of – data structures (variables & arrays), and – routines (procedures & functions) that process the data within those structures. • Each routine should perform a single, clearly identifiable operation. • Each routine should be self-contained • Abstract data type = structure + procedures Mark Dixon 6

Self-Contained Routines • Self-contained: – no references to external items (objects & variables) Function

Self-Contained Routines • Self-contained: – no references to external items (objects & variables) Function a 1(h, f) yes a 1 = (h + f) * f End Function Sub a 2() img. Ball. Style. pos. Left = 0 End Sub Mark Dixon no 7

Question: Self-contained • Which of the following routines are self-contained? Dim s Mark Dixon

Question: Self-contained • Which of the following routines are self-contained? Dim s Mark Dixon Function goo(h, f) goo = (h + f) * s End Function no Function poo(j, k, vi) If j > 45 Then poo = k + vi Else poo = k - vi End If End Function yes 8

Routines: Self-Contained • Good design principle: – routines (functions and procedures) should be self-contained

Routines: Self-Contained • Good design principle: – routines (functions and procedures) should be self-contained (easier to re-use in other programs) Dim u As Long Dim a As Long a=4 u = Twice() Function Twice() Return a * 2 End Function Mark Dixon Dim u As Long u = Twice(4) Function Twice(a) Return a * 2 End Function 9

Question: Self-Contained Routines • Are the following routines self contained? Dim num 1 Dim

Question: Self-Contained Routines • Are the following routines self contained? Dim num 1 Dim num 2 Dim diff Sub Compare() diff = num 1 - num 2 End Sub Function Half(num) Half = num / 2 End Function Mark Dixon 10

Example: Ball Bounce v 1 <html> <head><title>Ball Bounce</title></head> <body style="margin: 0; "> <img id="img.

Example: Ball Bounce v 1 <html> <head><title>Ball Bounce</title></head> <body style="margin: 0; "> <img id="img. Ball" src="Ball. gif" style="position: absolute; " /> </body> </html> <script language="vbscript"> Option Explicit Dim x Dim y Dim x. Inc Dim y. Inc Sub window_on. Load() window. setinterval "Main()", 20 x. Inc = 5 y. Inc = 3 End Sub Mark Dixon Sub Main() x = img. Ball. style. pos. Left + x. Inc If x <= 0 Or x >= document. body. client. Width - img. Ball. width x. Inc = -x. Inc Else img. Ball. style. pos. Left = x End If y = img. Ball. style. pos. Top + y. Inc If y <= 0 Or y >= document. body. client. Height - img. Ball. heigh y. Inc = -y. Inc Else img. Ball. style. pos. Top = y End If End Sub </script> 11

Example: Ball Bounce v 2 Sprite. vbs Ball. Bounce. htm Option Explicit Dim x

Example: Ball Bounce v 2 Sprite. vbs Ball. Bounce. htm Option Explicit Dim x Dim y Dim x. Inc Dim y. Inc Sub Init(tmp. XInc, tmp. YInc) <html> x. Inc = tmp. XInc <head><title>Ball Bounce</title></head> y. Inc = tmp. YInc <body style="margin: 0; "> End /> Sub <img id="img. Ball" src="Ball. gif" style="position: absolute; " </body> Sub Move(img) </html> x = img. style. pos. Left + x. Inc <script language="vbscript" src="Sprite. vbs"></script> If x <= 0 Or x >= document. body. client. Width - img. width x. Inc = -x. Inc <script language="vbscript"> Else Option Explicit img. style. pos. Left = x End If Sub window_on. Load() y = img. style. pos. Top + y. Inc window. setinterval "Main()", 20 If y <= 0 Or y >= document. body. client. Height - img. heig Init 5, 3 y. Inc = -y. Inc End Sub Else img. style. pos. Top = y Sub Main() End If Move img. Ball End Sub </script> Mark Dixon 12

Making a routine self-contained • identify why it isn't self-contained: Dim m Sub Move.

Making a routine self-contained • identify why it isn't self-contained: Dim m Sub Move. Middle() m = document. body. client. Width / 2 pic. Dog. style. pos. Left = m End Sub • the call: Move. Middle() Mark Dixon 13

Making a routine self-contained • if variables are only used by this routine, then

Making a routine self-contained • if variables are only used by this routine, then make them local: Sub Move. Middle() Dim m m = document. body. client. Width / 2 pic. Dog. style. pos. Left = m End Sub • the call: Move. Middle() Mark Dixon 14

Making a routine self-contained • if variables / identifiers are used elsewhere, then use

Making a routine self-contained • if variables / identifiers are used elsewhere, then use a parameter: Sub Move. Middle(pic. Any) Dim m m = document. body. client. Width / 2 pic. Any. style. pos. Left = m End Sub • the call now needs an actual parameter: Move. Middle(pic. Dog) Mark Dixon 15

Making a routine self-contained • some items are difficult to fix (e. g. document

Making a routine self-contained • some items are difficult to fix (e. g. document – built in object): Sub Move. Middle(pic. Any) Dim m m = document. body. client. Width / 2 pic. Any. style. pos. Left = m End Sub • the call: Either: - use parameter - leave it Move. Middle(pic. Dog) Mark Dixon 16

Example: Balloon Shoot • Question: – what objects? – what variables? – what procedures

Example: Balloon Shoot • Question: – what objects? – what variables? – what procedures / functions? Mark Dixon 17

Tutorial Exercise: Ball Bounce • Learning Objective: To create and use your own class.

Tutorial Exercise: Ball Bounce • Learning Objective: To create and use your own class. • Task 1: Get the Ball Bounce examples (1, 2, and 5) from the lecture working. • Task 2: Add another sprite. Hint: Use Arrays. • Task 3: Add another 5 sprites. • Task 4: Add a hit method to the sprite class, which detects the collision with another sprite. • Task 5: Modify your page to count the number of hits between the two sprites. • Task 6: Modify your page to make sprites bounce off each other. Mark Dixon 18

Tutorial Exercise: Balloon Shoot • Learning Objective: To create and use your own classes.

Tutorial Exercise: Balloon Shoot • Learning Objective: To create and use your own classes. • Task 1: Create the Balloon Shoot example (from the lecture) using object oriented concepts (classes, properties, methods, and instances) hint: use some of the code from your Interceptor example (from last week) Mark Dixon 19