Applied Programming/Conditions/Java
conditions.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
*
* 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) {
try {
double fahrenheit = getFahrenheit();
double celsius = fahrenheitToCelsius(fahrenheit);
displayResults(fahrenheit, celsius);
}
catch (Exception exception) {
System.out.println("Unexpected error.");
exception.printStackTrace();
}
}
/**
* Gets Fahrenheit temperature.
*
* @return Fahrenheit temperature
*
* Exits:
* If Fahrenheit temperature is not a valid float.
* If Fahrenheit temperature is below absolute zero.
*/
private static double getFahrenheit() {
try {
System.out.println("Enter Fahrenheit temperature:");
Scanner scanner = new Scanner(System.in);
double fahrenheit;
fahrenheit = scanner.nextDouble();
if (fahrenheit < ABSOLUTE_ZERO) {
System.out.println("Fahrenheit temperature cannot be below absolute zero.");
System.out.println("Temperature is invalid.");
System.exit(0);
}
return fahrenheit;
}
catch (java.util.InputMismatchException exception) {
System.out.println("Fahrenheit temperature must be a floating point value.");
System.out.println("InputMismatchException: Temperature is invalid.");
System.exit(0);
return 0;
}
}
/**
* 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 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",
fahrenheit,
celsius));
}
}
Try It
editCopy and paste the code above into one of the following free online development environments or use your own Java compiler / interpreter / IDE.