9.6 Code Practice: Question 1
Instructions
Write a method named buildArray that builds an array by appending a given number of random two-digit integers. It should accept two parameters—the first parameter is the array, and the second is an integer for how many random values to add.
Print the array after calling buildArray.
Sample Run
How many values to add to the array:
12
[14, 64, 62, 21, 91, 25, 75, 86, 13, 87, 39, 48]
Answers
Answer:
import java.util.Random;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int n = 10;
int [] myArray = new int[n];
buildArray(myArray, n);
System.out.println(Arrays.toString(myArray));
(int[] arr, int n)
{ for(int i=0; i < arr.length; i++){
Random rand = new Random();
arr[i] = rand.nextInt(90) +10;
Explanation:Firstly, create a method buildArray that take two inputs, an array and an array size (Line 12). In the method, use random nextInt method to repeatedly generate a two digit random number within a for loop that will loop over array size number of times (Line 13-15).
The expression rand.nextInt(90) + 10 will generate digits between 10 - 99.In the main program, call the build array method by using an array and array size as input arguments (Line 8).
At last, print the array after calling the buildArray method (Line 9).
Answer:
Hi buddy
Explanation:
hope helpful for you