**Java Language only** Directions: the code below tells me the first, second and
ID: 3742316 • Letter: #
Question
**Java Language only**
Directions: the code below tells me the first, second and third most relevant numbers that popped up in the array, as well as how many times they popped up in the code. It also gives me the numbers chance percentage of it being in the range of (1-5)(6-10)(11-15)(16-20)(21-25)(26-30)(31-35)(36-40)(41-45)(46-50) (51-55)(56-60)(61-65)(66-70) Only 1 throught 70 Eample below. Add to the code below making it tell me what the chance percentage of the precise numbers 1-70, 1 All the way to 70 . Have the output next to its chance spot in the code. Look below for current Example: Chances 1-5=42.68% (Num 1 chance 12% ) (Num 2 chance 10%) (Num 3 chance 10%) (Num 4 chance 5%) (Num 5 chance 5.68%)
Chances 6-10=20.73 (Num 1 chance 5.73% ) (Num 2 chance 5%) (Num 3 chance 5%) (Num 4 chance 5%) (Num 5 chance 5%)
I Will Thumbs up good work Thanks!!!
/**
Current Output:
First Relevance Number: output:1,17,31,64,70,2
Most relevant numbers came up (1 came up 12 times) (17 came up 7 times) (31 came up 7 times) (64 came up 5 times) (70 came up 7 times) (2 came up 6 times)
Second Relevance Number: output:2,13,23,56,59,9
Second most relevant numbers came up (2 came up 9 times) (13 came up 5 times) (23 came up 4 times) (56 came up 4 times) (59 came up 6 times) (9 came up 5 times)
Third Relevance Number: output:4,11,18,66,67,1
Third most relevant numbers came up (4 came up 7 times) (11 came up 4 times) (18 came up 3 times) (66 came up 3 times) (67 came up 5 times) (1 came up 4 times)
For array1:
Chances 1-5 = 42.68%
Chances 6-10 = 20.73%
Chances 11-15 = 13.41%
Chances 16-20 = 10.98%
Chances 21-25 = 4.88%
Chances 26-30 = 4.88%
Chances 31-35 = 1.22%
Chances 36-40 = 0.00%
Chances 41-45 = 0.00%
Chances 46-50 = 0.00%
Chances 51-55 = 0.00%
Chances 56-60 = 0.00%
Chances 61-65 = 0.00%
Chances 66-70 = 0.00%
For array2:
Chances 1-5 = 6.80%
Chances 6-10 = 10.68%
Chances 11-15 = 19.42%
Chances 16-20 = 15.53%
Chances 21-25 = 10.68%
Chances 26-30 = 12.62%
Chances 31-35 = 9.71%
Chances 36-40 = 7.77%
Chances 41-45 = 3.88%
Chances 46-50 = 0.00%
Chances 51-55 = 1.94%
Chances 56-60 = 0.97%
Chances 61-65 = 0.00%
Chances 66-70 = 0.00%
For array3:
Chances 1-5 = 2.70%
Chances 6-10 = 1.35%
Chances 11-15 = 5.41%
Chances 16-20 = 5.41%
Chances 21-25 = 10.81%
Chances 26-30 = 8.11%
Chances 31-35 = 14.86%
Chances 36-40 = 14.86%
Chances 41-45 = 10.81%
Chances 46-50 = 6.76%
Chances 51-55 = 10.81%
Chances 56-60 = 5.41%
Chances 61-65 = 2.70%
Chances 66-70 = 0.00%
For array4:
Chances 1-5 = 0.00%
Chances 6-10 = 2.67%
Chances 11-15 = 1.33%
Chances 16-20 = 1.33%
Chances 21-25 = 9.33%
Chances 26-30 = 4.00%
Chances 31-35 = 6.67%
Chances 36-40 = 4.00%
Chances 41-45 = 13.33%
Chances 46-50 = 17.33%
Chances 51-55 = 6.67%
Chances 56-60 = 10.67%
Chances 61-65 = 13.33%
Chances 66-70 = 8.00%
For array5:
Chances 1-5 = 0.00%
Chances 6-10 = 2.70%
Chances 11-15 = 0.00%
Chances 16-20 = 0.00%
Chances 21-25 = 1.35%
Chances 26-30 = 1.35%
Chances 31-35 = 1.35%
Chances 36-40 = 4.05%
Chances 41-45 = 10.81%
Chances 46-50 = 6.76%
Chances 51-55 = 10.81%
Chances 56-60 = 17.57%
Chances 61-65 = 14.86%
Chances 66-70 = 24.32%
For array6:
Chances 1-5 = 22.37%
Chances 6-10 = 21.05%
Chances 11-15 = 22.37%
Chances 16-20 = 13.16%
Chances 21-25 = 21.05%
Chances 26-30 = 0.00%
Chances 31-35 = 0.00%
Chances 36-40 = 0.00%
Chances 41-45 = 0.00%
Chances 46-50 = 0.00%
Chances 51-55 = 0.00%
Chances 56-60 = 0.00%
Chances 61-65 = 0.00%
Chances 66-70 = 0.00%
BUILD SUCCESSFUL (total time: 0 seconds)
/*
// Cheggs.java
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Cheggs {
public static TreeMap<Integer, Integer> getFrequencyMap(int num[]) {
HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
for (int i = 0; i < num.length; i++) {
if (counts.containsKey(num[i])) {
int c = counts.get(num[i]) + 1;
counts.put(num[i], c);
}
else {
counts.put(num[i], 1);
}
}
ValueComparator<Integer, Integer> bvc = new ValueComparator<Integer, Integer>(
counts);
TreeMap<Integer, Integer> sortedMap = new TreeMap<Integer, Integer>(bvc);
sortedMap.putAll(counts);
return sortedMap;
}
public static void main(String args[]) {
int num1[] = { 1,21,1,2,13,2,17,2,11,+
1,1,14,22,5,2,118,4,2,2,3,1,12,8,5,16,4,11,7,4,14,1,6,7,1,+
24,10,7,17,14,5,28,14,1,29,10,2,6,3,17,16,28,1,4,10,1,28,4,8,6,14,16,10,16,3,+
3,1,6,1,10,6,17,20,6,31,2,7,21,12,4,11,4,2 };
int num2[] = { 6,16,31,26,3,13,28,15,32,2,14,41,22,17,10,35,32,25,6,13,+
11,3,30,33,24,11,14,17,26,34,38,15,5,7,29,19,15,34,9,13,33,29,28,25,8,38,13,30,+
17,4,28,17,11,19,38,12,34,17,5,41,16,6,12,11,18,29,20,42,10,12,20,37,12,23,+
37,15,22,17,36,7,26,14,23,54,22,28,57,24,23,45,7,16,33,18,51,10,39,28,24,+
37,5,17,13 };
int num3[] = { 4,30,61,39,45,47,29,11,28,37,5,33,+
41,52,55,51,21,53,44,40,39,10,20,38,31,18,44,32,31,51,39,31,43,23,51,26,58,+
18,26,42,21,13,23,48,15,30,23,23,33,31,39,47,18,36,24,46,37,+
40,47,55,21,38,60,42,31,62,57,54,31,34,41,14,59,31 };
int num4[] = { 32,34,18,22,21,54,64,49,66,22,56,50,49,49,65,47,8,44,48,+
62,58,64,36,64,36,64,49,12,55,63,47,42,43,42,46,56,53,64,33,+
60,46,24,53,38,31,49,29,46,67,59,33,42,+
44,25,60,42,41,51,6,22,42,68,56,66,56,26,61,45,25,73,66,63,50,+
26,70 };
int num5[] = {55,22,59,37,56,46,42,62,62,67,68,70,56,44,70,51,70,+
56,52,66,67,68,48,45,65,59,53,67,62,59,59,59,70,52,61,66,+
39,64,43,58,43,58,49,47,58,40,57,43,55,66,70,62,41,42,70,67,59,+
61,64,50,70,28,53,52,64,73,6,67,74,75,66,61,34,6 };
int num6[] = { 17,6,2,2,1,12,2,14,16,23,23,19,20,14,6,3,13,4,17,+
2,25,9,11,21,22,13,21,1,11,10,4,9,11,17,22,22,8,23,9,14,+
16,1,14,25,4,24,1,7,10,22,7,25,16,9,24,22,8,5,19,24,11,3,+
12,13,6,9,11,12,2,12,15,10,7,2,17,5 };//20 came 5 22 came up 5 23 came 5 times
TreeMap<Integer, Integer> sortedMap1 = getFrequencyMap(num1);
TreeMap<Integer, Integer> sortedMap2 = getFrequencyMap(num2);
TreeMap<Integer, Integer> sortedMap3 = getFrequencyMap(num3);
TreeMap<Integer, Integer> sortedMap4 = getFrequencyMap(num4);
TreeMap<Integer, Integer> sortedMap5 = getFrequencyMap(num5);
TreeMap<Integer, Integer> sortedMap6 = getFrequencyMap(num6);
for (int i = 0; i < 3; i++) {
if (i == 0) {
System.out.print("First Relevance Number: output:");
// storing all the keys (relevant numbers) in integer variables
int v1 = (Integer) sortedMap1.keySet().toArray()[i];
int v2 = (Integer) sortedMap2.keySet().toArray()[i];
int v3 = (Integer) sortedMap3.keySet().toArray()[i];
int v4 = (Integer) sortedMap4.keySet().toArray()[i];
int v5 = (Integer) sortedMap5.keySet().toArray()[i];
int v6 = (Integer) sortedMap6.keySet().toArray()[i];
// displaying them
System.out.print(v1 + "," + v2 + "," + v3 + "," + v4 + "," + v5
+ "," + v6);
System.out.println();
System.out.print(" Most relevant numbers came up ");
// displaying relevant numbers and their number of occurrences
System.out.print("(" + v1 + " came up " + sortedMap1.get(v1)
+ " times) ");
System.out.print("(" + v2 + " came up " + sortedMap2.get(v2)
+ " times) ");
System.out.print("(" + v3 + " came up " + sortedMap3.get(v3)
+ " times) ");
System.out.print("(" + v4 + " came up " + sortedMap4.get(v4)
+ " times) ");
System.out.print("(" + v5 + " came up " + sortedMap5.get(v5)
+ " times) ");
System.out.print("(" + v6 + " came up " + sortedMap6.get(v6)
+ " times) ");
System.out.println();
}
if (i == 1) {
System.out.print(" Second Relevance Number: output:");
int v1 = (Integer) sortedMap1.keySet().toArray()[i];
int v2 = (Integer) sortedMap2.keySet().toArray()[i];
int v3 = (Integer) sortedMap3.keySet().toArray()[i];
int v4 = (Integer) sortedMap4.keySet().toArray()[i];
int v5 = (Integer) sortedMap5.keySet().toArray()[i];
int v6 = (Integer) sortedMap6.keySet().toArray()[i];
System.out.print(v1 + "," + v2 + "," + v3 + "," + v4 + "," + v5
+ "," + v6);
System.out.println();
System.out.print(" Second most relevant numbers came up ");
System.out.print("(" + v1 + " came up " + sortedMap1.get(v1)
+ " times) ");
System.out.print("(" + v2 + " came up " + sortedMap2.get(v2)
+ " times) ");
System.out.print("(" + v3 + " came up " + sortedMap3.get(v3)
+ " times) ");
System.out.print("(" + v4 + " came up " + sortedMap4.get(v4)
+ " times) ");
System.out.print("(" + v5 + " came up " + sortedMap5.get(v5)
+ " times) ");
System.out.print("(" + v6 + " came up " + sortedMap6.get(v6)
+ " times) ");
System.out.println();
}
if (i == 2) {
System.out.print(" Third Relevance Number: output:");
int v1 = (Integer) sortedMap1.keySet().toArray()[i];
int v2 = (Integer) sortedMap2.keySet().toArray()[i];
int v3 = (Integer) sortedMap3.keySet().toArray()[i];
int v4 = (Integer) sortedMap4.keySet().toArray()[i];
int v5 = (Integer) sortedMap5.keySet().toArray()[i];
int v6 = (Integer) sortedMap6.keySet().toArray()[i];
System.out.print(v1 + "," + v2 + "," + v3 + "," + v4 + "," + v5
+ "," + v6);
System.out.println();
System.out.print(" Third most relevant numbers came up ");
System.out.print("(" + v1 + " came up " + sortedMap1.get(v1)
+ " times) ");
System.out.print("(" + v2 + " came up " + sortedMap2.get(v2)
+ " times) ");
System.out.print("(" + v3 + " came up " + sortedMap3.get(v3)
+ " times) ");
System.out.print("(" + v4 + " came up " + sortedMap4.get(v4)
+ " times) ");
System.out.print("(" + v5 + " came up " + sortedMap5.get(v5)
+ " times) ");
System.out.print("(" + v6 + " came up " + sortedMap6.get(v6)
+ " times) ");
System.out.println();
}
}
/**
* finding the chances of numbers between ranges
* 1-5)(6-10)(11-15)(16-20)(21-25)(26-30)(31-35)(36-40)(41-45)(46-50)
* (51-55)(56-60)(61-65)(66-70) in the first array
*/
System.out.println("For array1:");
int chances = 1; // starting value of first range
// loops until the range crosses 70
while ((chances + 4) <= 70) {
// count count of occurrences of values between chance and chance+4
int count = 0;
// inner loop to find the above count
for (int j = chances; j < chances + 5; j++) {
// checking if the first map contains j
if (sortedMap1.containsKey(j)) {
// adding to the count
count += sortedMap1.get(j);
}
}
// finding the percentage
double percentage = (double) count * 100.0 / num1.length;
// printing the percentage with a precision of two decimal points
System.out.printf("Chances %d-%d = %.2f%% ", chances, chances + 4,
percentage);
chances += 5;
}
/**
* performing the same technique for all other arrays
*/
System.out.println("For array2:");
chances = 1;
while ((chances + 4) <= 70) {
int count = 0;
for (int j = chances; j < chances + 5; j++) {
if (sortedMap2.containsKey(j)) {
count += sortedMap2.get(j);
}
}
double percentage = (double) count * 100.0 / num2.length;
System.out.printf("Chances %d-%d = %.2f%% ", chances, chances + 4,
percentage);
chances += 5;
}
System.out.println("For array3:");
chances = 1;
while ((chances + 4) <= 70) {
int count = 0;
for (int j = chances; j < chances + 5; j++) {
if (sortedMap3.containsKey(j)) {
count += sortedMap3.get(j);
}
}
double percentage = (double) count * 100.0 / num3.length;
System.out.printf("Chances %d-%d = %.2f%% ", chances, chances + 4,
percentage);
chances += 5;
}
System.out.println("For array4:");
chances = 1;
while ((chances + 4) <= 70) {
int count = 0;
for (int j = chances; j < chances + 5; j++) {
if (sortedMap4.containsKey(j)) {
count += sortedMap4.get(j);
}
}
double percentage = (double) count * 100.0 / num4.length;
System.out.printf("Chances %d-%d = %.2f%% ", chances, chances + 4,
percentage);
chances += 5;
}
System.out.println("For array5:");
chances = 1;
while ((chances + 4) <= 70) {
int count = 0;
for (int j = chances; j < chances + 5; j++) {
if (sortedMap5.containsKey(j)) {
count += sortedMap5.get(j);
}
}
double percentage = (double) count * 100.0 / num5.length;
System.out.printf("Chances %d-%d = %.2f%% ", chances, chances + 4,
percentage);
chances += 5;
}
System.out.println("For array6:");
chances = 1;
while ((chances + 4) <= 70) {
int count = 0;
for (int j = chances; j < chances + 5; j++) {
if (sortedMap6.containsKey(j)) {
count += sortedMap6.get(j);
}
}
double percentage = (double) count * 100.0 / num6.length;
System.out.printf("Chances %d-%d = %.2f%% ", chances, chances + 4,
percentage);
chances += 5;
}
}
}
class ValueComparator<T1, T2 extends Comparable<T2>> implements Comparator<T1> {
Map<T1, T2> base;
public ValueComparator(Map<T1, T2> base) {
this.base = base;
}
@Override
public int compare(T1 k1, T1 k2) {
if (base.containsKey(k1) && base.containsKey(k2)) {
T2 val1 = base.get(k1);
T2 val2 = base.get(k2);
return val2.compareTo(val1);
}
return -1;
}
}
Explanation / Answer
In your code, there is lot of code, i have concised that and also made the changes as you asked. Please try to write the code in modules always and try to reuse the code which you have written, that way you will have lesser code to maintain and find error. Thanks!
Do let me know in comments, if you have any concerns. PLease upvote, if you like the answer.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.