"There are 33 problems to choose from. Write a program that randomly chooses one of them. Make the program choose 100 times. The program should report how many times each problem has been chosen, also the problem that was selected most. Make the program sort the problems descending by the number of times they were selected"
I have got everything running except that I haven't been able to sort the problems in a descending order by the numbers of the times they were sorted. I looked into hashmaps and also 2d arrays. Ran out of brain power before I could figure something out successfully. Help?
Code:
quesSimulator.java
ques.java
My current output:
I have got everything running except that I haven't been able to sort the problems in a descending order by the numbers of the times they were sorted. I looked into hashmaps and also 2d arrays. Ran out of brain power before I could figure something out successfully. Help?
Code:
quesSimulator.java
//import java.util.Comparator;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.TreeMap;
//import java.util.Set;
public class quesSimulator
{
public static void main(String[] args)
{ques d=new ques(34);
int[] array1= new int[50]; //array1 contains random values
for( int i=0; i<array1.length; i++)
{
array1=d.cast();
int j=array1;
System.out.print("{"+ j + "}");
}
System.out.println();
int[] array2= new int[33]; //array2 contains values 1-33
for( int i=0; i< 33 ; i++)
{
array2=(i+1);
// System.out.print(array2+ " ");
}
System.out.println();
int[] array3=new int[33]; //array3 contains [number of times that number is repeated]
//Map<Integer, Integer> NOT1 = new HashMap<Integer, Integer>();
for (int i=0; i< 33; i++)
{ int count=0;
for (int j=0; j< array1.length; j++)
if(array2==array1[j])
{
count=count+1;
//NOT1.put((i+1), count);
array3=count;
} System.out.println((i+1)+ " has occured " + count + " time/s ");
}
//Set<Integer> keySet =NOT1.keySet();
//for(Integer key : keySet)
// {Integer value=NOT1.get(key);
// System.out.println(key + " " + value);
//System.out.println(NOT1);
}
}
ques.java
import java.util.Random;
public class ques
{ private Random generator;
private int sides;
public ques(int s)
{ sides=s;
generator= new Random();
}
public int cast()
{ return 1+generator.nextInt(sides);}}
My current output:
run quesSimulator
{4}{12}{6}{3}{25}{30}{17}{14}{17}{9}{25}{14}{16}{5}{28}{1}{10}{27}{16}{1}{21}{34}{10}{20}{33}{19}{12}{24}{30}{3}{22}{18}{2}{24}{8}{13}{6}{4}{6}{13}{33}{21}{31}{8}{22}{3}{22}{25}{25}{29}
1 has occured 2 time/s
2 has occured 1 time/s
3 has occured 3 time/s
4 has occured 2 time/s
5 has occured 1 time/s
6 has occured 3 time/s
7 has occured 0 time/s
8 has occured 2 time/s
9 has occured 1 time/s
10 has occured 2 time/s
11 has occured 0 time/s
12 has occured 2 time/s
13 has occured 2 time/s
14 has occured 2 time/s
15 has occured 0 time/s
16 has occured 2 time/s
17 has occured 2 time/s
18 has occured 1 time/s
19 has occured 1 time/s
20 has occured 1 time/s
21 has occured 2 time/s
22 has occured 3 time/s
23 has occured 0 time/s
24 has occured 2 time/s
25 has occured 4 time/s
26 has occured 0 time/s
27 has occured 1 time/s
28 has occured 1 time/s
29 has occured 1 time/s
30 has occured 2 time/s
31 has occured 1 time/s
32 has occured 0 time/s
33 has occured 2 time/s