Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
• Exercises 7.1 Review the arguments for and against goto statements and criticize their validity. Exercises 99 4. the difficulty of finding out where you came from in a program constructed using gotos 5. the need to read backwards in a program with gotos in it. 6. the difficulty of proving the correctness of programs that use gotos. The arguments for gotos are: 1. to ban them is to deskill programming 2. they have a use in exceptional circumstances 3. gotos are sometimes necessary in order to make programs perform well 4. it is sometimes “natural” to use gotos. Much of the debate about structured programming has been conducted in this way. Throughout the arguments, the need for clarity in programs is the dominant idea. The avoidance of gotos does not necessarily lead to a structured program. The central idea of structured programming is abstraction. Indeed, we can define structured programming as: “the systematic use of (control) abstraction in programming” (Abstraction can be applied not just to control, but to data as well. This is the subject of other chapters.) Abstraction requires not only the use of suitable control structures but also a systematic design method. 7.2 State in your own words what structured programming is. Imagine that you meet a friend who is used to programming using gotos. Explain in simple clear language what structured programming is. 7.3 A while statement can be used instead of an if statement, because the statement: if bool then s endif can be rewritten as: while bool do s bool = false endwhile Show how an if-then-else statement can similarly be rewritten using while statements. If the if statement is superfluous, why have it?
100 Chapter 7 ■ Structured programming > > 7.4 Convert the following into a structured program. i = start loop: if x = a[i] then goto found endif if i = end then goto notFound endif i = i + 1 goto loop notFound: display 'not found' action1 goto end found: display 'found' action2 end: Compare and contrast the two programs. 7.5 Recursion can be used to accomplish repetition, as in the following example of a method to skip spaces in a stream of input information: method skipSpaces read(char) if char = space then skipSpaces endif endmethod Convert this example to a method that uses a while statement for repetition. Compare and contrast the two solutions. What is the role of recursion in the programmer’s toolkit? 7.6 Argue for and against providing the following constructs in a programming language: ■ case ■ repeat-until ■ until-do Answer to self-test question 7.1 count = 0 while count < 10 do count = count + 1 endwhile > >
- Page 72: Further reading 49 Further reading
- Page 76 and 77: CHAPTER 5 This chapter explains: 5.
- Page 78 and 79: 5.3 Styles of human-computer interf
- Page 80 and 81: 5.5 Design principles and guideline
- Page 82 and 83: 5.5 Design principles and guideline
- Page 84 and 85: SELF-TEST QUESTION 5.2 What problem
- Page 86 and 87: 5.8 Help systems 63 Our plan is to
- Page 88 and 89: Further reading 65 5.5 Design a use
- Page 90 and 91: CHAPTER 6 Modularity This chapter e
- Page 92 and 93: 6.2 Why modularity? 69 observed fau
- Page 94 and 95: Figure 6.1 Two alternative software
- Page 96 and 97: ■ a simple program is more likely
- Page 98 and 99: 6.6 Information hiding 75 The class
- Page 100 and 101: 6.8 ● Coupling 6.8 Coupling 77 We
- Page 102 and 103: 6. Method calls with parameters tha
- Page 104 and 105: 3. Temporal cohesion 6.9 Cohesion 8
- Page 106 and 107: > } public void setY(int newY) { y
- Page 108 and 109: • Exercises 6.1 What is modularit
- Page 110 and 111: CHAPTER 7 Structured programming Th
- Page 112 and 113: 7.2 Arguments against goto 89 If we
- Page 114 and 115: ■ if-then-else ■ while-do or re
- Page 116 and 117: 7.3 Arguments in favor of goto 93 l
- Page 118 and 119: 7.4 Selecting control structures 95
- Page 120 and 121: while do if endif then else endWhil
- Page 124 and 125: count = 0 loop: count = count + 1 i
- Page 126 and 127: > 8.2 Case study 103 A statement th
- Page 128 and 129: start button event create defender
- Page 130 and 131: 8.3 ● Discussion Abstraction One
- Page 132 and 133: Exercises 109 skill. On the other h
- Page 134 and 135: CHAPTER 9 This chapter explains: 9.
- Page 136 and 137: 9.2 Identifying data flows 113 Noti
- Page 138 and 139: 9.3 Creation of a structure chart 1
- Page 140 and 141: SELF-TEST QUESTION 9.4 Discussion 1
- Page 142 and 143: Exercises 119 During the second sta
- Page 144 and 145: CHAPTER 10 This chapter explains:
- Page 146 and 147: In English, this reads: 10.2 A simp
- Page 148 and 149: 10.2 A simple example 125 Now comes
- Page 150 and 151: 10.4 Multiple input and output stre
- Page 152 and 153: Process header Process issue 10.4 M
- Page 154 and 155: 10.5 Structure clashes 131 As seen
- Page 156 and 157: 10.5 Structure clashes 133 Let us r
- Page 158 and 159: 10.6 Discussion 135 ■ teachable -
- Page 160 and 161: Exercises 137 2. a control block, s
- Page 162 and 163: CHAPTER 11 Object-oriented design T
- Page 164 and 165: Figure 11.1 The cyberspace invaders
- Page 166 and 167: SELF-TEST QUESTION 11.1 Derive info
- Page 168 and 169: 11.5 Class-responsibility-collabora
- Page 170 and 171: 11.7 ● Discussion Summary 147 OOD
100 Chapter 7 ■ Structured programming<br />
><br />
><br />
7.4 Convert the following into a structured program.<br />
i = start<br />
loop:<br />
if x = a[i] then goto found endif<br />
if i = end then goto notFound endif<br />
i = i + 1<br />
goto loop<br />
notFound:<br />
display 'not found'<br />
action1<br />
goto end<br />
found:<br />
display 'found'<br />
action2<br />
end:<br />
Compare and contrast the two programs.<br />
7.5 Recursion can be used to accomplish repetition, as in the following example of a<br />
method to skip spaces in a stream of input in<strong>for</strong>mation:<br />
method skipSpaces<br />
read(char)<br />
if char = space then skipSpaces endif<br />
endmethod<br />
Convert this example to a method that uses a while statement <strong>for</strong> repetition. Compare<br />
and contrast the two solutions. What is the role of recursion in the programmer’s toolkit?<br />
7.6 Argue <strong>for</strong> and against providing the following constructs in a programming language:<br />
■ case<br />
■ repeat-until<br />
■ until-do<br />
Answer to self-test question<br />
7.1 count = 0<br />
while count < 10 do<br />
count = count + 1<br />
endwhile<br />
><br />
>