Ask Question
29 March, 17:26

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)

+1
Answers (1)
  1. 29 March, 18:05
    0
    import java. util. Arrays; import java. util. Random; public class Main { public static void main (String[] args) { int [] testArray = {}; buildArray (testArray, 12); } public static void buildArray (int [] arr, int size) { arr = new int[size]; Random rand = new Random (); for (int i=0; i < size; i++) { arr[i] = rand. nextInt (90) + 10; } System. out. println (Arrays. toString (arr)); } }

    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]
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Instructions Write a method named buildArray that builds an array by appending a given number of random two-digit integers. It should ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers