Program: TempConverter

The program shown in Example 5-5 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-5. 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-6).

Example 5-6. 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("##.###");
    }

    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
umbers>java  TempConverter2
Fahr    Centigrade.
-40.0 -40
-30.0 -34.444
-20.0 -28.889
-10.0 -23.333
0.0 -17.778
10.0 -12.222
20.0 -6.667
30.0 -1.111
40.0 4.444
50.0 10

It would look neater if we lined up the decimal points, but Java has 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,[20] and DOS terminal windows. However, with a bit of simple arithmetic, the FieldPosition from Section 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:

C:javasrc
umbers>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-7) is only ten lines longer!

Example 5-7. 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 will 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 Section 12.6) or use a JTable (see the Javadoc for javax.swing.JTable or the O’Reilly book Java Swing).



[20] 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 . . .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.188.244