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 */ importjava.util.Scanner; /** * Runs main program logic. */ class Main{ publicstaticfinaldoubleABSOLUTE_ZERO=-459.67; publicstaticfinalintTEMPERATURE_DIFFERENCE=32; publicstaticfinaldoubleTEMPERATURE_RATIO=5.0/9; publicstaticvoidmain(String[]args){ while(true){ try{ doublefahrenheit=getFahrenheit(); doublecelsius=fahrenheitToCelsius(fahrenheit); displayResults(fahrenheit,celsius); displayTable(fahrenheit); } catch(Exceptionexception){ System.out.println("Unexpected error."); exception.printStackTrace(); System.exit(1); } } } /** * Gets Fahrenheit temperature. * * @return Fahrenheit temperature * * Exits: * If no temperature is entered. */ privatestaticdoublegetFahrenheit(){ while(true){ try{ System.out.println("Enter Fahrenheit temperature or press <Enter> to quit:"); Scannerscanner=newScanner(System.in); Stringtext=scanner.nextLine(); if(text.isEmpty()){ System.exit(0); } doublefahrenheit=Double.parseDouble(text); if(fahrenheit<ABSOLUTE_ZERO){ System.out.println("Fahrenheit temperature cannot be below absolute zero."); System.out.println("Temperature is invalid.\n"); } returnfahrenheit; } catch(NumberFormatExceptionexception){ 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 */ privatestaticdoublefahrenheitToCelsius(doublefahrenheit){ if(fahrenheit<ABSOLUTE_ZERO){ thrownewIllegalArgumentException( "fahrenheit must not be below absolute zero."); } doublecelsius; celsius=(fahrenheit-TEMPERATURE_DIFFERENCE)*TEMPERATURE_RATIO; returncelsius; } /** * 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 */ privatestaticvoiddisplayResults(doublefahrenheit,doublecelsius){ assertfahrenheit>=ABSOLUTE_ZERO:"fahrenheit less than absolute zero"; assertcelsius>=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 */ privatestaticvoiddisplayTable(doublefahrenheit){ assertfahrenheit>=ABSOLUTE_ZERO:"fahrenheit less than absolute zero"; System.out.println("F\tC"); doublestart=((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.
