See below for the modified program in Java
<h3>How to modify the program?</h3>
The for loop statements in the program are:
- for (int i = 0; i < arr.length; i++) { arr[i] = rd.nextInt(1000); }
- for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
To use the enhanced for loop, we make use of the for each statement.
This syntax of the enhanced for loop is:
for(int elem : elems)
Where elem is an integer variable and elems is an array or arrayList
So, we have the following modifications:
- i = 0; for (int num : arr){ arr[i] = rd.nextInt(1000); i++;}
- for (int num : arr) { System.out.println(num); }
Hence, the modified program in Java is:
class Main {
static int[] createRandomArray(int nrElements) {
Random rd = new Random();
int[] arr = new int[nrElements];
int i = 0;
for (int num : arr){
arr[i] = rd.nextInt(1000);
i++;
}
return arr;
}
static void printArray(int[] arr) {
for (int num : arr) {
System.out.println(num);
}
}
}
public static void main(String[] args) {
int[] arr = createRandomArray(5);
printArray(arr);
}
}
Read more about enhanced for loop at:
brainly.com/question/14555679
#SPJ1