A Java question. The United States Social Security Administration keeps lists of
ID: 3824328 • Letter: A
Question
A Java question. The United States Social Security Administration keeps lists of the most popular baby names by year and by decade. We have two list:, one from the 1900 to 1909 and one 100 years later from the 2000 to 2009. Each contains the top names for the decade. We want to know what names from the 1900s were still popular in the 2000s.
Write an application called BabyNames1900sAnd2000s to print two columns in alphabetical order. The first column will contain all the boys names for 1900s that were still popular in 2000s. The second column will contain all the girls names for 1900s that were still popular in 2000s. Make the columns 30 characters wide and left justified.
The file was as follow:
------------------------------------------babynames1900s.txt------------------------------------------
------------------------------------------------------------------------------------------------------
-------------------------------------babynames2000s.txt------------------------------------------
------------------------------------------------------------------------------------------------------
Here is the file format
Do not use try / catch here. Just declare that the method throws an exception
For full points, design the program so you only read each file one time.
Males Females Rank Name Numb er Name Numb John 84,591 Mary 161, 508 William 69, 321 Helen 69, 429 James 62, 170 Margaret 57, 921 George 43, 589 Anna. 54, 918 Charles 36, 186 Ruth 51, 011Explanation / Answer
package org.jay.chegg.march27;
import java.io.File;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
enum GENDER {
MALE, FEMALE
}
enum DECADE {
NINETEENTH, TWENTYTH
}
class Baby implements Comparable<Baby> {
private String name;
private GENDER gender;
private DECADE year;
private Long count;
/**
* @param name
* @param gender
* @param year
* @param count
*/
public Baby(String name, GENDER gender, DECADE year, long count) {
this.name = name;
this.gender = gender;
this.year = year;
this.count = count;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the gender
*/
public GENDER getGender() {
return gender;
}
/**
* @param gender
* the gender to set
*/
public void setGender(GENDER gender) {
this.gender = gender;
}
/**
* @return the year
*/
public DECADE getYear() {
return year;
}
/**
* @param year
* the year to set
*/
public void setYear(DECADE year) {
this.year = year;
}
/**
* @return the count
*/
public long getCount() {
return count;
}
/**
* @param count
* the count to set
*/
public void setCount(long count) {
this.count = count;
}
// comparing the baby object on the count
@Override
public int compareTo(Baby o) {
return o.count.compareTo(this.count);
}
@Override
public String toString() {
return this.name+" "+this.count;
}
}
public class BabyNames1900sAnd2000s {
/**
* for reading the file
*
* @return inputfile
*/
public static Scanner readFile(String file) {
Scanner inputFile = null;
try {
inputFile = new Scanner(new File(file)); // accessing the file
} catch (Exception e) {
System.out.println("Difficulties opening the file! " + e);
}
return inputFile;
}
/**
* to get the list of babies by decade
* @param decade
* @param inputFile
* @return
* @throws ParseException
*/
public static List<Baby> getListOfDecade(DECADE decade, Scanner inputFile) throws ParseException {
inputFile.nextLine(); // skipping the first line
inputFile.nextLine(); // skipping the second line
List<Baby> babyList = new ArrayList<>();
while (inputFile.hasNext()) { // looping to the content of the file
String line = inputFile.nextLine();
String[] info = line.split("\s+");
Baby maleBaby = new Baby(info[1], GENDER.MALE, decade,
NumberFormat.getNumberInstance(Locale.US).parse(info[2]).longValue());
Baby femaleBaby = new Baby(info[3], GENDER.FEMALE, decade,
NumberFormat.getNumberInstance(Locale.US).parse(info[2]).longValue());
babyList.add(maleBaby);
babyList.add(femaleBaby);
}
return babyList;
}
public static void main(String[] args) throws Exception {
Scanner inputFile = readFile("D:\babynames1900s.txt"); // calls the readfile method
List<Baby> babyList19th = getListOfDecade(DECADE.NINETEENTH, inputFile);
Scanner inputFile2 = readFile("D:\babynames2000s.txt"); // calls the readfile method
List<Baby> babyList20th = getListOfDecade(DECADE.TWENTYTH, inputFile2);
List<Baby>finalMaleList=new ArrayList<>();
List<Baby>finalFeMaleList=new ArrayList<>();
// looping through the list and adding the common babies to final list
for (Baby baby : babyList19th) {
for (Baby baby2 : babyList20th) {
if (baby.getName().equals(baby2.getName())) {
if (baby.getGender() == GENDER.MALE && baby.getGender() == baby2.getGender()) {
finalMaleList.add(baby);
}else if(baby.getGender() == GENDER.FEMALE && baby.getGender() == baby2.getGender()){
finalFeMaleList.add(baby);
}
}
}
}
Collections.sort(finalMaleList); // sorting the list as per number
Collections.sort(finalFeMaleList); // sorting the list as per number
System.out.println(" Males Females ");
System.out.format("%5s%10s%10s%10s%10s", "Rank", "Name", "Number","Name", "Number");
System.out.println();
String output="";
//printing out the list
for(int i=0;i<finalFeMaleList.size();i++){
System.out.format("%5s%10s%10s%10s%10s", ""+(i+1)+"", ""+finalMaleList.get(i).getName()+"", ""+NumberFormat.getNumberInstance(Locale.US).format(finalMaleList.get(i).getCount())+"",""+finalFeMaleList.get(i).getName()+"", ""+NumberFormat.getNumberInstance(Locale.US).format(finalFeMaleList.get(i).getCount())+"");
System.out.println();
}
System.out.println(output);
}
}
---------------------------------------------------------------------Output--------------------------------------------------------------------------
Males Females
Rank Name Number Name Number
1 John 84,591 Mary 84,591
2 William 69,321 Margaret 62,170
3 James 62,170 Anna 43,589
4 George 43,589 Elizabeth 35,850
5 Charles 36,186 Lillian 17,906
6 Robert 35,850 Grace 12,796
7 Joseph 35,173 Emma 11,651
8 Edward 24,515 Catherine 9,251
9 Thomas 21,785 Evelyn 6,490
10 Henry 21,773 Eva 6,281
11 Paul 12,637 Ruby 6,161
12 Raymond 11,651 Sarah 5,629
13 Richard 10,678 Julia 5,043
14 Jack 9,222 Laura 5,036
15 David 8,623 Ella 4,633
16 Samuel 8,149 Katherine 4,156
17 Andrew 6,281 Lucy 3,961
18 Michael 5,222 Kathryn 2,915
19 Anthony 5,043 Daisy 2,597
20 Daniel 5,036 Sadie 2,531
21 Kenneth 4,579 Emily 2,134
22 Jesse 4,559 Charlotte 2,122
23 Oscar 4,517 Katie 1,821
24 Peter 4,376 Lydia 1,771
25 Benjamin 4,009 Caroline 1,552
26 Edwin 3,961 Sara 1,517
27 Martin 3,346 Maria 1,513
28 Edgar 3,154 Madeline 1,500
29 Victor 2,447 Rebecca 1,468
30 Stephen 2,247 Rachel 1,343
31 Alexander 2,231 Sophie 1,319
32 Jacob 2,191 Naomi 1,291
33 Patrick 2,141 Victoria 1,281
34 Oliver 2,088 Amelia 1,276
35 Vincent 1,954 Isabel 1,131
36 Manuel 1,750 Amanda 1,122
37 Alex 1,745 Audrey 1,049
38 Wesley 1,693 Hannah 988
39 Isaac 1,638 Amy 972
40 Jose 1,628 Isabelle 950
41 Nathan 1,616 Sophia 941
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.