QuestionQuestion

Background Information

The popularity of human names varies quite a bit over time. In the United States, the Social Security Administration (SSA) maintains statistics on names. In this project, you’ll be writing software that reads a file of SSA data containing the historical popularity of several thousand first names. Based on this information, the user can see popularity trends for specific names, or see the top names during a specific census.

The provided file, named ProjectData.txt, contains several thousand lines formatted like this:

Edwardo 0 0 0 0 0 889 762 803 964 0 0

Each line begins with a first name. The name is followed by 11 integers, separated by spaces. These integers indicate the rank of that name among names of the same gender, during the census years 1900, 1910, 1920, and so on, up to 2000. In general, a lower rank indicates that the name was more popular during that census. However, a rank of 0 indicates that the name was not popular enough to be ranked.

For example, the line above tells us that

• Edwardo was not popular enough to be ranked in 1900, 1910, 1920, 1930, 1940, 1990, and 2000.
• Edwardo was ranked 889th among male names in 1950, 762nd among male names in 1960, 803rd among male names in 1970, and 964th among male names in 1980.

The Assignment

Write a program that reads the provided SSA data file and supports the following features:

• The user should be able to look up a specific name and see a bar graph (produced via text characters) of that name’s popularity from 1900-2010. The height of each bar should be scaled according to the name’s rank during each census, with lower rank resulting in a higher bar. Each bar should also indicate the name’s numerical rank during that census.

If the name entered by the user does not exist in the data, show an appropriate error message.

The next page contains two examples of what this bar graph might look like. Your formatting does not need to match this exactly, as long as the same information is conveyed.
Historical popularity of Erica:
(Ranks shown above each bar, 0 indicates not popular enough to be ranked)

                                                      
                                     62   36   57   
                                    xxxx xxxx xxxx 105
                                    xxxx xxxx xxxx xxxx
                                    xxxx xxxx xxxx xxxx
                                    xxxx xxxx xxxx xxxx
                               397 xxxx xxxx xxxx xxxx
                               xxxx xxxx xxxx xxxx xxxx
                               xxxx xxxx xxxx xxxx xxxx
                               xxxx xxxx xxxx xxxx xxxx
                               xxxx xxxx xxxx xxxx xxxx
                               xxxx xxxx xxxx xxxx xxxx
                           806 xxxx xxxx xxxx xxxx xxxx
                         xxxx xxxx xxxx xxxx xxxx xxxx
                         xxxx xxxx xxxx xxxx xxxx xxxx
    0    0    0    0    0 xxxx xxxx xxxx xxxx xxxx xxxx
+----+----+----+----+----+----+----+----+----+----+----+
1900 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000


Historical popularity of Horace:
(Ranks shown above each bar, 0 indicates not popular enough to be ranked)

                                                      
                                                      
133 128 143                                       
xxxx xxxx xxxx 194 229                              
xxxx xxxx xxxx xxxx xxxx                              
xxxx xxxx xxxx xxxx xxxx 301                        
xxxx xxxx xxxx xxxx xxxx xxxx                        
xxxx xxxx xxxx xxxx xxxx xxxx 471                  
xxxx xxxx xxxx xxxx xxxx xxxx xxxx                  
xxxx xxxx xxxx xxxx xxxx xxxx xxxx                  
xxxx xxxx xxxx xxxx xxxx xxxx xxxx 670               
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx               
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx               
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx 849         
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx         
xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx    0    0
+----+----+----+----+----+----+----+----+----+----+----+
1900 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000


• The user should be able to change the height of the bar graph above. The example graphs above are scaled to a maximum height of 15 lines of text, but the user should be able to specify any height from 10-30 lines. Include error checking to catch invalid input.

• The user should be able to request a list of the top n names from any of the census years in the data (1900, 1910, 1920, … , 2000). The program should show a list of the names with rank ≤ n from that census, sorted by ascending rank. Note that since both male and female names are included in the file, there may be more than one name per rank! Your program should list all names of the same rank. Include error checking to ensure that the census year is valid and that n is between 1-1000.

I will demonstrate a working version of the project in class, but yours does not have to match mine exactly as long as it supports the features above. Be sure to include error checking on all user inputs in your program.

Code Organization

Your program should use methods to break the code down into manageable chunks. At minimum, include the following methods (although you are welcome to add more if you want):

• A method that handles reading the data file.
• A method that prints a bar graph showing the historical popularity of a specific name from 1900-2010. This method should include parameters for name and height of the graph.
• A method that shows the top n names from a particular census year. This method should include parameters for year and n.

Notes on File Reading

There are many ways to read a file in Java. One of the easiest is to use the Scanner class that you’re already familiar with. Instead of having the Scanner object read from System.in, we’ll simply tell it to read from a file.

To read the data file for this project, start by placing the data file in your Eclipse project folder. It should go into the root of the folder, outside the bin and src subfolders. Once the file is there, here’s some code that will get you started with reading it from your program:

Scanner s = null;
try {
s = new Scanner(new FileReader("ProjectData.txt"));
} catch (FileNotFoundException e) {
System.exit(1); // Exit program if file not found
}

while (s.hasNext()) { // Keep reading until nothing left to read
String name = s.next(); // Read the first word (the name)
int rank1 = s.nextInt(); // Read the next integer (rank in 1900)
int rank2 = s.nextInt(); // Read the second integer (rank in 1910)
// etc.
}

You will also need to add import java.io.*; to your import statements at the top of the source code file.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.io.*;
import java.util.*;

public class NameDataProgram {

// Method to read data from file
public static HashMap<String,ArrayList<Integer>> readData(String filename) {
    HashMap<String,ArrayList<Integer>> data = new HashMap<String,ArrayList<Integer>>();
    try(BufferedReader br = new BufferedReader(new FileReader(filename))) {
      for(String line; (line = br.readLine()) != null; ) {
       String[] params = line.split("\\s+");
       String name = params[0];
       ArrayList<Integer> ranks = new ArrayList<Integer>();
       for (int i = 1; i < params.length; i++) {
          ranks.add(Integer.parseInt(params[i]));
       }
       data.put(name, ranks);
      }
    } catch (Exception e) {
      System.out.println("Error: Could not read data from " + filename);
    }
    return data;
}

// Method to display bar graph
public static void displayBarGraph(HashMap<String,ArrayList<Integer>> data, String name, int maxHeight) {
    System.out.println("\nHistorical popularity of " + name + ":");
    System.out.println("(Ranks shown above each bar, 0 indicates not popular enough to be ranked)\n");
    int bin = 1000 / (maxHeight-3);...

By purchasing this solution you'll be able to access the following files:
Solution.java and SolutionData.txt.

$45.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats