Applied Programming/Variables/Java

From Wikiversity
Jump to navigation Jump to search

variables.java[edit]

/** 
 * This program converts a Fahrenheit temperature to Celsius.
 * 
 * Input:
 *     Fahrenheit temperature
 * 
 * Output:
 *     Fahrenheit temperature
 *     Celsius temperature
 * 
 * Example:
 *     Enter Fahrenheit temperature:
 *      100
 *     100.0° Fahrenheit is 37.8° Celsius
 * 
 * TODO:
 *     * Functions will be added in a future release.
 * 
 * References:
 *     * http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
 *     * https://www.w3resource.com/slides/java-coding-style-guide.php
 *     * http://www.mathsisfun.com/temperature-conversion.html
 */

import java.util.Scanner;

class Main {
    public static final int TEMPERATURE_DIFFERENCE = 32;
    public static final double TEMPERATURE_RATIO = 5.0 / 9;

    public static void main(String[] args) {
        System.out.println("Enter Fahrenheit temperature:");
        Scanner scanner = new Scanner(System.in);
        double fahrenheit;
        fahrenheit = scanner.nextDouble();

        double celsius;
        celsius = (fahrenheit - TEMPERATURE_DIFFERENCE) * TEMPERATURE_RATIO;

        System.out.println(String.format(
            "%1$.1f° Fahrenheit is %2$.1f° Celsius",
            fahrenheit,
            celsius));
    }
}

Try It[edit]

Copy and paste the code above into one of the following free online development environments or use your own Java compiler / interpreter / IDE.

See Also[edit]