Another Example of Fahrenheit and Celsius Temperatures Converter

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Another Example of Fahrenheit and Celsius Temperatures Converter

This is a example of Java program.

[edit] Java Source Code

  • Package: com.darwinsys
  • File: TempConverter3.java
package com.darwinsys;
 
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.");
    }
}

Package:com.darwinsys

File:TempConverter2.java

package com.darwinsys;
 
import java.text.DecimalFormat;
 
/* Print a table of fahrenheit and celsius temperatures, a bit more neatly.
 * @author Ian F. Darwin, http://www.darwinsys.com/
 * @version $Id: TempConverter2.java,v 1.1 2008/08/07 12:42:30 cat Exp $
 */
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(df.format(f) + " " + df.format(c));
    }
 
    protected void start() {
	System.out.println("Fahr    Centigrade.");
    }
 
    protected void end() {
	System.out.println("-------------------");
    }
}

Package:com.darwinsys

File:TempConverter.java

package com.darwinsys;
 
/* Print a table of Fahrenheit and Celsius temperatures 
 * @author Ian F. Darwin, http://www.darwinsys.com/
 * @version $Id: TempConverter.java,v 1.1 2008/08/07 12:42:32 cat Exp $
 */
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() {
    }
}

[edit] What Result You Can Get

Run the program, you will get:

Fahr    Centigrade.
 -40.0   -40.00
 -30.0   -34.44
 -20.0   -28.89
 -10.0   -23.33
   0.0   -17.78
  10.0   -12.22
  20.0    -6.67
  30.0    -1.11
  40.0     4.44
  50.0    10.00
  60.0    15.56
  70.0    21.11
  80.0    26.67
  90.0    32.22
 100.0    37.78
 110.0    43.33
 120.0    48.89
-------------------

[edit] Required External Library for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools