Electronic handin will be available after 5PM on Thursday.
A recent CNN news story on the rise of Latinos in America reports that the last name of Garcia now ranks in the top 10 of the most common last names in America. What is the most common name with 6 letters? Is it Garcia? What about names of other lengths? What is the most common length for a name?
Write a Java class named NameAnalyzer that contains a main method that reads in data from a text file that contains U.S. Census data for names and their rankings and outputs an analysis of the most common name of length 2, 3, ... etc. and the percentage of names reported of each length to determine which length is most common.
You will be supplied with three files to test your program. Each file will consist of an unknown number of entries, one per line, where each entry consists of a name followed by a space followed by a ranking. For example, here are the first five lines of the file containing male first names:
AARON 77 ABDUL 831 ABE 854 ABEL 485 ABRAHAM 347
The first line indicates that AARON is the 77th most common name in the U.S. A lower ranking means a more common name. So the name with a ranking of 1 is the most common name in the U.S.
Your program should ask the user for the name of the text file, and then read in the data line by line, keeping track of the total number of names read in, the number of names read in of each length, and the most common name of each length. You may assume that the length of a name will not exceed 15 characters. (HINT: You will need to use some arrays here.)
Your program should then output the following information:
For each percentage printed, you should also print out a line of stars who length is proportionally to the percentage (one star per percentage point). See sample output for an example.
You should define the maximum number of characters in a name using a final variable and base your loops and other instructions on this variable, rather than hard-coding the number 15 throughout your program.
Your program should NOT ask the user for the number of data entries in the file. Your program should not read through the file more than once. You may assume the file is properly formatted.
Use the next method of the Scanner class to read each name from the text file and the nextInt method of the Scanner class to read each ranking from the text file. Use the hasNext method of the Scanner class to determine if the file has another name (and therefore another ranking) to be processed.
Write another method called displayStars that displays the stars required for the percentage output (see sample output below). This method should have one parameter which is the percentage value. You should display one star for each percentage point, rounding to the nearest integer. So if the percentage value is 7.6%, you would output 8 stars. Call this method from your main method to display each line of stars, as needed.
Use the following three files to test your program (right click to save to your computer):
malefirst.txt - male first names
femalefirst.txt - female first names
lastname.txt - last names
These files are based on data from the 1990 U.S. Census. Remember, if you use Eclipse, these files should be in the top level of your project folder for this homework. Do not place these in the src folder.
When you run your program, you should display your results as shown in the example below, for the male names:
Input name of data file: malefirst.txt MOST REPORTED NAMES BY LENGTH: Length 1: none Length 2: WM Length 3: JOE Length 4: JOHN Length 5: JAMES Length 6: ROBERT Length 7: MICHAEL Length 8: JONATHAN Length 9: FREDERICK Length 10: KRISTOPHER Length 11: CHRISTOPHER Length 12: none Length 13: none Length 14: none Length 15: none DISTRIBUTION OF NAMES (Total = 1219) Length 1: |0.000% Length 2: |0.492% Length 3: |*****5.414% Length 4: |***************14.684% Length 5: |************************23.954% Length 6: |***************************26.661% Length 7: |******************18.130% Length 8: |*******6.973% Length 9: |***3.281% Length 10: |0.328% Length 11: |0.082% Length 12: |0.000% Length 13: |0.000% Length 14: |0.000% Length 15: |0.000%
Your output should match this formatting as closely as possible for maximum credit.
Your code should be well documented and should demonstrate proper Java formatting style. Be sure to name your variables appropriately and indent properly.
See the course website for instructions on how to hand in your program. Please zip your project folder that is created in Eclipse. This makes it easier for us to grade your work.
Remember that the work you submit must be your own. Also, late hand-ins are not accepted. Please plan ahead and submit early to avoid server overload at the deadline. The deadline is based on the server's clock, not your clock. Please do not email your code to your instructor or course assistant as your official hand-in; these will not be graded.