Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

start button event create defender create alien start timer We have hidden the detail in such statements as: move all the objects 8.2 Case study 105 and we can study and check the high-level design without worrying about detail. When we are satisfied with the high-level design, we choose one of the statements and write down what it does in more detail. An obvious first candidate is: move all the objects and then: move alien move bomb move laser check boundaries display all the objects display background display defender display alien display laser display bomb At this stage we have expressed the design in terms of a number of methods. The most complex of these are check hits and check boundaries. So we now design their detail: check hits if collides(laser, alien) then endOfGame(userWins) else if collides(bomb, defender) then endOfGame(alienWins) endif endif >

106 Chapter 8 ■ Functional decomposition > > check boundaries if bomb is outside window then destroy bomb endif if laser is outside window then destroy laser endif This completes some of the detail. The next item that has some complexity is the method collides. It has two parameters, the objects to be tested for impact. It returns either true or false. It uses the x and y coordinates of the objects, together with their heights and widths. We use the notation a.x to mean the x coordinate of object a. (The joy of pseudo-code is that you can use any notation that is convenient.) collides(a, b) if a.x > b.x and a.y < b.y + b.height and a.x + a.width < b.x + b.width and a.y + a.width > b.y then return true else return false endif This illustrates how we have used the tool of abstraction to hide this complexity within a method. There is still some way to go with this particular design, but this much gives the flavor of the design and of the method. Notice how the design is refined again and again. Notice how, throughout, we can check the design without having to take account of a lot of unnecessary detail. Notice the informality of the statements used; we can simply make up new actions as necessary, but still within the discipline of the control structures – sequence, if and while, supplemented with methods. SELF-TEST QUESTION 8.1 Write method move alien assuming it is at window coordinates x and y. >

106 Chapter 8 ■ Functional decomposition<br />

><br />

><br />

check boundaries<br />

if bomb is outside window<br />

then<br />

destroy bomb<br />

endif<br />

if laser is outside window<br />

then<br />

destroy laser<br />

endif<br />

This completes some of the detail. The next item that has some complexity is the<br />

method collides. It has two parameters, the objects to be tested <strong>for</strong> impact. It<br />

returns either true or false. It uses the x and y coordinates of the objects,<br />

together with their heights and widths. We use the notation a.x to mean the x<br />

coordinate of object a. (The joy of pseudo-code is that you can use any notation<br />

that is convenient.)<br />

collides(a, b)<br />

if a.x > b.x<br />

and a.y < b.y + b.height<br />

and a.x + a.width < b.x + b.width<br />

and a.y + a.width > b.y<br />

then<br />

return true<br />

else<br />

return false<br />

endif<br />

This illustrates how we have used the tool of abstraction to hide this complexity<br />

within a method.<br />

There is still some way to go with this particular design, but this much gives the flavor<br />

of the design and of the method. Notice how the design is refined again and again. Notice<br />

how, throughout, we can check the design without having to take account of a lot of<br />

unnecessary detail. Notice the in<strong>for</strong>mality of the statements used; we can simply make up<br />

new actions as necessary, but still within the discipline of the control structures – sequence,<br />

if and while, supplemented with methods.<br />

SELF-TEST QUESTION<br />

8.1 Write method move alien assuming it is at window coordinates x<br />

and y.<br />

>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!