Playing Compiler
Determine whether or not each of the following classes compiles, and if not, fix the errors. Each class should compute the square root of 122.0.
a. public class Test
{ public static void main(String[] args) { float x = (float) 122.0; float newGuess = (float) 1.0; float oldGuess = x; // This code computes the square root of x; try { while (oldGuess != newGuess) { oldGuess = newGuess; newGuess = (float) (x/oldGuess = oldGuess)/ (float) 2.0; System.out.println(oldGuess); System.out.println(newGuess); } // Keep improving the guess until two consecutive guesses are equal catch { System.out.println("Did not work"); } } }b. public class Test { public static void main(String[] args) throws ArithmeticException { float x = (float) 122.0; float newGuess = (float) 1.; float oldGuess = x; // This code computes the square root of x; try { while (oldGuess != newGuess) { oldGuess = newGuess; newGuess = (float) (x/oldGuess = oldGuess)/ (float) 2.0; System.out.println(oldGuess); System.out.println(newGuess); } // Keep improving the guess until two consecutive guesses are equal finally {} } }c. public class Test { public static void main(String[] args) throws ArithmeticException { float x = (float) 122.0; float newGuess = (float) 1; float oldGuess = x; // This code computes the square root of x; try { while (oldGuess != newGuess) { oldGuess = newGuess; newGuess = (float) (x/oldGuess = oldGuess)/ (float) 2.0; System.out.println(oldGuess); System.out.println(newGuess); } // Keep improving the guess until two consecutive guesses are equal } catch (Arithmetic Exception e) { System.out.println("Bad division in algorithm"); } } }
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.