Define a Java method named valueCounter) with the following header: public stati
ID: 3589065 • Letter: D
Question
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) 5occurs 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
FrequencyOfArrayElements.java
import java.util.Random;
import java.util.Scanner;
public class FrequencyOfArrayElements {
public static void main(String[] args) {
//Creating an int array of size 5
int arr[] = new int[5];
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
for (int i = 0; i < arr.length; i++) {
System.out.print("Enter the element#" + (i + 1) + ":");
arr[i] = sc.nextInt();
}
//calling the method by passing the array as argument
valueCounter(arr);
}
//This function will display the frequency of elements in the array
private static void valueCounter(int[] arr) {
int counter;
for (int i = 0; i < arr.length; i++) {
counter = 1;
for (int j = i + 1; j <= arr.length - 1; j++) {
if (arr[i] == arr[j] && arr[i] != '') {
counter++;
arr[j] = '';
}
}
if (arr[i] != '') {
System.out.println(arr[i] + " occurs " + counter + " time(s)");
}
}
}
}
___________________
Output:
Enter the element#1:2
Enter the element#2:33
Enter the element#3:44
Enter the element#4:33
Enter the element#5:2
2 occurs 2 time(s)
33 occurs 2 time(s)
44 occurs 1 time(s)
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.