2. Define a Java method named valueCounter) with the following header: public st
ID: 3589338 • Letter: 2
Question
2. Define a Java method named valueCounter) with the following header: public static void valuecounter (int values) This method takes an array of integers as its argument and prints the number of times that each unique value appears in the array. Your solution does not need to print the array contents in sorted order, but it MUST NOT print any values that do not exist in the array. For example, given the array [23, 12, 14, 12, 5] acceptable output would be: 23 occurs 1 time(s). 12 occurs 2 time(s) 14 occurs 1 time(s). 5 occurs 1 time(s). Note: You may assume that the array only contains integers in the range 0-100, inclusive. Finish this problem by writing a complete Java program that reads in (or generates via Math.random())a series of integer values in the range 0-100, stores them in an array, and calls valueCounter) to determine and display the frequency of each unique value.Explanation / Answer
package chegg2counter;
import java.util.Random;
public class counter {
public static void main(String[] args) {
Random rand = new Random();
int[] random_values = new int[1000]; // array size 1000 can be increased to increase the counts
for (int i = 0; i < 1000; i++) { // same array count should be given here
random_values[i] = rand.nextInt(101) ; // This will make sure the values are between 0 to 100
}
valueCounter(random_values);
}
public static void valueCounter(int[] values) {
int[] count_random_values = new int[101]; // arrays size equals to the total number of integers under consideration i.e. 0 - 100 both inclusive
int temp_var;
for(int i = 0; i < values.length; i++){
temp_var = values[i];
count_random_values[temp_var]++;
}
for(int i=0; i <= 100; i++){
System.out.printf("%d occurs %d time ",i, count_random_values[i]);
}
}
}
Kindly Let me know incase you need more details.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.