Design a class to display values of type double that are not necessarily
for money amounts. Call the class OutputFormat. It should have
two static methods, write and writeln, each of which takes two
arguments. The first argument gives a double value to be written to the
screen. The second argument is an int value telling how many digits
to show after the decimal point. Have your methods round any extra
digits. These methods are similar to the methods write and writeln in
the class DollarFormat. Although you can use the class DollarFormat
as a model, the methods in these two classes have some differences.
As you would expect, any output occurring after write executes will
be on the same line, and output occurring after writeln executes will
be on the following line; otherwise, write and writeln do the same
thing.
For example, the statements
OutputFormat.writeln(9.1234667, 4);
OutputFormat.writeln(9.9999, 2);
OutputFormat.writeln(7.01234, 4);
should produce the following output:
9.1235
10.00
7.0123
Do not forget to test your methods with numbers that have zeros after
the decimal point, like 1.023 and 1.0023. [Hint: You may find the static
method Math.pow (Figure 6.3) helpful as part of an expression to move
a decimal point.] This is a fairly difficult exercise, so allow yourself some
time to complete it. If you do not succeed in writing this class, be sure that
you at least understand the answer given at the end of the chapter. This is
a very useful class.