Unformatted text preview:

Newton1and2.java Thursday, January 27, 2022, 2:16 PM1 import components.simplereader.SimpleReader;56 /**7 * Calculate the square root an inputed number with a given error range that is8 * compatible with 0.9 *10 * @author Chime Nwaru11 *12 */13 public final class Newton1and2 {1415 /**16 * Private constructor so this utility class cannot be instantiated.17 */18 private Newton1and2() {19 }2021 /**22 * Computes estimate of square root of x to within relative error 0.01%.23 *24 * @param x25 * positive number to compute square root of26 * @return estimate of square root27 */28 private static double sqrt(double x) {2930 /*31 * Finds the square root of the given number by going from 0 to the32 * given number and seeing if that number squared is within .0001 of the33 * given number34 */35 double range = x;36 for (double i = 0.0; i < x; i = i + .0001) {37 if (Math.abs(i * i - x) < Math.abs(range * range - x)) {38 range = i;39 }4041 }4243 return range;4445 }4647 /**48 * Put a short phrase describing the static method myMethod here.49 */5051 /**52 * Main method.53 *54 * @param args55 * the command line arguments56 */57 public static void main(String[] args) {58 SimpleReader in = new SimpleReader1L();59 SimpleWriter out = new SimpleWriter1L();60 /*Page 1Newton1and2.java Thursday, January 27, 2022, 2:16 PM61 * Put your main program code here; it may call myMethod as shown62 */6364 /*65 * Asks for the user to input the number a number to find the square66 * root of67 */68 out.print("What number do you want to find the square root of: ");69 double x = in.nextDouble();7071 /*72 * Calls the sqrt method and prints out the square root73 */74 double root = sqrt(x);75 out.print("The square root is " + root);76 /*77 * Close input and output streams78 */79 in.close();80 out.close();81 }8283 }84Page


View Full Document

UIUC SOC 100 - Newton 1

Download Newton 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Newton 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Newton 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?