CSC-220 HW5 Baby Names Application with Solution

Solution that includes Nam’s Suggestion

Due: Monday, Oct 29 11:59pm

This assignment is just way too cool.  It exercises many of the topics we discussed his semester; not to mention, it is also a useful utility.  Start coding now!  This is not the type and homework you can finish two hours before the deadline.

Warning! Do not create a GUI if the basic operation of the classes described below hasn’t been properly developed.  Don’t waste time on a GUI that doesn’t work.  You will not get extra credit for a dysfunctional GUI.  If you code the classes properly, and they work in a command line app, then you should be able to plug and play these classes in a GUI that calls their public methods.  That will give you extra credit.

The following database of Baby Names (Zip) is maintained by the federal government and is located here:

It contains text files of names from 1880 to 2017.  Each file is a CSV file capable of being imported into any popular spread sheet.

To use this data, download, unzip the file, and place the contents in a package called babynames in your project.  Alternatively, you may access these same files online from my website located here:

Each line of the text file contains the following info:


where name is the baby’s name, their sex (M/F), and the number of babies born with that name.

Since this is a comma separated value (CSV) file, use the following sample program to read a data file:

If you choose not to read the files directly from the server, then copy the data files into your project.  Make a package in the project to hold these files.  It’s a fairly hefty database by our standards, 23MB!

Your application will use our application template to prompt for start and ending years, as well as an integer representing the top number of names to display.  The app will read data from those years and display the top number of names of name, sex, and the total number of babies born with that name in the specified range.  A JavaFX program would be even better if you know the technology.

Error check these inputs.  Year input allow for only integers and must be positive.  The start year must be ≤ to the end year.  Years input must map to a datafile.  For example, you can’t enter a 1879 year as there is no such file.  You can’t enter 2018 as that year hasn’t happened or finished yet.  Common sense on the inputs!

For instance, you enter 2010, 2015, 3.  What the app displays is (Data is totally contrived!):


  1. Tony, 23456
  2. Bob, 22123
  3. John, 20123


  1. Mary, 23453
  2. Joan, 21345
  3. Amelia, 19456

The heart of this application is an ArrayList!  Actually two, one for males and females.

BabyName class Specifications

You will create a class called BabyName that contains the instance variables, name, and number.  This class must contain the following methods:

equals – Create a standard equals method.  It is this equals method that the ArrayList uses to determine if a name is already in the list. Remember NO duplicates!  If a name is already in the list, you will simply access the BabyName object for that name and add to the number the number of additional babies with that name.

compareTo – The BabyName class must implement the Comparable Interface.  It is the comparable interface method compareTo that you will use to sort the ArrayLists in descending order.  Look at the number field in the BabyName.  Base your logic on that first.  If you are comparing two BabyNames with the same numbers, then use the Name to decide which goes first, alphabetical order.

toString – You know every class that you intend to print out needs one.  Make sure you output the name and number for the baby name.

BabyNameStats Class Specifications

Two private ArrayLists of BabyName.  One for males and one for females.  Have the constructor null out these fields.

A public Constructor that takes 3 int values:

  • Start Year
  • End Year
  • Number of Top Names desired.

and saves these values as fields.  Error checking these values would be appropriate.  If bad values were passed, throw an exception.

A private method that creates and builds the male and female array lists.  These lists could potentially have 10,000+ names.  Use Collections.sort to sort the ArrayLists by the number people who have a particular name.  If your names are appearing in ascending order, small numbers first, then you can use the Collections.reverse method to rearrange the list.

A public method called getTopMaleNames() that returns an arraylist of males that is as larges as “Number of Top Names desired”.  This method will call your private method if the property that holds the male arraylist is null.  If it isn’t null, then you’ve already went through the logic to generate the top male names and their counts so don’t do it again.  Just return an array list of the top names.  It is ok and efficient to also have your logic build the female array list since the probability of getting and top females is almost certain.

Another public method called getTopFemaleNames() that returns an arraylist of females that is as larges as “Number of Top Names desired”. Same basic steps as described for getTopFemaleNames().