In the main method, write the Java code that computes

      In the main method, write the Java code that computes an estimate of pi based on the percentage of numbers from a random sampling that are relatively prime (e.g., coprime). The instructions for createArray and percentageRelativelyPrime along with the source code for gcd are given below. (a) Prompt the user for two integers: a number of samples (numSamples) and a maximum random number (maxRandom). (b) Make two calls to createArray to populate two arrays (array1 and array2 ) with numSamples integers from 1 to maxRandom, including the endpoints. (c) Call the method percentageRelativelyPrime with the actual parameters array1 and array2, storing the return value in a double variable percent. (d) Print the result of the following equation with six (6) positions after the decimal. πestimate = r 6.0 percent (e) Ask the user if they would like to continue. Repeat the above steps until the user elects to stop.

     Write a new method in the same class as the method main to create an array of random integers and return the reference of the array. (a) The public and static method named createArray accepts two (2) integer formal parameters and returns an integer array reference variable. (b) The two formal parameters define the size of the array to be created and the maximum value to be stored in the array. (c) Declare an integer array reference variable and array object with the number of elements from the size parameter. Should the size parameter be zero or negative, create an array of size one hundred (100). (d) Fill the integer array with random values based on the following expression: R∗maxV al+1, where R is a call to Math.random() and maxV al is the maximum value parameter. Should maxV al by zero or negative, replace the maximum value parameter with one hundred (100). (e) Return the array reference variable to the calling method. 

    Write a new method in the same class as the method main to repeatedly call the gcd method (given below) for every corresponding pair of elements from two integer arrays and return the percentage of pairs that were relatively prime. (a) The public and static method named percentageRelativelyPrime accepts two (2) integer array formal parameters and returns a double value  (b) The two formal parameters A and B are arrays of equal length. Iterate through the elements of the arrays and increment a counter whenever Ai and Bi are relatively prime (i.e., their greatest common divisor is 1). (c) Compute and return the counter divided by the number of elements in an array 

 public static int gcd(int number1, int number2) { int smaller = Math.min(number1,number2); for(int lcv = smaller; lcv > 1; lcv–) { if(number1 % lcv == 0 && number2 % lcv == 0) return lcv; //the gcd of number1 and number 2 } return 1; //number1 and number2 are relatively prime } 

Looking for a Similar Assignment? Our Experts can help. Use the coupon code SAVE30 to get your first order at 30% off!