Java Reference
In-Depth Information
Multiple-File Programs
In the preceding section, you learned how to create a package . Creating a package to
group related class (es) is very useful if the classes are to be used again and again. On the
other hand, if a class is to be used in only one program, or if you have divided your
program so that it uses more than one class , rather than create a package , you can
directly add the file(s) containing the class (es) to the program.
The Java IDEs—J++ Builder and JGrasp—put the editor, compiler, and loader all into
one program. With one command, a program is compiled. These IDEs also manage
multiple-file programs in the form of a project. A project consists of several files, called
the project files. These IDEs include a command that allows you to add several files to a
project. Also, these IDEs usually have commands such as build, rebuild,ormake (check
your software's documentation) to automatically compile all the files required. When one
or more files in the project change, you can use these commands to recompile the files.
Formatting the Output of Decimal Numbers Using
the class DecimalFormat
Chapter 3 explained how to format the output of floating-point numbers, using the
method format of the class String , to a specific number of decimal places. Chapter 3
also noted that another way to format the output of floating-point numbers is to use the
class DecimalFormat .
Recall that the default output of decimal numbers of the type float is up to six decimal
places. Similarly, the default output of decimal numbers of the type double is up to 15
decimal places. For example, consider the statements in Table D-3; the output is shown
to the right.
TABLE D-3 Default Output of Floating-Point Numbers
Statement
Output
System.out.println(22.0 / 7.0);
3.142857142857143
System.out.println(75.0 / 7.0);
10.714285714285714
System.out.println(( float )(33.0 / 16.0));
2.0625
System.out.println(( float )(22.0 / 7.0));
3.142857
As discussed in Chapter 3, sometimes floating-point numbers must be output in a specific
way. For example, a paycheck must be printed to two decimal places, whereas the results
of a scientific experiment might require the output of floating-point numbers to six,
seven, or perhaps even 10 decimal places.
 
Search WWH ::




Custom Search