Question

In: Computer Science

in JAVA, Hash table The goal is to count the number of common elements between two...

in JAVA, Hash table

The goal is to count the number of common elements between two sets. Download the following data sets, and add them to your project:

  • girlNames2016.txt
  • boyNames2016.txt

These files contain lists of the 1,000 most popular boy and girl names in the US for 2016, as compiled by the Social Security Administration. Each line of the file consists of a first name, and the number of registered births that year using that name.

Your task is to determine which names appeared on BOTH the boys list and girls list. We want a list of the most popular baby names that could be used for either a girl or a boy (ex. Alex or Drew).

Create a Driver class, and in the main method, use the following algorithm:

  1. Read each girl name as a String, ignoring the number of namings, and add it to a set girlNames.
  2. Read each boy name as a String, ignoring the number of namings, and add it to a set boyNames.
  3. Calculate the intersection of these sets and place the result into a new set unisexNames.
    1. Build this set by iterating through one set, and checking whether the other set contains that element. If so, this element belongs to both sets and should be added to unisexNames.
  4. Print all elements in unisexNames.

girlName2016.txt:

Emma 19414
Olivia 19246
Ava 16237
Sophia 16070
Isabella 14722
Mia 14366
Charlotte 13030
Abigail 11699
Emily 10926
Harper 10733
Amelia 10702
Evelyn 10060
Elizabeth 9493
Sofia 9134
Madison 8982
Avery 8733
Ella 7866
Scarlett 7680
Grace 7531
Chloe 7410
Victoria 7267
Riley 7110
Aria 6904
Lily 6558
Aubrey 6507
Zoey 6414
Penelope 6367
Lillian 6333
Addison 6295
Layla 6234
Natalie 6156
Camila 6036
Hannah 5976
Brooklyn 5922
Zoe 5706
Nora 5561
Leah 5380
Savannah 5336
Audrey 5330
Claire 5190
Eleanor 5100
Skylar 5099
Ellie 5045
Samantha 4924
Stella 4912
Paisley 4839
Violet 4837
Mila 4802
Allison 4762
Alexa 4760

boyName2016.txt

Noah 19015
Liam 18138
William 15668
Mason 15192
James 14776
Benjamin 14569
Jacob 14416
Michael 13998
Elijah 13764
Ethan 13758
Alexander 13321
Oliver 12975
Daniel 12839
Lucas 12838
Matthew 12551
Aiden 12158
Jackson 11210
Logan 11200
David 11028
Joseph 10823
Samuel 10492
Henry 10323
Owen 10249
Sebastian 10249
Gabriel 10142
Carter 10134
Jayden 10063
John 9952
Luke 9930
Anthony 9815
Isaac 9671
Dylan 9657
Wyatt 9514
Andrew 9324
Joshua 9138
Christopher 9020
Grayson 8622
Jack 8367
Julian 8333
Ryan 8215
Jaxon 8054
Levi 8009
Nathan 7933
Caleb 7914
Hunter 7613
Christian 7584
Isaiah 7454
Thomas 7309
Aaron 7118
Lincoln 7076

Solutions

Expert Solution

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 or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Driver.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.HashSet;

import java.util.Scanner;

public class Driver {

