Java Reference
In-Depth Information
9
char
ch = RandomCharacter.getRandomLowerCaseLetter();
10
if
((i +
1
) % CHARS_PER_LINE ==
0
)
11 System.out.println(ch);
12
else
13 System.out.print(ch);
14 }
15 }
16 }
lower-case letter
gmjsohezfkgtazqgmswfclrao
pnrunulnwmaztlfjedmpchcif
lalqdgivxkxpbzulrmqmbhikr
lbnrjlsopfxahssqhwuuljvbe
xbhdotzhpehbqmuwsfktwsoli
cbuwkzgxpmtzihgatdslvbwbz
bfesoklwbhnooygiigzdxuqni
Line 9 invokes
getRandomLowerCaseLetter()
defined in the
RandomCharacter
class.
Note that
getRandomLowerCaseLetter()
does not have any parameters, but you still have
to use the parentheses when defining and invoking the method.
parentheses required
The key to developing software is to apply the concept of abstraction.
Key
Point
You will learn many levels of abstraction from this topic.
Method abstraction
is achieved by
separating the use of a method from its implementation. The client can use a method without
knowing how it is implemented. The details of the implementation are encapsulated in the
method and hidden from the client who invokes the method. This is also known as
information
hiding
or
encapsulation.
If you decide to change the implementation, the client program will
not be affected, provided that you do not change the method signature. The implementation of
the method is hidden from the client in a “black box,” as shown in Figure 6.7.
VideoNote
Stepwise refinement
method abstraction
information hiding
Optional arguments
for input
Optional return
value
Method Header
Black box
Method Body
F
IGURE
6.7
The method body can be thought of as a black box that contains the detailed
implementation for the method.
You have already used the
System.out.print
method to display a string and the
max
method
to find the maximum number. You know how to write the code to invoke these methods in your
program, but as a user of these methods, you are not required to know how they are implemented.
The concept of method abstraction can be applied to the process of developing programs.
When writing a large program, you can use the
divide-and-conquer
strategy, also known
as
stepwise refinement
, to decompose it into subproblems. The subproblems can be further
decomposed into smaller, more manageable problems.
Suppose you write a program that displays the calendar for a given month of the year. The
program prompts the user to enter the year and the month, then displays the entire calendar for
the month, as shown in the following sample run.
divide and conquer
stepwise refinement
Search WWH ::
Custom Search