Java Reference
In-Depth Information
The decision and merge symbols can be distinguished by the number of “incoming”
and “outgoing” transition arrows. A decision symbol has one transition arrow pointing to
the diamond and two or more pointing out from it to indicate possible transitions from
that point. In addition, each transition arrow pointing out of a decision symbol has a guard
condition next to it. A merge symbol has two or more transition arrows pointing to the
diamond and only one pointing from the diamond, to indicate multiple activity flows
merging to continue the activity. None of the transition arrows associated with a merge
symbol has a guard condition.
Figure 4.6 clearly shows the repetition of the while statement discussed earlier in this
section. The transition arrow emerging from the action state points back to the merge,
from which program flow transitions back to the decision that's tested at the beginning of
each iteration of the loop. The loop continues to execute until the guard condition
product > 100 becomes true. Then the while statement exits (reaches its final state), and
control passes to the next statement in sequence in the program.
4.9 Formulating Algorithms: Counter-Controlled
Repetition
To illustrate how algorithms are developed, we solve two variations of a problem that av-
erages student grades. Consider the following problem statement:
A class of ten students took a quiz. The grades (integers in the range 0-100) for this
quiz are available to you. Determine the class average on the quiz.
The class average is equal to the sum of the grades divided by the number of students. The
algorithm for solving this problem on a computer must input each grade, keep track of the
total of all grades input, perform the averaging calculation and print the result.
Pseudocode Algorithm with Counter-Controlled Repetition
Let's use pseudocode to list the actions to execute and specify the order in which they
should execute. We use counter-controlled repetition to input the grades one at a time.
This technique uses a variable called a counter (or control variable ) to control the number
of times a set of statements will execute. Counter-controlled repetition is often called defi-
nite repetition , because the number of repetitions is known before the loop begins execut-
ing. In this example, repetition terminates when the counter exceeds 10. This section
presents a fully developed pseudocode algorithm (Fig. 4.7) and a corresponding Java pro-
gram (Fig. 4.8) that implements the algorithm. In Section 4.10, we demonstrate how to
use pseudocode to develop such an algorithm from scratch.
Note the references in the algorithm of Fig. 4.7 to a total and a counter. A total is a
variable used to accumulate the sum of several values. A counter is a variable used to
count—in this case, the grade counter indicates which of the 10 grades is about to be
entered by the user. Variables used to store totals are normally initialized to zero before
being used in a program.
Software Engineering Observation 4.2
Experience has shown that the most difficult part of solving a problem on a computer is
developing the algorithm for the solution. Once a correct algorithm has been specified,
producing a working Java program from it is usually straightforward.
 
 
Search WWH ::




Custom Search