UIUC SOC 100 - Newton 1and 2

Unformatted text preview:

Newton1and2 java Thursday January 27 2022 2 40 PM 1 import components simplereader SimpleReader 5 6 7 Calculate the square root an inputed number with a given error range that is 8 compatible with 0 9 10 author Chime Nwaru 11 12 13 public final class Newton1and2 14 15 16 Private constructor so this utility class cannot be instantiated 17 18 private Newton1and2 19 20 21 22 Computes estimate of square root of x to within relative error 0 01 23 24 param x 25 positive number to compute square root of 26 return estimate of square root 27 28 private static double sqrt double x 29 30 31 Finds the square root of the given number by going from 0 to the 32 given number and seeing if that number squared is within 0001 of the 33 given number 34 35 final double error 0001 36 double range x 37 38 The code goes through every possible value from 0 0 to x 1 the plus 39 1 accounts for the square root of number between 0 and 1 and keeps 40 the first value that gives answer that s within the error range 41 42 for double i 0 0 i x 1 i i error 43 if Math abs i i x Math abs range range x 44 range i 45 46 47 48 49 return range 50 51 52 53 54 Put a short phrase describing the static method myMethod here 55 56 57 58 Main method 59 60 param args Page 1 Newton1and2 java Thursday January 27 2022 2 40 PM 61 the command line arguments 62 63 public static void main String args 64 SimpleReader in new SimpleReader1L 65 SimpleWriter out new SimpleWriter1L 66 67 Put your main program code here it may call myMethod as shown 68 69 70 71 Asks for the user to input the number a number to find the square 72 root of 73 74 out print What number do you want to find the square root of 75 double x in nextDouble 76 77 78 Calls the sqrt method and prints out the square root 79 80 double root sqrt x 81 out print The square root is root 82 83 Close input and output streams 84 85 in close 86 out close 87 88 89 90 Page 2

View Full Document

# UIUC SOC 100 - Newton 1and 2

Pages: 2
Documents in this Course

2 pages

3 pages

1 pages

2 pages

1 pages

1 pages

17 pages

1 pages

1 pages

1 pages

1 pages

2 pages