Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java Problem Statement: Write a program that generates 100 random integers betwe

ID: 3709623 • Letter: J

Question

java

Problem Statement: Write a program that generates 100 random integers between 0 and 9 and then reports the number of times each integer value was generated.

Problem Statement:
Write a program that generates 100 random integers between 0 and 9 and then reports the number of times each integer value was generated. Report also the integer value or values that were generated least and those which were generated the most.
An example run might look like this:

The count of values generated:
0: 15
1: 11
2:   5
3:   8
4: 10
5:   7
6: 15
7: 13
8:   5
9: 11

Values generated the least: 2, 8
Values generated the most: 0, 6

***** End of xxPLP08*****

Notes:

You have experience working with a randomly generated characters. Refer to those labs and be sure to set the upper limit for nine (9).

Use an array of ten integers -- call it counts for example -- to store the counts for the number of 0s, 1s,...9s.

Pass your array to a method that will generate and count the individual values. During testing, this method should also display the individual value when it is generated (see #5 below).

Use the printf() method on the report to align the integer counts on a right-justified boundary

You may want to test for validity by starting with a smaller number of randomly generated values and displaying each value as it is generated (and counted)

Name your source file (and class) xxPLP08.java where xx should be the initials of your first and last name.

Explanation / Answer

RandomNumberCount.java

import java.io.IOException;

import java.util.Random;

public class RandomNumberCount {

public static void main(String[] args) throws IOException {

Random r = new Random();

int counts[] = new int[10];

for(int i=0; i<100; i++){

counts[r.nextInt(10)]++;

}

generateCount(counts);

}

public static void generateCount(int counts[]){

int max = counts[0];

int maxIndex = 0;

int min = counts[0];

int minIndex = 0;

System.out.println("The count of values generated:");

for(int i=0; i<counts.length; i++) {

System.out.printf("%d: %d", i, counts[i]);

if(max < counts[i]){

max = counts[i];

maxIndex = i;

}

if(min > counts[i]){

min = counts[i];

minIndex = i;

}

System.out.println();

}

System.out.println("Values generated the least: "+maxIndex+", "+max);

System.out.println("Values generated the most: "+minIndex+", "+min);

}

}

Output:

The count of values generated:
0: 5
1: 9
2: 10
3: 7
4: 16
5: 9
6: 10
7: 10
8: 10
9: 14
Values generated the least: 4, 16
Values generated the most: 0, 5