Java Reference
In-Depth Information
2. Count the number of test results of each type.
3. Display a summary of the test results, indicating the number of students who passed and
the number who failed.
4. If more than eight students passed the exam, print “Bonus to instructor!”
After reading the problem statement carefully, we make the following observations:
1. The program must process test results for 10 students. A counter-controlled loop
can be used, because the number of test results is known in advance.
2. Each test result has a numeric value—either a 1 or a 2. Each time it reads a test
result, the program must determine whether it's a 1 or a 2. We test for a 1 in our
algorithm. If the number is not a 1, we assume that it's a 2. (Exercise 4.24 con-
siders the consequences of this assumption.)
3. Two counters are used to keep track of the exam results—one to count the num-
ber of students who passed the exam and one to count the number who failed.
4. After the program has processed all the results, it must decide whether more than
eight students passed the exam.
Let's proceed with top-down, stepwise refinement. We begin with a pseudocode rep-
resentation of the top:
Analyze exam results and decide whether a bonus should be paid
Once again, the top is a complete representation of the program, but several refinements
are likely to be needed before the pseudocode can evolve naturally into a Java program.
Our first refinement is
Initialize variables
Input the 10 exam results, and count passes and failures
Print a summary of the exam results and decide whether a bonus should be paid
Here, too, even though we have a complete representation of the entire program, further
refinement is necessary. We now commit to specific variables. Counters are needed to re-
cord the passes and failures, a counter will be used to control the looping process and a
variable is needed to store the user input. The variable in which the user input will be
stored is not initialized at the start of the algorithm, because its value is read from the user
during each iteration of the loop.
The pseudocode statement
Initialize variables
can be refined as follows:
Initialize passes to zero
Initialize failures to zero
Initialize student counter to one
Notice that only the counters are initialized at the start of the algorithm.
The pseudocode statement
Input the 10 exam results, and count passes and failures
 
Search WWH ::




Custom Search