Skip to main content
Engineering LibreTexts

5.13: Java Examples

  • Page ID
    10666
  • Temperature

    // This program displays a temperature conversion table showing Fahrenheit
    // temperatures from 0 to 100, in increments of 10, and the corresponding 
    // Celsius temperatures using While, Do While, and For loops.
    //
    // References:
    //     https://www.mathsisfun.com/temperature-conversion.html
    //     https://en.wikibooks.org/wiki/Java_Programming
    
    import java.util.*;
    
    class Main
    {
        public static void main(String[] args) {
            whileLoop();
            doLoop();
            forLoop();
        }
    
        private static void whileLoop() {
            double fahrenheit;
            double celsius;
            
            displayHeading();
            fahrenheit = 0;
            while (fahrenheit <= 100) {
                celsius = calculateCelsius(fahrenheit);
                displayResult(fahrenheit, celsius);
                fahrenheit += 10;
            }
            System.out.println();
        }
    
        private static void doLoop() {
            double fahrenheit;
            double celsius;
           
            displayHeading();
            fahrenheit = 0;
            do {
                celsius = calculateCelsius(fahrenheit);
                displayResult(fahrenheit, celsius);
                fahrenheit += 10;
            } while (fahrenheit <= 100);
            System.out.println();
        }
    
        private static void forLoop() {
            double fahrenheit;
            double celsius;
            
            displayHeading();
            for (fahrenheit = 0 ; fahrenheit <= 100 ; fahrenheit += 10) {
                celsius = calculateCelsius(fahrenheit);
                displayResult(fahrenheit, celsius);
            }
            System.out.println();
        }
    
        private static void displayHeading() {
            System.out.println("F°\tC°");
        }
    
        private static double calculateCelsius(double fahrenheit) {
            double celsius;
    
            celsius = (fahrenheit - 32) * 5 / 9;
    
            return celsius;
        }
    
        private static void displayResult(double fahrenheit, double celsius) {
                System.out.println(String.valueOf(fahrenheit) + "\t" + String.valueOf(celsius));
        }
    }
    

    Output

    F°	C°
    0.0	-17.77777777777778
    10.0	-12.222222222222221
    20.0	-6.666666666666667
    30.0	-1.1111111111111112
    40.0	4.444444444444445
    50.0	10.0
    60.0	15.555555555555555
    70.0	21.11111111111111
    80.0	26.666666666666668
    90.0	32.22222222222222
    100.0	37.77777777777778
    
    F°	C°
    0.0	-17.77777777777778
    10.0	-12.222222222222221
    20.0	-6.666666666666667
    30.0	-1.1111111111111112
    40.0	4.444444444444445
    50.0	10.0
    60.0	15.555555555555555
    70.0	21.11111111111111
    80.0	26.666666666666668
    90.0	32.22222222222222
    100.0	37.77777777777778
    
    F°	C°
    0.0	-17.77777777777778
    10.0	-12.222222222222221
    20.0	-6.666666666666667
    30.0	-1.1111111111111112
    40.0	4.444444444444445
    50.0	10.0
    60.0	15.555555555555555
    70.0	21.11111111111111
    80.0	26.666666666666668
    90.0	32.22222222222222
    100.0	37.77777777777778