1) Create two JAVA linked hash sets {\"George\", \"Jim\", \"Blake\", \"Kevin\",
ID: 640286 • Letter: 1
Question
1) Create two JAVA linked hash sets {"George", "Jim", "Blake", "Kevin", "Michael"} and {"George", "Katie", "Kevin", "Michelle", "Ryan"} and find their union, intersection, and difference. Note that you can clone the sets to preserve the original sets from being changed by these methods.
2)Write a program that reads an unspecified number of integers from a file and finds the integer that has the most occurrences. If several numbers have the most occurrences, all of them should be reported.
3) Use these data files to write a program that enables the determination of the ranking of popularity of a name if the year, gender, and name are known. To achieve the best efficiency, create two arrays for boy
Explanation / Answer
1)
import java.util.*;
public class Problem1 {
public static void main(String[] args) {
//Create hash set
Set<String> set1 = new HashSet<String>();
//Add strings to the set1
set1.add("George");
set1.add("Jim");
set1.add("John");
set1.add("Blake");
set1.add("Kevin");
set1.add("Michael");
//Create set1 clones
HashSet<String> set1Copy1 = new HashSet<String>(set1);
HashSet<String> set1Copy2 = new HashSet<String>(set1);
HashSet<String> set1Copy3 = new HashSet<String>(set1);
//Create hash set
Set<String> set2 = new HashSet<String>();
//Add strings to the set2
set2.add("George");
set2.add("Katie");
set2.add("Kevin");
set2.add("Michelle");
set2.add("Ryan");
System.out.print("set1 = " + set1);
System.out.println("set2 = " + set2);
set1Copy1.addAll(set2);
set1Copy2.removeAll(set2);
set1Copy3.retainAll(set2);
System.out.println("Union = " + set1Copy1);
System.out.println("Difference = " + set1Copy2);
System.out.println("Intersection = " + set1Copy3);
System.out.println("set1 = " + set1);
System.out.println("set2 = " + set2);
}
}
2)
public int getPopularElement(int[] a)
{
int count = 1, tempCount;
int popular = a[0];
int temp = 0;
for (int i = 0; i < (a.length - 1); i++)
{
temp = a[i];
tempCount = 0;
for (int j = 1; j < a.length; j++)
{
if (temp == a[j])
tempCount++;
}
if (tempCount > count)
{
popular = temp;
count = tempCount;
}
}
return popular;
}
3)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.