Question

Project 1: Inheritance and Baby Names
In this project you will provide a tool for visualizing popularity of baby names in the years since 1880. Your program will use the data provided by Social Security Administration that contains names given to babies born in US each year. Using this data and a name specified by the user, your program will need to generate a histogram showing the fraction of children who were given that name in each year.

Objectives
The goal of this programming project is for you to master (or at least get practice on) the following tasks:
• working with multi-file programs
• reading data from input files
• working with large data sets
• using the ArrayList class
• writing classes
• working with existing code
• extending existing classes (inheritance)

Dataset
In this project you will be working with open data. Wikipedia has a good description of open data: ”Open data is the idea that some data should be freely available to everyone to use and republish as they wish, without restrictions from copyright, patents or other mechanisms of control.”
The data set that you need can be found at Data.gov1 website.
The file that you download is an archive zip file that contains files with data organized by year. There is also NationalReadMe.pdf file that contains detailed description of how the data is organized. In short, each file contains a long list of lines each in the format:
NAME,GENDER,COUNT
1 Data.gov is managed and hosted by the U.S. General Services Administration, Office of Citizen Services and Innovative Technologies. It hosts close to 200,000 datasets made available by federal, state and local governments as well as other organizations.

Program’s Data
For this program, the data files are considered to be part of the application (not the input provided by the user at runtime).
You should store all of the data files in their own directory called data inside the project directory in Eclipse (this is at the same directory as src directory that stores the actual code, possibly in packages).
This way you can open the files from within your program using the relative path, for example, "data/yob1984.txt".
Within Eclipse the project/folder/package hirearchy should look something like the image on the right.
You can test your setup using the TestFileRead class included at the end of this specification.
If you setup your program and data differently, the graders may not be able to run the code successfully.

User Interface
Your program has to be a console based program (no graphical interface).

Program Usage
The program is started from the command line (or run within an IDE).
It does not use any command line arguments (if any are provided, the program should simply ignore them).

Input and Output
The program should run in a loop that allows the user to check popularity of different names.
On each iteration, the user should be prompted to enter either a name (for which the program computes the results) or the letter ’Q’ or ’q’ to indicate the termination of the program.
The user should not be prompted for any other response.
If the name entered by the user cannot be found in the list of names stored in the dataset (i.e., it does not occur in ANY of the files), the program should print a message
No such name in the dataset.
and continue into the next iteration.
If the name entered by the user has matches in at least one data file, the program should print the histogram showing popularity of this name (see below for the details of formatting).
Once the results are displayed, the program should continue into the next iteration.
Any error messages generated by your code should be written to the System.err stream (not the System.out stream).
Histogram format:
If the name entered by the user is found in at least one data file, a histogram showing the popularity of this name should be displayed.
If a name does not occur in all of the files, the program should set the number of babies who are given this name in a particular year to zero.
For each years data, the program needs to determine the percentage of babies given a particular name number of babies with a given name total number of babies in that year × 100
For each year there should be a line of output matching the following format:
YYYY (F.FFFF): HISTOGRAM_BAR
where
• YYYY is a four digit year,
• F.FFFF is a percentage calculated according to the above formula (it has to be printed with exactly one digit before the decimal
point and four digits after the decimal point3),
• HISTOGRAM_BAR is a visual representation of the percentage; it should consist of a sequence of vertical bars ’|’, one bar for each 0.01 percent (rounded up to the nearest integer); the number of bars can be calculated by
number of babies with a given nametotal number of babies in that year 
where the symbol d . . .e means the ceiling function (in Java you can use Math.ceil() to compute it).
Here are the example lines calculated for ’Joanna’ for three different years:
1880 (0.0129): ||
...
1984 (0.1989): ||||||||||||||||||||
...
2015 (0.0616): ||||||
(Note, that the program should not be printing ..., but rather the information for each year.) There should not be any blank lines between the lines for each year.
A few things to consider:
• For a name that does not occur in a particular data file, the count should be set to zero, which will result in no vertical bars printed.
• For a name that is the most popular in a given year, the number of bars may exceed the width of the display window and (depending on the settings) may wrap to the next line. This is fine and you do not need to modify this behavior.
• Some names may occur in the data file more than once (since they are given to both female and male babies). Your histogram should combine the data for both occurences.
• The program should be case in-sensitive. The name in the data file is always capitalized, for example ’Joanna’. Your program should produce exactly the same results regardless of if the user types ’joanna’, ’JOANNA’, ’JoAnNa’ or any other variation of cases.4
• Some names have several different spellings in common use, for example ’Joanna’, ’Johana’, ’Joannah’. For the purpose of this program these are considered to be completely different names.

Data Sorage and Organization
1) Your need to provide an implementation of several classes that store the data and compute the results when the program is executed.
2) In particular, your program must implement and use the following classes. You may implement additional classes as well, if you wish.
3) You should be using printf for that. If you are not familiar with formatted output in Java, research it or ask questions.
4) You may want to explore the methods of the String class that ignore the case when comparing two objects.