      public static void main(String[] args) throws FileNotFoundException {

            // creating three sets to store girl names, boy names and common names

            HashSet<String> girlNames = new HashSet<String>();

            HashSet<String> boyNames = new HashSet<String>();

            HashSet<String> uniSexualNames = new HashSet<String>();

            // opening girlName206.txt file. Make sure that the file exists in the

            // root directory of your project and has the same name, or else this

            // will cause an Exception, and that is not a problem with the code.

            Scanner scanner = new Scanner(new File("girlName2016.txt"));

            // looping and reading each record from the file

            while (scanner.hasNext()) {

                  // reading name and number

                  String name = scanner.next();

                  int num = scanner.nextInt();

                  // ignoring num, adding name converted to upper case to girlNames

                  // set

                  girlNames.add(name.toUpperCase());

            }

            // closing file

            scanner.close();

            // opening next file, doing the same thing for boy names

            scanner = new Scanner(new File("boyName2016.txt"));

            while (scanner.hasNext()) {

                  String name = scanner.next();

                  int num = scanner.nextInt();

                  boyNames.add(name.toUpperCase());

            }

            scanner.close();

            // now looping through each name in girlNames

            for (String name : girlNames) {

                  // checking if boyNames contain this name

                  if (boyNames.contains(name)) {

                        // adding to uniSexualNames

                        uniSexualNames.add(name);

                  }

            }

            System.out.println("Unisexual names found:");

            // looping and displaying all the unisexual names found

            for (String name : uniSexualNames) {

                  // capitalizing the string so that the first letter will be upper

                  // case and all remaining letters will be in lower case.

                  String capitalized = name.charAt(0)

                              + name.substring(1).toLowerCase();

                  System.out.println(capitalized);

            }

      }

}


Related Solutions

Java Question 5: Count elements in the heap Write a function that returns the number of...
Java Question 5: Count elements in the heap Write a function that returns the number of elements in a min heap strictly less than a given number. Method signature: public static int elemNumHeap(PriorityQueue minHeap, int val) Please also include testers.
Write a program to insert the following elements into a hash table of size 17. The...
Write a program to insert the following elements into a hash table of size 17. The hash function is X mod 17 where X is the input element.   6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45 Use linear probing to resolve any collisions.
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
An ACCOUNTING RATIO is a number that shows the relationship between two elements on a firm’s...
An ACCOUNTING RATIO is a number that shows the relationship between two elements on a firm’s financial statements. Like the individual elements on the financial statements, these ratios can be compared with those of competitors, with industry averages, and with the firm’s ratios from previous accounting periods. Since there are many accounts shown on the business’s financial statements, there can be many ratios. The four most popular types of ACCOUNTING RATIOS are LIQUIDITY RATIOS, PROFITABILITY RATIOS, ACTIVITY RATIOS and LEVERAGE...
An ACCOUNTING RATIO is a number that shows the relationship between two elements on a firm’s...
An ACCOUNTING RATIO is a number that shows the relationship between two elements on a firm’s financial statements. Like the individual elements on the financial statements, these ratios can be compared with those of competitors, with industry averages, and with the firm’s ratios from previous accounting periods. Since there are many accounts shown on the business’s financial statements, there can be many ratios. The four most popular types of ACCOUNTING RATIOS are LIQUIDITY RATIOS, PROFITABILITY RATIOS, ACTIVITY RATIOS and LEVERAGE...
Java: The goal is to find the number of unique words found in a story file...
Java: The goal is to find the number of unique words found in a story file text.txt but there some conditions each such word must be found in the dictionary WordList.txt file, each such word should not be among the commonly used ones such as a, it, etc. Such forbidden words are listed in a separate file called stopwords.txt. Your output will be the single number which is the number of unique words in the story file. Use only ArrayLists...
Java: The goal is to find the number of unique words found in a story file...
Java: The goal is to find the number of unique words found in a story file text.txt but there some conditions 1. each such word must be found in the dictionary WordList.txt file, 2. each such word should not be among the commonly used ones such as a, it, etc. Such forbidden words are listed in a separate file called stopwords.txt. Output will be the single number which is the number of unique words in the story file. Use only...
Implement a Cuckoo hash table in which two separate tables are maintained. Each table should have...
Implement a Cuckoo hash table in which two separate tables are maintained. Each table should have a fixed size of 13. Use the hash function h1(x) = x mod 13 for the first table, and h2(x)= 11 – (x mod 11) for the second table. Your program should read an input file, input.txt, consisting of one input per line, insert the input to the table in order, and print out the final content of the hash table in the provided...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
goal Find the average of the elements in the list (list could have any number of...
goal Find the average of the elements in the list (list could have any number of elements). If the average is a decimal number, return only the integer part. Example: if average=10.8, return 10 Example: if list={10, 20, 30, 40, 50}, the method should return: 30 import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class LinkedList {// a Singly Linked List    Node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT