**Java Language only** **Please let it be copy pastable to netbeans** I will thu
ID: 3741808 • Letter: #
Question
**Java Language only**
**Please let it be copy pastable to netbeans**
I will thumbs up good work thank you!!!
Write a java code that has 6 sections too it each section will hold int arrays that will show the first, second and third relevant single array numbers in each section. The Output should show all 6 numbers in order from relevant array numbers, too second relevant array numbers, to third realvent array numbers for all 6 sections.
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],[]
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
Second relevant number:Output:4,2,60,25,50,5
Third relevant number:9,21,8,90,12,20
Explanation / Answer
Hi, Copy the below code in RelevantNumbers.java file and run it. any Queries please comment.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
class Section {
List<Map.Entry<String, Integer>> list= new ArrayList();
public Section(String sectionValue) {
Map<String, Integer> numbersAndRepetationCount = new HashMap();
String[] numbers = sectionValue.split(",");
for (int i = 0; i < numbers.length; i++) {
// check if the key existing in the map then read count and increment by one,
// other wise just add as new key with value as 1
if (numbersAndRepetationCount.containsKey(numbers[i])) {
int count = numbersAndRepetationCount.get(numbers[i]);
numbersAndRepetationCount.put(numbers[i], count + 1);
} else {
numbersAndRepetationCount.put(numbers[i], 1);
}
}
// these steps to arrange the elements in descending order based on their repeatation
list = new ArrayList(numbersAndRepetationCount.entrySet());
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o2)).getValue())
.compareTo(((Map.Entry) (o1)).getValue());
}
});
}
public String getNthHigheshNumber(int n){
return list.get(n).getKey();
}
}
public class RelevantNumbers {
public static void main(String []arg) {
// this statement is used to read the input from the user
Scanner scanner = new Scanner(System.in);
List<Section> sectionsList = new ArrayList();
System.out.println("1 .Enter First Section of elements, seperated by ,");
String inputValue = scanner.nextLine();
sectionsList.add(new Section(inputValue));
System.out.println("2. Enter Second Section of elements, seperated by ,");
inputValue = scanner.nextLine();
sectionsList.add(new Section(inputValue));
System.out.println("3, Enter Third Section of elements, seperated by ,");
inputValue = scanner.nextLine();
sectionsList.add(new Section(inputValue));
System.out.println("4. Enter Fourth Section of elements, seperated by ,");
inputValue = scanner.nextLine();
sectionsList.add(new Section(inputValue));
System.out.println("5. Enter Fifth Section of elements, seperated by ,");
inputValue = scanner.nextLine();
sectionsList.add(new Section(inputValue));
System.out.println("6. Enter Sixth Section of elements, seperated by ,");
inputValue = scanner.nextLine();
sectionsList.add(new Section(inputValue));
// final step to print the first , second and Third relevant numbers from 6 sections
System.out.println("First Relevant Number:" + sectionsList.get(0).getNthHigheshNumber(0)
+" , "+sectionsList.get(1).getNthHigheshNumber(0)
+" , "+sectionsList.get(2).getNthHigheshNumber(0)
+" , "+sectionsList.get(3).getNthHigheshNumber(0)
+" , "+sectionsList.get(4).getNthHigheshNumber(0)
+" , "+sectionsList.get(5).getNthHigheshNumber(0));
System.out.println("Second Relevant Number:" + sectionsList.get(0).getNthHigheshNumber(1)
+" , "+sectionsList.get(1).getNthHigheshNumber(1)
+" , "+sectionsList.get(2).getNthHigheshNumber(1)
+" , "+sectionsList.get(3).getNthHigheshNumber(1)
+" , "+sectionsList.get(4).getNthHigheshNumber(1)
+" , "+sectionsList.get(5).getNthHigheshNumber(1));
System.out.println("Third Relevant Number:" + sectionsList.get(0).getNthHigheshNumber(2)
+" , "+sectionsList.get(1).getNthHigheshNumber(2)
+" , "+sectionsList.get(2).getNthHigheshNumber(2)
+" , "+sectionsList.get(3).getNthHigheshNumber(2)
+" , "+sectionsList.get(4).getNthHigheshNumber(2)
+" , "+sectionsList.get(5).getNthHigheshNumber(2));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.