Programming and Problem Solving with Java

(やまだぃちぅ) #1
2.1 The Elements of Java Programs | 67

prints exactly the same thing on the screen as the statement


System.out.print(“Susy Sunshine”);


There is a difference, however. If the latter statement (which uses the printmethod) is fol-
lowed by another message to System.out, the next string would begin on the same line. If the
former code (which uses the printlnmethod) is followed by another message to System.out,
the next string would begin on the next line.


Input


An application needs data on which to operate. So far, we have written all of the data values
into the code itself, in literal and named constants. If this technique was the only way we
could enter data, we would have to rewrite our code each time we wanted to apply it to a dif-
ferent set of values. In this section we look at ways of entering data into an application while
it is running.
One of the biggest advantages associated with computers is that an application can be
used with many different sets of data. To do so, the data must be kept separate from the
code until the code executes. Then method calls in the code can copy values from the data
set into variables in the application. After storing these values in the variables, the code can
perform calculations with
them (see Figure 2.3).
The process of placing
values from an outside data
set into variables in an appli-
cation is called input. The data
for the application can come
from an input device or from a
file on an auxiliary storage de-
vice. We will look at file input
in detail in Chapter 5; here we
consider only the standard in-
put window, represented by
the object System.in.
Unfortunately, Java doesn’t
make it quite as simple to in-
put data from System.inas it
does to output data to
System.out.System.inis a very
primitive object that is de-
signed to serve as the basis for
building more sophisticated
objects. With System.in, we can


Data as
constants

Figure 2.3 Separating the Data from Code
Free download pdf