Task Write a Java program generates 10 random integers betwe
Task: Write a Java program generates 10 random integers between 1 and 100 and stores them into an array. The program then goes through the array and prints each number out followed by showing the minimum, maximum and average of all the numbers in it. For example, say the 10 random numbers picked are
5
22
63
31
7
42
100
68
22
91
The output should be: 5 22 63 31 7 42 100 68 22 91 Minimum: 5 Maximum: 100 Average: 45.1 You must use an array in this program. Use the java.util.Random class to generate your random numbers as we did with the Dice program discussed earlier in class. You must also use functions to find the minimum, maximum and average. Pass the array as a parameter into the function and return the result. For example, your findMinimum function should have the following signature: public static int findMinimum(int[] a) After creating the array in main, call these functions appropriately to get the necessary values and print them all in main.
Solution
RandomNum.java
import java.util.Random;
public class RandomNum {
public static void main(String[] args) {
Random r= new Random();
int a[] = new int[10];
for(int i =0; i<a.length; i++){
a[i] = r.nextInt(100)+1;
}
System.out.println(\"Random numbera are: \");
for(int i =0; i<a.length; i++){
System.out.print(a[i] +\" \");
}
System.out.println();
System.out.println(\"Minimum: \"+findMinimum(a));
System.out.println(\"Maximum: \"+findMaximum(a));
System.out.println(\"Average: \"+findAverage(a));
}
public static int findMinimum(int[] a) {
int min = a[0];
for(int i=0; i<a.length; i++){
if(min > a[i]){
min = a[i];
}
}
return min;
}
public static int findMaximum(int[] a) {
int max = a[0];
for(int i=0; i<a.length; i++){
if(max < a[i]){
max = a[i];
}
}
return max;
}
public static double findAverage(int[] a) {
int sum =0;
for(int i=0; i<a.length; i++){
sum = sum + a[i];
}
return sum/(double)a.length;
}
}
Output:
Random numbera are:
83 82 71 35 91 18 17 47 94 9
Minimum: 9
Maximum: 94
Average: 54.7

