Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a Java program that allows searches on name popularity by birth year (from 1880-2016). Here is a sample run of the program (user input

write a Java program that allows searches on name popularity by birth year (from 1880-2016). Here is a sample run of the program (user input is in red):

Enter a birth year (1880-1996): 1996

Enter (n)ame, (r)ank, (t)op, or (q)uit: n Enter name: Taylor Taylor is ranked 6 for females Taylor is ranked 82 for males

Enter (n)ame, (r)ank, (t)op, or (q)uit: n Enter name: Abc Abc is not ranked for females Abc is not ranked for males

Enter (n)ame, (r)ank, (t)op, or (q)uit: r Enter rank: 5 Samantha is the #5 female name Joshua is the #5 male name

Enter (n)ame, (r)ank, (t)op, or (q)uit: T Enter size of top list (1-20): 4

Top 4 female names: (1) Emily (2) Jessica (3) Ashley (4) Sarah

Top 4 male names: (1) Michael (2) Matthew (3) Jacob (4) Christopher

Enter (n)ame, (r)ank, (t)op, or (q)uit: q Input Files

The input files for this project area available from the Social Security Administration (Lhttps://www.ssa.gov/oact/babynames/limits.html)Links to an external site.. Follow the link, and then click "National data". This will download the file names.zip, which contains a separate text file of names for each birth year between 1880 and 2016. Extract these files so that the names folder is in your Java working directory. (If you use the console, the names folder should be in the same location as your .java file. If you use Eclipse, the names folder should be just inside your Eclipse project folder for this project.)

The input files are named yob1880.txt, yob1881.txt, ..., yob2016.txt. If the user enters 1995 for the birth year, for example, you will open input file "names\yob1995.txt". (The "names\" in front will first open the names folder to get to the input file. Hint: in a String, you need to put \\ when you really want just \.) Think about how you can concatenate text with the input year to get the name of the needed input file.

Each input file has multiple lines, one for each first name from that birth year. If a name is used for both males and females, then it will be included twice. Each line has this format:

Mary,F,7065

First the name, then the sex (M or F), then the number of occurrences (how many babies born that year were given that name). All the female names are listed first, followed by all the male names. The names are ordered by popularity, so the most popular female name of that birth year is first, then the second-most-popular, etc. (The male names are similarly ordered.) There are multiple ties in popularity, but you don't need to handle this -- just assume that the name listed first is more popular.

When you read the input file, you should create an array of female names and an array of male names. You should store the names in the same order that they are listed in the file, so that you will know the order of popularity. You do not need to store the sex (since you will be separated the names into two arrays) or the number of occurrences.

Running Your Program

This program should contain a single class (called Proj5) with a main method and several other methods which will be described below. Your program must compile (by command-line) with the statement:

javac Proj5.java It must then run with the command:

java Proj5

Program Design

Your program should have the following global variables (declared before any method):

public static String[] mnames; //will hold the male names in order of popularity public static String[] fnames; //will hold the female names in order of popularity public static Scanner s; //for user input (initialize in main) You should NOT use an ArrayList to hold the names at any point. I realize that this would make things easier, but I would like to have more practice working with standard arrays. We will use ArrayLists later in the semester.

Your program should include the following methods:

public static int numNames(String filename, char sex) - opens the input file filename and returns a count of how many names have the given sex (M or F). It should close the input file when done. public static void readNamesData(String filename) - gets a count of how many male and female names are in the input file filename, and allocates that amount of space for mnames and fnames. Then, it opens the input file filename and reads each name in order into mnames and fnames (separating by sex). It should close the input file when done.

public static int getRank(String name, char sex) - returns the popularity rank of name, where 1 is the rank of the most popular name (remember that mnames and fnames are ordered by popularity). If sex is 'M', look in mnames. If it is 'F', look in fnames.

public static void nameOption() - This method should include the code for the 'n' (name) option in the control loop. In this method, assume that the user has ALREADY selected 'n'. Include the code and method calls for getting an input name from the user and printing its rank for both males and females. This method should call the getRank method.

public static void rankOption() - This method should include the code for the 'r' (rank) option in the control loop. In this method, assume that the user has ALREADY selected 'r'. Include the code for getting an input rank (1 and up) from the user, and printing the male and female names with that rank.

public static void topOption() - This method should include the code for the 't' (top) option in the control loop. In this method, assume that the user has ALREADY selected 't'. Include the code for getting a size for the top names list from the user (between 1-20), and printing the top female names and top male names of that size.

public static void main(String[] args) - This method should ask the user for the birth year and create a String that is the corresponding filename. It should call readNamesData to read in the input file. Then, include a do-while loop to repeatedly get a menu option from the user. In the cases of 'n', 'r', or 't', you should call your nameOption, rankOption, or topOption methods.

Error Handling

Your program should handle the following errors:

If the user enters a birth year that is not between 1880 and 2016, print a descriptive error. If the user enters an invalid menu option, print a descriptive error and prompt for another menu option. If the user searches for a name, and that name is not in fnames, print that that name is not ranked for females. Similarly handle the case if the name is not in mnames. If the user searches for a rank that is either less than one or bigger than the size of either name array, print an error that the rank is invalid. If the user chooses the "top" option and enters a size that is not between 1-20, print an error that the size is invalid. You may assume that each input file has the format described in "Input Files" above. You do not need to handle potential errors in the input files.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

74 Motivation concepts and applications.

Answered: 1 week ago

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago