Jump to content

Applied Programming/Loops/Java

From Wikiversity

loops.java

[edit | edit source]
/** 
 * This program converts a Fahrenheit temperature to Celsius.
 * 
 * Input:
 *     Fahrenheit temperature
 * 
 * Output:
 *     Fahrenheit temperature
 *     Celsius temperature
 * 
 * Example:
 *     Enter Fahrenheit temperature or press <Enter> to quit:
 *      100
 *     100.0° Fahrenheit is 37.8° Celsius
 * 
 *     Enter Fahrenheit temperature or press <Enter> to quit:
 *     ...
 * 
 * References:
 *     * http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
 *     * https://www.w3resource.com/slides/java-coding-style-guide.php
 *     * https://en.wikipedia.org/wiki/Javadoc
 *     * http://www.mathsisfun.com/temperature-conversion.html
 */

import java.util.Scanner;

/**
 * Runs main program logic.
 */
class Main {
    public static final double ABSOLUTE_ZERO = -459.67;
    public static final int TEMPERATURE_DIFFERENCE = 32;
    public static final double TEMPERATURE_RATIO = 5.0 / 9;

    public static void main(String[] args) {
        while (true) {
            try {
                double fahrenheit = getFahrenheit();
                double celsius = fahrenheitToCelsius(fahrenheit);
                displayResults(fahrenheit, celsius);
                displayTable(fahrenheit);
            }
            catch (Exception exception) {
                System.out.println("Unexpected error.");
                exception.printStackTrace();
                System.exit(1);
            }
        }
    }
    
    /**
     * Gets Fahrenheit temperature.
     * 
     * @return  Fahrenheit temperature
     * 
     * Exits:
     *      If no temperature is entered.
    */
    private static double getFahrenheit() {
        while (true) {
            try {
                System.out.println("Enter Fahrenheit temperature or press <Enter> to quit:");
                Scanner scanner = new Scanner(System.in);
                String text = scanner.nextLine();
                
                if (text.isEmpty()) {
                    System.exit(0);
                }

                double fahrenheit = Double.parseDouble(text);
                if (fahrenheit < ABSOLUTE_ZERO) {
                    System.out.println("Fahrenheit temperature cannot be below absolute zero.");
                    System.out.println("Temperature is invalid.\n");
                }
                return fahrenheit;
            }
            catch (NumberFormatException exception) {
                System.out.println("Fahrenheit temperature must be a floating point value.");
                System.out.println("NumberFormatException: Temperature is invalid.\n");
            }
        }
    }
    
    /**
     * Converts Fahrenheit temperature to Celsius.
     * 
     * @param   Fahrenheit temperature
     * @return  Celsius temperature
     * @throws  IllegalArgumentException if temperature is below absolute zero
    */
    private static double fahrenheitToCelsius(double fahrenheit) {
        if (fahrenheit < ABSOLUTE_ZERO) {
            throw new IllegalArgumentException(
                "fahrenheit must not be below absolute zero.");
        }
        double celsius;
        celsius = (fahrenheit - TEMPERATURE_DIFFERENCE) * TEMPERATURE_RATIO;
        return celsius;
    }
    
    /**
     * Displays Fahrenheit and Celsius temperatures.
     * 
     * @param   Fahrenheit temperature
     * @param   Celsius temperature
     * @throws  AssertionError if fahrenheit is less than absolute zero
     * @throws  AssertionError if celsius is less than absolute zero
    */
    private static void displayResults(double fahrenheit, double celsius) {
        assert fahrenheit >= ABSOLUTE_ZERO : "fahrenheit less than absolute zero";
        assert celsius >= fahrenheitToCelsius(ABSOLUTE_ZERO) : 
            "celsius less than absolute zero";
        System.out.println(String.format(
            "%1$.1f° Fahrenheit is %2$.1f° Celsius\n",
            fahrenheit,
            celsius));
    }
    
    /**
     * Displays nearest multiples of 10 Fahrenheit and Celsius temperatures.
     * 
     * @param   Fahrenheit temperature
     * @throws  AssertionError if fahrenheit is null
    */
    private static void displayTable(double fahrenheit) {
        assert fahrenheit >= ABSOLUTE_ZERO : "fahrenheit less than absolute zero";

        System.out.println("F\tC");
        double start = ((int)(fahrenheit / 10)) * 10; 
        for (fahrenheit = start; fahrenheit < start + 11; fahrenheit++) {
            System.out.print(String.format(
                "%1$.1f\t", 
                fahrenheit));
            System.out.println(String.format(
                "%1$.1f", 
                fahrenheitToCelsius(fahrenheit)));
        }
        System.out.println();
    }
}

Try It

[edit | edit source]

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 | edit source]