MyArrayList Class
The MyArrayList class is a container class just like the ArrayList<E> class that is part of JavaAPI. In fact, your own implementation must inherit from ArrayList<E> class. The main difference is that your class will be used for working with generic elements whose type implements Comparable<E> interface. Your class will be using a bounded generic type. To achieve this, your class header should look as follows:
public class MyArrayList<E extends Comparable<E>> extends ArrayList<E>
Your own implementation ...
• ... must overload5 the sort method of the ArrayList class. Your own sort method should take no parameters and should not return anything. It should operate on the object on which it is called and sort the elements according to their natural order (the one defined by the compareTo method). This method should make use of Collections.sort() to perform its task.
[Optional: You may try to implement your own sort method (one of the ones you learned about in cs101) to see how the performance of Collections.sort() compares to your own implementation. Do not submit that version of the program. ]
• ... must implement isSorted() method that returns true or false if the elements stored in the collection are sorted or not sorted, respectively. The method should use the natural ordering of the elements, i.e. the compareTo method defined on the elements.
• ... must override6 the contains method implemented in the ArrayList class. The method should determine if the elements stored in the container are sorted, and if so, apply a binary search algorithm. If the elements are not in a sorted order, the method should call the contains implemented in the ArrayList class (this method performs a linear search). You may implement additional methods, if you wish. Note, that MyArrayList is a generic class, i.e., it can store elements of any type (as long as the type is consistent within the container).

Name Class
The Name class stores information about a particular name for a particular year. It should store information about the name itself, the gender and the count (how many babies have been given that name).
This class should provide a three parameter constructor:
public Name ( String name, String gender, int count )
If the constructor is called with an empty string for name, invalid gender indicator (valid values are single charactes ’f’ for female, ’m’ for male in either lower- or uppercase), or a negative value for count, then an instance of IllegalArgumentException should be thrown carrying an appropriate error message. There should be no default constructor.
This class should implement Comparable<Name> interface. The comparison should be done by the name as the primary key (using alphabetical order), by the count as the secondary key (i.e., when two objects that have the same value of name are compared, the comparison should be performed by count) and by the gender as the ternary key. This class should override the equals methods. The two Name objects should be considered equal if the name, count and gender data fields are identical.
The class should override the toString method. The details are up to you, but you should make sure that it returns a String object that is a meaningful representation of the object on which it is called.

YearNames Class
The YearNames class should be used to store all the Name objects for a particular year.
Function overloading refers to ability to define multiple functions with the same name but different signatures.
Function overriding refers to ability to define methods in subclasses that have already been implemented in one of the superclasses. A default constructor is one that can be used without passing any arguments.
The class should contain a data field that stores all of the Name objects for a given year.
You should use your own MyArrayList class for that purpose.
The class needs to provide the one parameter constructor public YearNames ( int year )
The class should implement
• public void add ( Name n )
method that adds a new Name object to the list for a current year. This method should throw an instance of IllegalArgumentException if it is called with a Name object that already exists (i.e., the name argument is equal to an existing element based on the equals method for the Name class).
• public int getCountByName ( String name )
method that returns the number of babies that were given the name specified by the parameter. If there are two Name objects matching the specified name string (one male, one female), the sum of two counts should be returned.
• public double getFractionByName ( String name )
method that returns the fraction of babies that were given the name specified by the parameter (this is the namber of such babies divided by the total number of babies in the data file for the year).
The class should override the toString method. The details are up to you, but you should make sure that it returns a String object that is a meaningful representation of the object on which it is called (it may or may not contain the listing of all of the elements).
You may implement other methods, if you wish. You will need to instantiate one YearNames object for each year in the data set.

BabyNames Class
The BabyNames class is the actual program. This is the class that should contain main method. It is responsible for opening and reading the data files, obtaining user input, performing some data validation and handing all errors that may occur (in particular, it should handle any exceptions thrown by your other classes and terminate gracefully, if need be, with a friendly error message presented to the user).
You may (and probably should) implement other methods in this class to modularize the design.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

package project1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* @author
* @version
*/
public class BabyNames {

    /**
    * main function
    * @param args
    */
    public static void main(String[] args) {
               
       MyArrayList<YearNames> yns = new MyArrayList<>();
       // read all files
       readAllFiles(yns);
      
       char signal = ' ';
       String input;
       Scanner console = new Scanner(System.in);
       do {
            input = console.nextLine();
            if (input.length() == 1) {
                signal = Character.toLowerCase(input.charAt(0));
            } else {
                process(input, yns);
            }
       } while (signal != 'q');
    }...

This is only a preview of the solution. Please use the purchase button to see the entire solution

$48.00

or $1 if you
register a new account!

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats