Java Reference
In-Depth Information
name . The values of id and name are inserted into the string, producing a result
such as:
ID: 24036 Name: Larry Flagelhopper
The printf method was added to Java to mirror a similar function used in
programs written in the C programming language. It makes it easier for a pro-
grammer to translate (or migrate ) an existing C program into Java.
Older software that still has value is called a
legacy system . Main-
taining a legacy system is often a costly effort because, among other
things, it is based on older technologies. But in many cases, main-
taining a legacy system is still more cost effective than migrating it
to new technology, such as writing it in a newer language. Adding
the printf method is an attempt to make such migrations easier, and therefore
less costly, by providing the same kind of output statement that C programmers
have come to rely on.
However, using the printf method is not a particularly clean object-oriented
solution to the problem of formatting output, so we avoid its use in this topic.
KEY CONCEPT
The printf method was added to
Java to support the migration of
legacy systems.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 3.25 Describe how you request a NumberFormat object for use within a pro-
gram.
SR 3.26 Suppose that in your program you have a double variable named
cost . You want to output the value stored in cost formatted as the
currency of the current locale.
a. Write a code statement that declares and requests a NumberFormat
object named moneyFormat that can be used to represent currency
in the format of the current locale.
b. Write a code statement that uses the moneyFormat object and prints
the value of cost , formatted as the currency of the current locale.
c. What would be the output from the statement you wrote in part
(b) if the value in cost is 54.89 and your computer's locale is set
to the United States? What if your computer's locale is set to the
United Kingdom?
SR 3.27 What are the steps to output a floating point value as a percentage
using Java's formatting classes?
SR 3.28 Write code statements that prompt for and read in a double value from
the user, and then print the result of taking the square root of the abso-
lute value of the input value. Output the result to two decimal places.
 
Search WWH ::




Custom Search