Java\'s Math class contains a static method called random. The RootTwo class bel
ID: 3650101 • Letter: J
Question
Java's Math class contains a static method called random. The RootTwo class below computes an approximation to the the square root of 2. However, there's a missing statement in the code. In the answer box provided, assign an appropriate random value to the variable val so that the code wil calculate the intended approximation. Hint: the algorithm works this way: the square root of 2 falls between 1 and 2. If you choose 1000 random non-integral values between 1 and 2, the fraction of these values that fall below the square root of 2 give a reasonable estimate for the decimal part of that square root. (For example, if 951 of the 1000 fall below the square root of 2, then our estimate would be 1.951). Ifs easy to check if a number falls below the square root of 2: just multiply it by itself (square it), and compare the result with 2.0. import java.util.Scanner; public class RootTwo { public static void main(String[] args) { System.out.println(Enter the number of iterations:"); Scanner scan = new Scanner(System.in); int ct = 0; int n = scan.nextlnt(); double val; for(int j = 0;jExplanation / Answer
Add this line:
val=Math.random()+1;
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.