Unit 6 - Project One Baby Names Problem Description and Program Behavior: This a
ID: 3579661 • Letter: U
Question
Unit 6 - Project One
Baby Names
Problem Description and Program Behavior:
This assignment will give you practice with file processing. Turn in your code on a Google Doc.
Every 10 years, the Social Security Administration gives data about the 1000 most popular boy and girl names for children born in the US. This data is provided on the web at http://www.ssa.gov/OACT/babynames/. Your task in this program is to prompt the user for a name, and then to display popularity statistics about that name for each decade since 1900.
Your program is to give an introduction and then prompt the user for a name to display. Then it will read through a data file searching for that name. If the name is found in the file, you should print the name and the statistics about that name's popularity in each decade.
Your program will read its data from a file named names.txt - which you can download from Google Classroom.
Each line of this file has a name, followed by the popularity rank of that name in 1900, 1910, 1920, and so on. The default input file has 11 numbers per line, meaning that the last number represents the ranking in the year 2000. A rank of 1 was the most popular name that year, while a rank of 999 was not very popular. A rank of 0 means the name did not appear in the top 1000 that year at all. Here is a sample of the data:
Lionel 387 344 369 333 399 386 408 553 492 829 972
Lisa 0 0 0 0 464 38 1 6 31 113 298
Lise 0 0 0 0 0 997 0 0 0 0 0
Lisette 0 0 0 0 0 0 0 816 958 0 864
"Lionel" was #387 in 1900 and is slowly decreasing. "Lisa" made the list in 1940 and peaked in 1960 at #1.
If the name is not found in the file, you should simply output that it was not found, and not print any data.
Your program should work correctly regardless of the capitalization the user uses to type the name. For example, if the user asks you to search for "LISA" or "lisa", you should find it even though the input file has it as "Lisa".
Stylistic Guidelines:
For this program you should have four class constants to represent the following values:
· the name of the input file (default of "names.txt")
· the starting year of the input data (default of 1900)
· the number of decades' worth of data in each line of the file (default of 11)
· the width used for each decade on the drawing panel (default of 50)
Use methods for structure and to avoid redundancy. For full credit, your methods should obey the following constraints:
· The main method should not read lines of input from a file.
· The code that asks the user for a name must not be in the same method as any code to read lines of input from a file.
For this assignment you are limited to the language featured in Units 1 through 6. Follow past stylistic guidelines about indentation, whitespace, identifier names, and localizing variables, and commenting at the beginning of your program, at the start of each method, and on complex sections of code.
Here is a sample of what your output might look like:
This program graphs the popularity of a name
in Social Security baby name statistics
recorded since the year 1900.
Type a name: Lisa
Popularity ranking of name "Lisa"
1900: 0
1910: 0
1920: 0
1930: 0
1940: 464
1950: 38
1960: 1
1970: 6
1980: 31
1990: 113
2000: 298
Explanation / Answer
Here I've provided code by extracting date from a local file you can use yours by changing the path.
names.txt
Lionel 387 344 369 333 399 386 408 553 492 829 972
Lisa 0 0 0 0 464 38 1 6 31 113 298
Lise 0 0 0 0 0 997 0 0 0 0 0
Lisette 0 0 0 0 0 0 0 816 958 0 864
Latthem 5 7 99 85 25 25 25 32 16 10 5
Liander 464 38 1 85 25 25 958 0 408 553 492
Lessley 333 39 5 7 99 85 25 2 58 0 4
Ross 9 85 2 33 39 5 0 0 0 8 1
Taylor 9 85 25 25 25 3 58 0 40 51
Cook 0 0 0 54 78 21 2 2 1 35 10
Root 5 8 4 1 1 1 1 25 20 15 10
Names.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class Names {
private final static String INPUT_FILE="names.txt";//file name
private final static int START_YEAR=1900; //start year
private final int NUM_DEC=11;
//method to ask user to enter the name.
private static String askName(){
Scanner s=new Scanner(System.in);
System.out.println("Type a Name: ");
String name=s.nextLine();
return name;
}
//method to search the record from the file for the entered name
private static String[] searchRecord(String name){
String k1[]={};
try{
BufferedReader br=new BufferedReader(new FileReader("F:\Corsera1\Chegg\src\"+INPUT_FILE));
String line="";
while((line=br.readLine())!=null){
String []k=line.split(" ");
if(k[0].equalsIgnoreCase(name)){
return k;
}
}
}catch(Exception e){e.printStackTrace();}
return k1;
}
public static void main(String a[]){
String name=askName();
String record[]=searchRecord(name);
//if record is available
if(record.length>0){
System.out.println("Popularity ranking of name ""+record[0]+""");
for(int i=1;i<record.length;i++){
int rank=Integer.parseInt(record[i]);
System.out.println((START_YEAR+(i-1)*10)+": "+rank);
}
}
//if not available
else{
System.out.println("Name Not Found");
}
}
}
Output:
Type a Name:
Lessley
Popularity ranking of name "Lessley"
1900: 333
1910: 39
1920: 5
1930: 7
1940: 99
1950: 85
1960: 25
1970: 2
1980: 58
1990: 0
2000: 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.