Hello. The follwoing program must be done in Eclipse java. In statistics, the mu
ID: 3571390 • Letter: H
Question
Hello. The follwoing program must be done in Eclipse java.
In statistics, the multi-mode is defined as the most frequently occurring set of numbers in a given dataset.
In the list {1, 3, 3, 5, 5, 7}, there are two modes {3, 5}. They both occur the most frequently among the set.
Write a program that would calculate the multi-mode for a randomly generated set of 60 integers with values between 10-100. Use Collections.sort method to sort the ArrayList.
This line is required in the program:
System.out.println("Multi-mode: { ... }, occurred X times");
I need the answer quickly. Any kind of assistance is appreciated
Explanation / Answer
CODE:
import java.util.*;
public class RandomNumber {
public static void main(String[] args){
ArrayList list = new ArrayList();
Random rand = new Random();
int num;
for (int j = 0; j<60; j++)
{
num = rand.nextInt(100);
list.add(num);
}
Collections.sort(list);
System.out.println("The list is: "+list);
showMode(list);
}
public static void showMode(ArrayList<Integer> arrayList){
Map<Integer,Integer> finalModes= new HashMap<Integer,Integer>();
int maxCount=0;
for (int i = 0; i < arrayList.size(); ++i){
int count = 0;
for (int j = 0; j < arrayList.size(); ++j){
if (arrayList.get(j) == arrayList.get(i)) ++count;
}
if (count > maxCount){
maxCount = count;
finalModes.clear();
finalModes.put(arrayList.get(i), count);
} else if ( count == maxCount ){
finalModes.put(arrayList.get(i), count);
}
}
System.out.println("Multi-mode: "+finalModes);
}
}
OUTPUT:
The list is: [3, 3, 4, 8, 8, 10, 10, 11, 14, 15, 15, 19, 20, 22, 24, 24, 24, 25, 26, 26, 26, 34, 35, 35, 35, 40, 43, 43, 43, 46, 46, 46, 46, 48, 50, 51, 53, 54, 55, 55, 57, 59, 59, 64, 68, 68, 69, 71, 71, 72, 75, 77, 78, 79, 79, 81, 87, 92, 95, 95]
Multi-mode: {46=4}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.