Java Reference
In-Depth Information
Integer . There are similar constants for the other integral types and for floating-point types.
Each primitive type has a corresponding class type in package java.lang . You can see the
values of these constants in each class's online documentation. The online documentation
for class Integer is located at:
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
It's considered a good practice to ensure, before you perform arithmetic calculations
like those in line 21 of Fig. 4.8 and line 23 of Fig. 2.7, that they will not overflow. The
code for doing this is shown on the CERT website www.securecoding.cert.org —just
search for guideline “NUM00-J.” The code uses the && (logical AND) and || (logical OR)
operators, which are introduced in Chapter 5. In industrial-strength code, you should per-
form checks like these for all calculations.
A Deeper Look at Receiving User Input
Any time a program receives input from the user, various problems might occur. For ex-
ample, in line 20 of Fig. 4.8
int grade = input.nextInt(); // input next grade
we assume that the user will enter an integer grade in the range 0 to 100. However, the
person entering a grade could enter an integer less than 0, an integer greater than 100, an
integer outside the range of values that can be stored in an int variable, a number contain-
ing a decimal point or a value containing letters or special symbols that's not even an in-
teger.
To ensure that inputs are valid, industrial-strength programs must test for all possible
erroneous cases. A program that inputs grades should validate the grades by using range
checking to ensure that hey are values from 0 to 100. You can then ask the user to reenter
any value that's out of range. If a program requires inputs from a specific set of values (e.g.,
nonsequential product codes), you can ensure that each input matches a value in the set.
4.10 Formulating Algorithms: Sentinel-Controlled
Repetition
Let's generalize Section 4.9's class-average problem. Consider the following problem:
Develop a class-averaging program that processes grades for an arbitrary number of
students each time it's run.
In the previous class-average example, the problem statement specified the number of stu-
dents, so the number of grades (10) was known in advance. In this example, no indication
is given of how many grades the user will enter during the program's execution. The pro-
gram must process an arbitrary number of grades. How can it determine when to stop the
input of grades? How will it know when to calculate and print the class average?
One way to solve this problem is to use a special value called a sentinel value (also
called a signal value , a dummy value or a flag value ) to indicate “end of data entry.” The
user enters grades until all legitimate grades have been entered. The user then types the
sentinel value to indicate that no more grades will be entered. Sentinel-controlled repeti-
tion is often called indefinite repetition because the number of repetitions is not known
before the loop begins executing.
 
 
Search WWH ::




Custom Search