import java.util.Scanner; /* Java application that produces a Fahrenheit-to-Celsius conversion table. ** The user provides as input (via the keyboard) the floor and ceiling ** Fahrenheit temperatures and the "delta" (the amount by which each line's ** Fahrenheit temperature exceeds that of the previous line). ** (This version improves upon the previous one by using System.out.printf() ** to format the temperature readings so that they line up nicely in the ** two columns (with the decimal points directly above/below each other in ** the same column).) ** ** Author: R. McCloskey ** Date: March 2015 */ public class FahToCel5 { // Create a new Scanner object that can read input from the keyboard. // By being declared here (outside any method), this variable becomes // "global", meaning that its scope is the entire class. private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { // Declare the input variables double fahFloor; // first Fah. temp. in the table double fahDelta; // increase in Fah. temp. from one row to next double fahCeil; // upper bound on Fah. temp.'s in the table // For each input, prompt the user and read it. fahFloor = getDoubleResponse("Enter Fahrenheit floor temperature: "); fahDelta = getDoubleResponse("Enter Fahrenheit delta: "); fahCeil = getDoubleResponse("Enter Fahrenheit ceiling: "); // Produce the table. printTable(fahFloor, fahDelta, fahCeil); } /* Displays the specified prompt, reads the user's response (using ** the global Scanner object, keyboard), converts it to a value of ** type double, and returns that value to the caller. */ private static double getDoubleResponse(String prompt) { System.out.print(prompt); // Display the prompt. return keyboard.nextDouble(); // Read response and return it. } /* Prints the Fahrenheit-to-Celsius conversion table, where the first ** row's Fah. temp, the increase in Fah. temp from one row to the next, ** and the ceiling Fah. temp. are provided via the parameters. */ private static void printTable(double fLow, double fDelta, double fHigh) { // Print column headings. System.out.printf("%15s %9s\n","Fahrenheit", "Celsius"); System.out.printf("%15s %10s\n","----------", "---------"); // Alternative to above printing of column headings: //System.out.println(" Fahrenheit Celsius"); //System.out.println(" ---------- ---------"); // Produce the body of the table. Each loop iteration produces // a single row, which includes a row number, a Fahrenheit temp., // and the equivalent Celsius temp. int rowNum = 1; for (double fah = fLow; fah <= fHigh; fah = fah + fDelta) { double celTemp = fahToCel(fah); // Display the row number in a field of width 3, followed by a period // and a few spaces, the Fahrenheit temperature (fah) in a field of // width 7 with 2 digits after the decimal point, followed by 'F', // followed by a few spaces, followed by the equivalent Celsius // temperature (celTemp) in a field of width 8 with 3 digits after // the decimal point, followed by 'C', and use the newline character // ('\n') to advance to the next line. System.out.printf("%3d. %7.2fF %8.3fC\n", rowNum, fah, celTemp); rowNum = rowNum + 1; // increment the row number } } /* Returns the Celsius equivalent of the given Fahrenheit temperature (f). */ private static double fahToCel(double f) { return (5.0 / 9.0) * (f - 32); } }