Java Reference
In-Depth Information
requires a loop that successively inputs the result of each exam. We know in advance that
there are precisely 10 exam results, so counter-controlled looping is appropriate. Inside the
loop (i.e., nested within the loop), a double-selection structure will determine whether each
exam result is a pass or a failure and will increment the appropriate counter. The refine-
ment of the preceding pseudocode statement is then
While student counter is less than or equal to 10
Prompt the user to enter the next exam result
Input the next exam result
If the student passed
Add one to passes
Else
Add one to failures
Add one to student counter
We use blank lines to isolate the If Else control structure, which improves readability.
The pseudocode statement
Print a summary of the exam results and decide whether a bonus should be paid
can be refined as follows:
Print the number of passes
Print the number of failures
If more than eight students passed
Print “Bonus to instructor!”
Complete Second Refinement of Pseudocode and Conversion to Class Analysis
The complete second refinement appears in Fig. 4.11. Notice that blank lines are also used
to set off the While structure for program readability. This pseudocode is now sufficiently
refined for conversion to Java.
1
Initialize passes to zero
2
Initialize failures to zero
3
Initialize student counter to one
4
5
While student counter is less than or equal to 10
6
Prompt the user to enter the next exam result
7
Input the next exam result
8
9
If the student passed
10
Add one to passes
11
Else
12
Add one to failures
13
14
Add one to student counter
Fig. 4.11 | Pseudocode for examination-results problem. (Part 1 of 2.)
 
Search WWH ::




Custom Search