In: Computer Science
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:
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:
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 |
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);
}
}
}