HW7 Names Application with Solution

Solution

Phase 1 – Building the Name class – Due: Friday, Oct 25 11:59pm for MWF section; Saturday, November 2 11:59pm for TTh section

Phase 2 – The Completed App – Due: Wednesday, Oct 30, 11:59pm for MWF section;
Saturday, November 2 11:59pm for TTh section.

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 Names (Zip) is maintained by the federal government and is located here:

https://www.ssa.gov/OACT/babynames/limits.html

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

You may access these files online from my website located here:

https://cs.stcc.edu/~silvestri/names/

Each line of the text file contains the following info:

name,sex,number

where name is the name, their sex (M/F), and the number born with that name.

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

https://gist.github.com/bytecodeman/ef4e0d2ab9a9beab177ee6426eb213ec

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. 

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 2019 or 2020 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!):

Males:

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

Females:

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

The heart of this application is one ArrayList! 

Name class Specifications (Phase 1 of Project)

You will create a class called Name that contains the instance variables, name, sex, and number. The class will have just 1 constructor that takes 3 parameters to initialize the instance variables.

You should provide getter and setter methods for each instance variable.  Error check the setters.  (i.e sex should only be “M” or “F”, name can’t be empty, number can’t be negative, etc.)

This class must also contain the following methods:

equals – Create a standard equals method.  Use the name and sex fields to check for equality. 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 Name object for that name and add to the number the number of additional people with that name.

compareTo – The Name class must implement the Comparable<Name> 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 Name.  Base your logic on that.

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

A solution to this class will be provided after the due time so you can proceed with the Phase 2 App completion.

NameStats Class Specifications

Maintain 1 ArrayList of Name.  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 method (ok to make it public) that creates and builds the Arraylist.  This list could potentially have 10,000+ names.  Use Collections.sort to sort the ArrayList 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.

Hints on this method:

Don’t use ArrayList contains method to determine the existence of a name in the list.  It just returns a boolean.  Use the indexOf method.  It returns an index of where the name in the list or -1 if it isn’t in the list.

Also understand that the get method of ArrayList returns the reference of the name not a copy.  So you can update the number immediately (through getters and setters) in place.

Some public methods:

A public method called getTopMaleNames() that returns an ArrayList of males that is as large as “Number of Top Names desired”.

Another public method called getTopFemaleNames() that returns an ArrayList of females that is as larges as “Number of Top Names desired”.