Programming and Problem Solving with Java

(やまだぃちぅ) #1
3.9 Applications with Multiple Class Files | 131

(^2) There are ways of indicating to the compiler that it should search other directories, but such a strat-
egy is mainly useful for much larger programming projects than we use in this book.


Noninteractive Input/Output


Although we tend to use examples of interactive I/O in this book, many applications work
with noninteractive I/O. A common example of noninteractive I/O on large computer systems
is batch processing. In batch processing (introduced in Chapter 1), the user and the computer
do not interact while the application is running. This method is most effective when an ap-
plication will input or output large amounts of data. An example of batch processing is an
application that takes as input a file containing semester grades for thousands of students
and prints grade reports to be mailed out.
When an application must read in many data values, the usual practice is to prepare them
ahead of time, storing them into a disk file. The user can then make changes or corrections
to the data as necessary before running the application. When an application is designed to
print lots of data, the output can be sent directly to a high-speed printer or another disk file.
After the application has been run, the user can examine the data at leisure.
Most Java applications are written for interactive use, but the flexibility of the language
allows you to write noninteractive applications as well. The biggest difference relates to the
input/output requirements. Noninteractive applications are generally more rigid about the
organization and format of the input and output data.
Applications can also combine interactive input and output with file input and output.
In Chapter 5, we will discuss input and output with disk files.


3.9 Applications with Multiple Class Files


An application with multiple classes can be implemented with those classes stored in separate
files. With each Java class stored in its own file, the code is divided into smaller chunks that are
easier to work with. Keeping classes in separate files also makes it easier to import them into
other applications. In addition, most Java development environments keep track of which files
have been changed, and do not recompile unchanged files.Thus, when you’re debugging just one
of the classes, you don’t have to wait for compilation of the other ones. Using multiple files has
a further advantage in that it provides us with more flexibility in developing the classes of an ap-
plication. Team programming projects, in which multiple programmers work together to solve
a problem, would be very cumbersome if all of the programmers had to share a single file.
Java systems require that we name each file using the name of the class it contains.
This approach allows the Java compiler to use file names to locate the classes. For example,
a class called Namewould be stored in a file called Name.java. Other classes that wish to use
this class would include the following statement:


importName;


The class files should all reside in the same directory on the disk. The Java compiler
automatically searches this directory for related files.^2 For example, we might have the

Free download pdf