Answer:
import java.util.Random;
public class NumberHistogram {
public static void main(String[] args) {
int[] data = new int[25];
Random rand = new Random();
// Fill data array with random numbers from 0-9
for (int i = 0; i < data.length; i++) {
data[i] = rand.nextInt(10);
}
int[] histogram = new int[10];
// Count occurrences of each number in data and store in histogram array
for (int i = 0; i < data.length; i++) {
histogram[data[i]]++;
}
// Print histogram
for (int i = 0; i < histogram.length; i++) {
System.out.println(i + " - occurred " + histogram[i] + " time(s)");
}
}
}
Step-by-step explanation:
This program uses a for loop to fill the data array with random integers between 0 and 9, and then uses another for loop to count the occurrences of each number in data and store the counts in the histogram array. Finally, the program uses a third for loop to print out the histogram.
The test variable in this program is the randomly generated integers in the data array. The outcome variable is the histogram of the occurrences of each number in the data array. The control variable is the fixed range of integers from 0 to 9.
Note that this solution uses an array to avoid using ten if statements inside the loop. Instead, it uses the current element of the data array as an index into the histogram array and increments the corresponding element. This way, the counts for each number are automatically tallied without needing to check each possible value.