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)​

Respuesta :

Answer:

  1. import java.util.Arrays;
  2. import java.util.Random;
  3. public class Main {
  4.    public static void main(String[] args) {
  5.        int [] testArray = {};
  6.        buildArray(testArray, 12);
  7.    }
  8.    public static void buildArray(int [] arr, int size){
  9.        arr = new int[size];
  10.        Random rand = new Random();
  11.        for(int i=0; i < size; i++){
  12.            arr[i] = rand.nextInt(90) + 10;
  13.        }
  14.        System.out.println(Arrays.toString(arr));
  15.    }
  16. }

Explanation:

The solution code is written in Java.

Firstly, create a method buildArray that takes two parameters, an array and an array size (Line 10).

In the method, use the input size to build an array with size-length (Line 11). Next, create a Random object and use a for loop to repeatedly generate a random integer using the Random object nextInt method. The expression rand.nextInt(90) + 10 will return an integer between 10 - 99. Each generated random integer is assigned as the value for one array item (Line 16).

At last, print out the array to terminal (Line 19).

In the main program, we can test the method by passing an empty array and a value 12 as array size (Line 7). We will get a sample array as follow:

[81, 36, 15, 20, 32, 84, 10, 13, 98, 12, 45, 45]

ACCESS MORE