Recipe 5.20 Program: TempConverter


The program shown in Example 5-6 prints a table of Fahrenheit temperatures (still used in daily life weather reporting in the United States) and the corresponding Celsius temperatures (used in science everywhere, and in daily life in most of the world).

Example 5-6. TempConverter.java
import java.text.*; /* Print a table of Fahrenheit and Celsius temperatures   */ public class TempConverter {     public static void main(String[] args) {         TempConverter t = new TempConverter( );         t.start( );         t.data( );         t.end( );     }     protected void start( ) {     }     protected void data( ) {         for (int i=-40; i<=120; i+=10) {             float c = (i-32)*(5f/9);             print(i, c);         }     }     protected void print(float f, float c) {         System.out.println(f + " " + c);     }     protected void end( ) {     } }

This works, but these numbers print with about 15 digits of (useless) decimal fractions! The second version of this program subclasses the first and uses a DecimalFormat to control the formatting of the converted temperatures (Example 5-7).

Example 5-7. TempConverter2.java
import java.text.*; /* Print a table of fahrenheit and celsius temperatures, a bit more neatly.  */ public class TempConverter2 extends TempConverter {     protected DecimalFormat df;     public static void main(String[] args) {         TempConverter t = new TempConverter2( );         t.start( );         t.data( );         t.end( );     }     // Constructor     public TempConverter2( ) {         df = new DecimalFormat("#0.00");     }     protected void print(float f, float c) {         System.out.println(f + " " + df.format(c));     }     protected void start( ) {         System.out.println("Fahr    Centigrade.");     }     protected void end( ) {         System.out.println("-------------------");     } }

This works, and the results are better than the first version's, but still not right:

C:\javasrc\numbers>java  TempConverter2 Fahr    Centigrade. -40.00 -40.00 -30.00 -34.44 -20.00 -28.89 -10.00 -23.33 0.00 -17.78 10.00 -12.22 20.00 -6.67 30.00 -1.11 40.00 4.44 50.00 10.00 60.00 15.56 70.00 21.11 80.00 26.67 90.00 32.22 100.00 37.78 110.00 43.33 120.00 48.89

It would look neater if we lined up the decimal points, but Java had nothing in its standard API for doing this. This is deliberate! They wanted to utterly break the ties with the ancient IBM 1403 line printers and similar monospaced devices such as typewriters, "dumb" terminals,[4] and DOS terminal windows. However, with a bit of simple arithmetic, the FieldPosition from Recipe 5.11 can be used to figure out how many spaces need to be prepended to line up the columns; the arithmetic is done in print( ), and the spaces are put on in prependSpaces( ). The result is much prettier:

[4] My children are quick to remind me that "dumb" means "incapable of speech." Nobody who has used, say, a TTY33 or a DecWriter 100 dumb terminal will claim that they are incapable of speech. Intelligible speech yes, but they certainly did talk at you while they were printing . . . .

C:\javasrc\numbers>java  TempConverter3 Fahr    Centigrade.  -40   -40  -30   -34.444  -20   -28.889  -10   -23.333    0   -17.778   10   -12.222   20    -6.667   30    -1.111   40     4.444   50    10   60    15.556   70    21.111   80    26.667   90    32.222  100    37.778  110    43.333  120    48.889 -------------------

And the code (Example 5-8) is only ten lines longer!

Example 5-8. TempConverter3.java
import java.text.*; /* Print a table of Fahrenheit and Celsius temperatures, with decimal  * points lined up.  */ public class TempConverter3 extends TempConverter2 {     protected FieldPosition fp;     protected DecimalFormat dff;     public static void main(String[] args) {         TempConverter t = new TempConverter3( );         t.start( );         t.data( );         t.end( );     }     // Constructor     public TempConverter3( ) {         super( );         dff = new DecimalFormat("##.#");         fp = new FieldPosition(NumberFormat.INTEGER_FIELD);     }     protected void print(float f, float c) {         String fs = dff.format(f, new StringBuffer( ), fp).toString( );         fs = prependSpaces(4 - fp.getEndIndex( ), fs);         String cs = df.format(c, new StringBuffer( ), fp).toString( );         cs = prependSpaces(4 - fp.getEndIndex( ), cs);         System.out.println(fs + "  " + cs);     }     protected String prependSpaces(int n, String s) {         String[] res = {             "", " ", "  ", "   ", "    ", "     "         };         if (n<res.length)             return res[n] + s;         throw new IllegalStateException("Rebuild with bigger \"res\" array.");     } }

Remember, though, that the fields line up only if you use a fixed-width font, such as Courier or LucidaSansTypewriter. If you want to line it up in a graphical display, you'll need to use Java's font capability (see Chapter 13) or a JTable (see the Javadoc for javax.swing.JTable or the O'Reilly book Java Swing).



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net