Ask Question
4 May, 00:31

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]

+2
Answers (1)
  1. 4 May, 04:12
    0
    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)); } public static void buildArray (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).
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “9.6 Code Practice: Question 1 - Instructions Write a method named "buildArray" that builds an array by appending a given number of random ...” 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