import java.util.Scanner; /* Java application that displays a table of square roots. ** The user is prompted to enter a lower bound, an upper bound, ** and a "delta" value, and the program displays a table ** showing the square roots of numbers in the specified range, ** differing by the specified delta. For each such number, ** it is printed, as well as the square root of its absolute ** value, as well as the integer closest to that square root. ** ** The program serves to illustrate the use of ** (1) "functional" methods (i.e., ones that return values) ** (e.g., methods in the java.lang.Math class and ones defined here) ** (2) a Scanner object to read input data from the keyboard ** (3) the System.out.printf() method for formatting output ** */ public class SqRootTable { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { double lower = getDoubleFromUser("Enter lower bound:"); // user enters lower double upper = getDoubleFromUser("Enter upper bound:"); // and upper bounds, double delta = getDoubleFromUser("Enter delta:"); // and the delta value System.out.println(" x sqrt(|x|) rounded sqrt(|x|)"); System.out.println(" ------- -------------- -------------------"); for (double x = lower; x <= upper; x = x + delta) { double sqRootOfX = Math.sqrt(Math.abs(x)); long sqRootOfXRounded = Math.round(sqRootOfX); System.out.printf("%8.2f %11.6f %4d \n", x, sqRootOfX, sqRootOfXRounded); } } private static double getDoubleFromUser(String prompt) { System.out.print(prompt); return keyboard.nextDouble(); } }