Print a table of Fahrenheit and Celsius temperatures 3 : Algorithms « Collections Data Structure « Java






Print a table of Fahrenheit and Celsius temperatures 3

Print a table of Fahrenheit and Celsius temperatures 3
 

import java.text.*;

/* Print a table of Fahrenheit and Celsius temperatures, with decimal
 * points lined up.
 * @author Ian F. Darwin, http://www.darwinsys.com/
 * @version $Id: TempConverter3.java,v 1.4 2004/03/07 02:50:49 ian Exp $
 */
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("#0.0");
    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.");
  }
}



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(df.format(f) + " " + df.format(c));
  }

  protected void start() {
    System.out.println("Fahr    Centigrade.");
  }

  protected void end() {
    System.out.println("-------------------");
  }
}
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() {
  }
}

           
         
  








Related examples in the same category

1.AnagramsAnagrams
2.Hanoi puzzleHanoi puzzle
3.FibonacciFibonacci
4.Sieve Sieve
5.Find connections using a depth-first searchFind connections using a depth-first search
6.Find connections using hill climbing.
7.Find optimal solution using least-cost
8.Find the lost keysFind the lost keys
9.Compute the area of a triangle using Heron's FormulaCompute the area of a triangle using Heron's Formula
10.Compute prime numbers
11.Print a table of fahrenheit and celsius temperatures 1
12.Print a table of fahrenheit and celsius temperatures 2
13.Soundex - the Soundex Algorithm, as described by KnuthSoundex - the Soundex Algorithm, as described by Knuth
14.A programmable Finite State Machine implementation.
15.An extendable Graph datastructure.
16.Utilities for flop (floating-point operation) counting.
17.LU Decomposition
18.Reverse Polish Notation
19.Permutator test
20.implements the LZF lossless data compression algorithm
21.Linear Interpolation
22.Utility class for generating the k-subsets of the numbers 0 to n
23.VersionVersion