**Java language only** Directions:the code below tells me the first second and t
ID: 3742004 • Letter: #
Question
**Java language only**
Directions:the code below tells me the first second and third numbers which are most relevant in the array, Add to the code below making it tell me how many times the first, second and third most relevant numbers came up in the code Example below.
I Will thumbs up good work thabk you!!
/*
EXAMPLE:
EX: Array=[9],[3],[3],[4],[3],[4],[3],[4],[58],[8],[9]
EX: Array=[21],[21],[25],[25],[25],[2],[25],[2],[2],[]
EX: Array=[60],[60],[15],[5],[5],[5],[5],[60],[8],[8]
EX: Array=[90],[4],[90],[4],[25],[25],[4],[25],[4],[4]
EX: Array=[50],[50],[50],[60],[60],[60],[60],[12],[12]
EX: Array=[20],[20],[5],[3],[8],[8],[8],[8],[5],[5]
OutPut Example:
First relevant number:Output:3,25,5,4,60,8
"most relevant numbers came up"(3 came up 4 times)(25 came up 4 times)(5 came up 4 times)(4 came up 5 times)(60 came up 4 times)(8 came up 4 times)
Second relevant number:Output:4,2,60,25,50,5
"Second most relevant numbers came up"(4 came up 4 times)(2 came up 3 times)(60 came up 3 times)(25 came up 3 times)(50 came up 3 times)(5 came up 3 times)
Third relevant number:9,21,8,90,12,20
"Third most relevant numbers"(9 came up 2 times)(21 came up 2 times)(8 came up 2 times)(90 came up 2 times)(12 came up 2 times)(20 came up 2 times)
*/
package chegg;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Chegg {
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[] = {3, 1,14,1,11,1,2,9,12,1,1,1,40,21,1,2,13,2,17,2,11,+
1,1,14,22,5,2,11,16,9,21,14,8,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,3,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,25,1,5,9,18,26,4,11,4,2};
int num2[] = {20,6,16,31,26,03,2,9,12,1,1,1,40,21,1,2,13,2,17,2,11,+
1,1,14,22,5,2,11,16,9,21,14,8,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,3,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,25,1,5,9,18,26,4,11,4,2,};
int num3[] = {60,60,15,5,5,5,5,60,8,8};
int num4[] = {90,4,90,4,25,25,4,25,4,};
int num5[] = {50,50,50,60,60,60,60,12,12};
int num6[] = {20,20,5,3,8,8,8,8,5,5};
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:");
System.out.print(sortedMap1.keySet().toArray()[i] + ",");
System.out.print(sortedMap2.keySet().toArray()[i] + ",");
System.out.print(sortedMap3.keySet().toArray()[i] +",");
System.out.print(sortedMap4.keySet().toArray()[i] + ",");
System.out.print(sortedMap5.keySet().toArray()[i] +",");
System.out.print(sortedMap6.keySet().toArray()[i] + ",");
System.out.println();
}
if(i==1){
System.out.print("Second Relevance Number: output:");
System.out.print(sortedMap1.keySet().toArray()[i] + ",");
System.out.print(sortedMap2.keySet().toArray()[i] + ",");
System.out.print(sortedMap3.keySet().toArray()[i] +",");
System.out.print(sortedMap4.keySet().toArray()[i] + ",");
System.out.print(sortedMap5.keySet().toArray()[i] +",");
System.out.print(sortedMap6.keySet().toArray()[i] + ",");
System.out.println();
}
if(i==2){
System.out.print("Third Relevance Number: output:");
System.out.print(sortedMap1.keySet().toArray()[i] + ",");
System.out.print(sortedMap2.keySet().toArray()[i] + ",");
System.out.print(sortedMap3.keySet().toArray()[i] +",");
System.out.print(sortedMap4.keySet().toArray()[i] + ",");
System.out.print(sortedMap5.keySet().toArray()[i] +",");
System.out.print(sortedMap6.keySet().toArray()[i] + ",");
System.out.println();
}
}
}
}
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) {
T2 val1 = base.get(k1);
T2 val2 = base.get(k2);
return val2.compareTo(val1);
}
}
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Chegg.java
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Chegg {
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[] = { 3, 1, 14, 1, 11, 1, 2, 9, 12, 1, 1, 1, 40, 21, 1, 2, 13,
2, 17, 2, 11, +1, 1, 14, 22, 5, 2, 11, 16, 9, 21, 14, 8, 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, 3, 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, 25, 1, 5, 9, 18, 26, 4, 11, 4, 2 };
int num2[] = { 20, 6, 16, 31, 26, 03, 2, 9, 12, 1, 1, 1, 40, 21, 1, 2,
13, 2, 17, 2, 11, +1, 1, 14, 22, 5, 2, 11, 16, 9, 21, 14, 8, 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, 3, 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, 25, 1, 5, 9, 18, 26, 4, 11, 4, 2, };
int num3[] = { 60, 60, 15, 5, 5, 5, 5, 60, 8, 8 };
int num4[] = { 90, 4, 90, 4, 25, 25, 4, 25, 4, };
int num5[] = { 50, 50, 50, 60, 60, 60, 60, 12, 12 };
int num6[] = { 20, 20, 5, 3, 8, 8, 8, 8, 5, 5 };
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();
}
}
}
}
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) {
T2 val1 = base.get(k1);
T2 val2 = base.get(k2);
return val2.compareTo(val1);
}
}
/*OUTPUT*/
First Relevance Number: output:1,1,5,4,60,8
Most relevant numbers came up (1 came up 18 times) (1 came up 15 times) (5 came up 4 times) (4 came up 4 times) (60 came up 4 times) (8 came up 4 times)
Second Relevance Number: output:2,2,60,25,50,5
Second most relevant numbers came up (2 came up 10 times) (2 came up 10 times) (60 came up 3 times) (25 came up 3 times) (50 came up 3 times) (5 came up 3 times)
Third Relevance Number: output:4,4,8,90,12,20
Third most relevant numbers came up (4 came up 7 times) (4 came up 7 times) (8 came up 2 times) (90 came up 2 times) (12 came up 2 times) (20 came up 2 times)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.