Java Reference
In-Depth Information
• The scope of a local-variable declaration that appears in the initialization section of a for state-
ment's header is the body of the for statement and the other expressions in the header.
• The scope of a method or field of a class is the entire body of the class. This enables a class's meth-
ods to use simple names to call the class's other methods and to access the class's fields.
• Any block may contain variable declarations. If a local variable or parameter in a method has the
same name as a field, the field is shadowed (p. 223) until the block terminates execution.
Section 6.12 Method Overloading
• Java allows overloaded methods (p. 225) in a class, as long as the methods have different sets of
parameters (determined by the number, order and types of the parameters).
• Overloaded methods are distinguished by their signatures (p. 226)—combinations of the meth-
ods' names and the number, types and order of their parameters, but not their return types.
Self-Review Exercises
6.1
Fill in the blanks in each of the following statements:
a)
A method is invoked with a(n)
.
b)
A variable known only within the method in which it's declared is called a(n)
.
c)
The statement in a called method can be used to pass the value of an expres-
sion back to the calling method.
d)
The keyword
indicates that a method does not return a value.
e)
Data can be added or removed only from the
of a stack.
f)
Stacks are known as data structures; the last item pushed (inserted) onto the
stack is the first item popped (removed) from the stack.
g)
The three ways to return control from a called method to a caller are
,
and .
h) An object of class produces truly random numbers.
i) The method-call stack contains the memory for local variables on each invocation of a
method during a program's execution. This data, stored as a portion of the method-call
stack, is known as the or of the method call.
j) If there are more method calls than can be stored on the method-call stack, an error
known as a(n) occurs.
k) The of a declaration is the portion of a program that can refer to the entity in
the declaration by name.
l) It's possible to have several methods with the same name that each operate on different
types or numbers of arguments. This feature is called method .
6.2 For the class Craps in Fig. 6.8, state the scope of each of the following entities:
a) the variable randomNumbers .
b) the variable die1 .
c) the method rollDice .
d) the method main .
e) the variable sumOfDice .
6.3 Write an application that tests whether the examples of the Math class method calls shown
in Fig. 6.2 actually produce the indicated results.
6.4
Give the method header for each of the following methods:
a)
Method hypotenuse , which takes two double-precision, floating-point arguments
side1 and side2 and returns a double-precision, floating-point result.
b)
Method smallest , which takes three integers x , y and z and returns an integer.
 
Search WWH ::




Custom Search